Advertisement
Ogomegbunam

Max485 temp moist ph and conductivity sensor

Mar 24th, 2023
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | Source Code | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. #define RE 7
  4. #define DE 6
  5.  
  6. const uint32_t TIMEOUT = 500UL;
  7.  
  8. const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
  9. const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
  10. const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
  11. const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
  12.  
  13. byte values[11];
  14. SoftwareSerial mod(8, 9); // Rx pin, Tx pin
  15.  
  16. void setup() {
  17. Serial.begin(9600);
  18. // Serial.begin(4800);
  19. mod.begin(4800);
  20. pinMode(RE, OUTPUT);
  21. pinMode(DE, OUTPUT);
  22.  
  23. delay(500);
  24. }
  25.  
  26. void loop() {
  27. byte val1, val2, val3, val4;
  28.  
  29. Serial.println("Moisture: ");
  30. val1 = moisture();
  31. Serial.println(val1);
  32. Serial.println(" %");
  33. delay(1000);
  34.  
  35. Serial.println("Temperature: ");
  36. val2 = temperature();
  37. Serial.println(val2);
  38. Serial.println(" *C");
  39. delay(1000);
  40.  
  41. Serial.println("Conductivity: ");
  42. val3 = conductivity();
  43. delay(1000);
  44. Serial.println(val3);
  45. Serial.println(" us/cm");
  46.  
  47. Serial.println("Ph: ");
  48. val4 = ph();
  49. delay(1000);
  50. Serial.println(val4);
  51. Serial.println(" ph");
  52.  
  53. delay(5000);
  54. }
  55.  
  56. byte moisture() {
  57. uint32_t startTime = 0;
  58. uint8_t byteCount = 0;
  59.  
  60. digitalWrite(DE, HIGH);
  61. digitalWrite(RE, HIGH);
  62. delay(10);
  63. mod.write(moist, sizeof(moist));
  64. mod.flush();
  65. digitalWrite(DE, LOW);
  66. digitalWrite(RE, LOW);
  67.  
  68. startTime = millis();
  69. while ( millis() - startTime <= TIMEOUT ) {
  70. if (mod.available() && byteCount < sizeof(values) ) {
  71. values[byteCount++] = mod.read();
  72. printHexByte(values[byteCount - 1]);
  73. }
  74. }
  75. Serial.println();
  76. return values[4];
  77. }
  78.  
  79. byte temperature() {
  80. uint32_t startTime = 0;
  81. uint8_t byteCount = 0;
  82.  
  83. digitalWrite(DE, HIGH);
  84. digitalWrite(RE, HIGH);
  85. delay(10);
  86. mod.write(temp, sizeof(temp));
  87. mod.flush();
  88. digitalWrite(DE, LOW);
  89. digitalWrite(RE, LOW);
  90.  
  91. startTime = millis();
  92. while ( millis() - startTime <= TIMEOUT ) {
  93. if (mod.available() && byteCount < sizeof(values) ) {
  94. values[byteCount++] = mod.read();
  95. printHexByte(values[byteCount - 1]);
  96. }
  97. }
  98. Serial.println();
  99. return values[4];
  100. }
  101.  
  102. byte conductivity() {
  103. uint32_t startTime = 0;
  104. uint8_t byteCount = 0;
  105.  
  106. digitalWrite(DE, HIGH);
  107. digitalWrite(RE, HIGH);
  108. delay(10);
  109. mod.write(EC, sizeof(EC));
  110. mod.flush();
  111. digitalWrite(DE, LOW);
  112. digitalWrite(RE, LOW);
  113.  
  114. startTime = millis();
  115. while ( millis() - startTime <= TIMEOUT ) {
  116. if (mod.available() && byteCount < sizeof(values) ) {
  117. values[byteCount++] = mod.read();
  118. printHexByte(values[byteCount - 1]);
  119. }
  120. }
  121. Serial.println();
  122. return values[4];
  123. }
  124.  
  125. byte ph() {
  126. uint32_t startTime = 0;
  127. uint8_t byteCount = 0;
  128.  
  129. digitalWrite(DE, HIGH);
  130. digitalWrite(RE, HIGH);
  131. delay(10);
  132. mod.write(PH, sizeof(PH));
  133. mod.flush();
  134. digitalWrite(DE, LOW);
  135. digitalWrite(RE, LOW);
  136.  
  137. startTime = millis();
  138. while ( millis() - startTime <= TIMEOUT ) {
  139. if (mod.available() && byteCount < sizeof(values) ) {
  140. values[byteCount++] = mod.read();
  141. printHexByte(values[byteCount - 1]);
  142. }
  143. }
  144. Serial.println();
  145. return values[4];
  146. }
  147.  
  148. void printHexByte(byte b)
  149. {
  150. Serial.print((b >> 4) & 0xF, HEX);
  151. Serial.print(b & 0xF, HEX);
  152. Serial.print(' ');
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement