Advertisement
Ogomegbunam

Soil station Abridged 1602

Mar 25th, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | Food | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <SimpleDHT.h>
  3. #define pinDHT11 5
  4. SimpleDHT11 dht11(pinDHT11);
  5.  
  6. #include <Wire.h>
  7. #include <LiquidCrystal_I2C.h>
  8.  
  9. LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  10.  
  11.  
  12. #include <OneWire.h>
  13. #include <DallasTemperature.h>
  14. int ONE_WIRE_BUS = 4;
  15. OneWire oneWire (ONE_WIRE_BUS);
  16. DallasTemperature sensors (&oneWire);
  17.  
  18. #define moisturepin A0
  19.  
  20. #define relaypin A2
  21. #define RE 7
  22. #define DE 6
  23.  
  24. const uint32_t TIMEOUT = 500UL;
  25.  
  26. const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
  27. const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
  28. const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
  29. const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
  30.  
  31. byte values[11];
  32. SoftwareSerial mod(2, 3); // Rx pin, Tx pin
  33.  
  34. void setup() {
  35. pinMode(moisturepin,INPUT); // mositure sensor
  36. pinMode(relaypin,OUTPUT); // Relay module control
  37. pinMode(5,INPUT);
  38. Serial.begin(115200);
  39. sensors.begin();
  40. lcd.init();
  41. // Print a message to the LCD.
  42. lcd.backlight();
  43.  
  44. mod.begin(4800);
  45. pinMode(RE, OUTPUT);
  46. pinMode(DE, OUTPUT);
  47.  
  48. delay(500);
  49. }
  50.  
  51. void loop() {
  52.  
  53. uint16_t val1, val2, val3, val4;
  54.  
  55. Serial.println("Moisture: ");
  56. val1 = moisture();
  57. float Val1 = val1*0.1;
  58. Serial.print(Val1);
  59. Serial.println(" %");
  60. Serial.println("-----");
  61.  
  62. Serial.println("Temperature: ");
  63. val2 = temperature();
  64. float Val2 = val2*0.1;
  65. Serial.print(Val2);
  66. Serial.println(" *C");
  67. Serial.println("-----");
  68.  
  69. Serial.println("Conductivity: ");
  70. val3 = conductivity();
  71. Serial.print(val3);
  72. Serial.println(" us/cm");
  73. Serial.println("-----");
  74.  
  75. Serial.println("Ph: ");
  76. val4 = ph();
  77. float Val4 = val4*0.1;
  78. Serial.print(Val4);
  79. Serial.println(" ph");
  80. Serial.println("-----");
  81.  
  82.  
  83. lcd.setCursor(0, 0);
  84. lcd.print("Tg");
  85. lcd.setCursor(7, 0);
  86. lcd.print("C");
  87. lcd.setCursor(0, 1);
  88. lcd.print("Mg");
  89. lcd.setCursor(7, 1);
  90. lcd.print("%");
  91. lcd.setCursor(9, 0);
  92. lcd.print("ECg");
  93. lcd.setCursor(9, 1);
  94. lcd.print("PHg");
  95.  
  96. lcd.setCursor(2, 1);
  97. lcd.print(Val1);
  98.  
  99. lcd.setCursor(2, 0);
  100. lcd.print(Val2);
  101.  
  102. lcd.setCursor(12, 0);
  103. lcd.print(val3);
  104.  
  105. lcd.setCursor(12, 1);
  106. lcd.print(Val4);
  107.  
  108. // lcd.setCursor(10, 0);
  109. // lcd.print(sensors.getTempCByIndex(0));
  110.  
  111. //Moisture probe
  112. int sensorValue = analogRead(moisturepin);
  113. Serial.println ("MOISTURE READING");
  114. Serial.print (sensorValue);
  115. // lcd.setCursor(10, 1);
  116. //lcd.print(sensorValue);
  117.  
  118. //dht section
  119. // start working...
  120. Serial.println("=================================");
  121. Serial.println("Sample DHT11...");
  122.  
  123. // read without samples.
  124. byte temperature = 0;
  125. byte humidity = 0;
  126. int err = SimpleDHTErrSuccess;
  127. if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  128. Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
  129. Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
  130. return;
  131. }
  132.  
  133. Serial.print("Sample OK: ");
  134. Serial.print((int)temperature); Serial.print(" *C, ");
  135. Serial.print((int)humidity); Serial.println(" H");
  136.  
  137. //Temperature probe
  138. sensors.requestTemperatures ();
  139.  
  140. Serial.print("Celsius temperature: ");
  141.  
  142. Serial.print(sensors.getTempCByIndex(0));
  143.  
  144. Serial.print(" - Fahrenheit temperature: ");
  145.  
  146. Serial.println (sensors.getTempFByIndex(0));
  147.  
  148. delay(3000);
  149.  
  150. if (Val1 <= 55.00) { //value for minimum value to open the valve
  151. analogWrite(relaypin, 255);
  152. Serial.print("Valve on");
  153. // lcd.setCursor(18, 2);
  154. // lcd.print("ON");
  155. Serial.print("ON");
  156. }
  157. if (Val1 >= 80.00) { //value for maximum value to close the valve
  158. analogWrite(relaypin, 0);
  159. Serial.print("Valve off");
  160. // lcd.setCursor(17, 2);
  161. // lcd.print("OFF");
  162. Serial.print("OFF");
  163. }
  164.  
  165. }
  166.  
  167. int16_t moisture() {
  168. uint32_t startTime = 0;
  169. uint8_t byteCount = 0;
  170.  
  171. digitalWrite(DE, HIGH);
  172. digitalWrite(RE, HIGH);
  173. delay(10);
  174. mod.write(moist, sizeof(moist));
  175. mod.flush();
  176. digitalWrite(DE, LOW);
  177. digitalWrite(RE, LOW);
  178.  
  179. startTime = millis();
  180. while ( millis() - startTime <= TIMEOUT ) {
  181. if (mod.available() && byteCount<sizeof(values) ) {
  182. values[byteCount++] = mod.read();
  183. printHexByte(values[byteCount-1]);
  184. }
  185. }
  186. Serial.println();
  187. return (int16_t)(values[4] << 8 | values[5]);
  188.  
  189. }
  190.  
  191. int16_t temperature() {
  192. uint32_t startTime = 0;
  193. uint8_t byteCount = 0;
  194.  
  195. digitalWrite(DE, HIGH);
  196. digitalWrite(RE, HIGH);
  197. delay(10);
  198. mod.write(temp, sizeof(temp));
  199. mod.flush();
  200. digitalWrite(DE, LOW);
  201. digitalWrite(RE, LOW);
  202.  
  203. startTime = millis();
  204. while ( millis() - startTime <= TIMEOUT ) {
  205. if (mod.available() && byteCount<sizeof(values) ) {
  206. values[byteCount++] = mod.read();
  207. printHexByte(values[byteCount-1]);
  208. }
  209. }
  210. Serial.println();
  211. return (int16_t)(values[4] << 8 | values[5]);
  212.  
  213. }
  214.  
  215. int16_t conductivity() {
  216. uint32_t startTime = 0;
  217. uint8_t byteCount = 0;
  218.  
  219. digitalWrite(DE, HIGH);
  220. digitalWrite(RE, HIGH);
  221. delay(10);
  222. mod.write(EC, sizeof(EC));
  223. mod.flush();
  224. digitalWrite(DE, LOW);
  225. digitalWrite(RE, LOW);
  226.  
  227. startTime = millis();
  228. while ( millis() - startTime <= TIMEOUT ) {
  229. if (mod.available() && byteCount<sizeof(values) ) {
  230. values[byteCount++] = mod.read();
  231. printHexByte(values[byteCount-1]);
  232. }
  233. }
  234. Serial.println();
  235. return (int16_t)(values[4] << 8 | values[5]);
  236.  
  237. }
  238.  
  239. int16_t ph() {
  240. uint32_t startTime = 0;
  241. uint8_t byteCount = 0;
  242.  
  243. digitalWrite(DE, HIGH);
  244. digitalWrite(RE, HIGH);
  245. delay(10);
  246. mod.write(PH, sizeof(PH));
  247. mod.flush();
  248. digitalWrite(DE, LOW);
  249. digitalWrite(RE, LOW);
  250.  
  251. startTime = millis();
  252. while ( millis() - startTime <= TIMEOUT ) {
  253. if (mod.available() && byteCount<sizeof(values) ) {
  254. values[byteCount++] = mod.read();
  255. printHexByte(values[byteCount-1]);
  256. }
  257. }
  258. Serial.println();
  259. return (int16_t)(values[4] << 8 | values[5]);
  260.  
  261. }
  262.  
  263. void printHexByte(byte b)
  264. {
  265. Serial.print((b >> 4) & 0xF, HEX);
  266. Serial.print(b & 0xF, HEX);
  267. Serial.print(' ');
  268. }
Tags: RS48
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement