Advertisement
Ogomegbunam

MAX 485

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