Advertisement
Ogomegbunam

Soil station 1602 moisture map

Apr 14th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 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. //LCD PRINTING
  83. //Page one
  84. lcd.setCursor(0, 0);
  85. lcd.print("Tg");
  86. lcd.setCursor(7, 0);
  87. lcd.print("C");
  88. lcd.setCursor(0, 1);
  89. lcd.print("Mg");
  90. lcd.setCursor(7, 1);
  91. lcd.print("%");
  92. lcd.setCursor(8, 0);
  93. lcd.print("Tp");
  94. lcd.setCursor(15, 0);
  95. lcd.print("C");
  96. lcd.setCursor(8, 1);
  97. lcd.print("Ta");
  98. lcd.setCursor(15, 1);
  99. lcd.print("C");
  100.  
  101. lcd.setCursor(2, 1);
  102. lcd.print(Val1);
  103. lcd.setCursor(2, 0);
  104. lcd.print(Val2);
  105. lcd.setCursor(10, 0);
  106. lcd.print(sensors.getTempCByIndex(0));
  107. lcd.setCursor(10, 1);
  108. lcd.print((int)temperature);
  109.  
  110. delay(2000);
  111. lcd.clear();
  112.  
  113. //page two
  114. lcd.setCursor(0, 0);
  115. lcd.print("ECg");
  116. lcd.setCursor(0, 1);
  117. lcd.print("PHg");
  118. lcd.setCursor(6, 1);
  119. lcd.print("ph");
  120. lcd.setCursor(8, 0);
  121. lcd.print("Mp");
  122. lcd.setCursor(15, 0);
  123. lcd.print("%");
  124. lcd.setCursor(8, 1);
  125. lcd.print("Ha");
  126. lcd.setCursor(15, 1);
  127. lcd.print("%");
  128.  
  129. lcd.setCursor(3, 0);
  130. lcd.print(val3);
  131. lcd.setCursor(3, 1);
  132. lcd.print(Val4);
  133. lcd.setCursor(10, 0);
  134. lcd.print(sensorVal);
  135. lcd.setCursor(10, 1);
  136. lcd.print((int)humidity);
  137.  
  138. delay(2000);
  139. lcd.clear();
  140.  
  141. //Moisture probe
  142. int sensorValue = analogRead(moisturepin);
  143. Serial.println ("MOISTURE READING");
  144. Serial.print (sensorValue);
  145. int sensorVal = map(sensorValue, 1023, 0, 0, 100);
  146.  
  147. //dht section
  148. // start working...
  149. Serial.println("=================================");
  150. Serial.println("Sample DHT11...");
  151.  
  152. // read without samples.
  153. byte temperature = 0;
  154. byte humidity = 0;
  155. int err = SimpleDHTErrSuccess;
  156. if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  157. Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
  158. Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
  159. return;
  160. }
  161.  
  162. Serial.print("Sample OK: ");
  163. Serial.print((int)temperature); Serial.print(" *C, ");
  164. Serial.print((int)humidity); Serial.println(" H");
  165.  
  166. //Temperature probe
  167. sensors.requestTemperatures ();
  168.  
  169. Serial.print("Celsius temperature: ");
  170.  
  171. Serial.print(sensors.getTempCByIndex(0));
  172.  
  173. Serial.print(" - Fahrenheit temperature: ");
  174.  
  175. Serial.println (sensors.getTempFByIndex(0));
  176.  
  177. delay(3000);
  178.  
  179. if (Val1 <= 55.00) { //value for minimum value to open the valve
  180. analogWrite(relaypin, 255);
  181. Serial.print("Valve on");
  182. // lcd.setCursor(18, 2);
  183. // lcd.print("ON");
  184. Serial.print("ON");
  185. }
  186. if (Val1 >= 80.00) { //value for maximum value to close the valve
  187. analogWrite(relaypin, 0);
  188. Serial.print("Valve off");
  189. // lcd.setCursor(17, 2);
  190. // lcd.print("OFF");
  191. Serial.print("OFF");
  192. }
  193.  
  194. }
  195.  
  196. int16_t moisture() {
  197. uint32_t startTime = 0;
  198. uint8_t byteCount = 0;
  199.  
  200. digitalWrite(DE, HIGH);
  201. digitalWrite(RE, HIGH);
  202. delay(10);
  203. mod.write(moist, sizeof(moist));
  204. mod.flush();
  205. digitalWrite(DE, LOW);
  206. digitalWrite(RE, LOW);
  207.  
  208. startTime = millis();
  209. while ( millis() - startTime <= TIMEOUT ) {
  210. if (mod.available() && byteCount<sizeof(values) ) {
  211. values[byteCount++] = mod.read();
  212. printHexByte(values[byteCount-1]);
  213. }
  214. }
  215. Serial.println();
  216. return (int16_t)(values[4] << 8 | values[5]);
  217.  
  218. }
  219.  
  220. int16_t temperature() {
  221. uint32_t startTime = 0;
  222. uint8_t byteCount = 0;
  223.  
  224. digitalWrite(DE, HIGH);
  225. digitalWrite(RE, HIGH);
  226. delay(10);
  227. mod.write(temp, sizeof(temp));
  228. mod.flush();
  229. digitalWrite(DE, LOW);
  230. digitalWrite(RE, LOW);
  231.  
  232. startTime = millis();
  233. while ( millis() - startTime <= TIMEOUT ) {
  234. if (mod.available() && byteCount<sizeof(values) ) {
  235. values[byteCount++] = mod.read();
  236. printHexByte(values[byteCount-1]);
  237. }
  238. }
  239. Serial.println();
  240. return (int16_t)(values[4] << 8 | values[5]);
  241.  
  242. }
  243.  
  244. int16_t conductivity() {
  245. uint32_t startTime = 0;
  246. uint8_t byteCount = 0;
  247.  
  248. digitalWrite(DE, HIGH);
  249. digitalWrite(RE, HIGH);
  250. delay(10);
  251. mod.write(EC, sizeof(EC));
  252. mod.flush();
  253. digitalWrite(DE, LOW);
  254. digitalWrite(RE, LOW);
  255.  
  256. startTime = millis();
  257. while ( millis() - startTime <= TIMEOUT ) {
  258. if (mod.available() && byteCount<sizeof(values) ) {
  259. values[byteCount++] = mod.read();
  260. printHexByte(values[byteCount-1]);
  261. }
  262. }
  263. Serial.println();
  264. return (int16_t)(values[4] << 8 | values[5]);
  265.  
  266. }
  267.  
  268. int16_t ph() {
  269. uint32_t startTime = 0;
  270. uint8_t byteCount = 0;
  271.  
  272. digitalWrite(DE, HIGH);
  273. digitalWrite(RE, HIGH);
  274. delay(10);
  275. mod.write(PH, sizeof(PH));
  276. mod.flush();
  277. digitalWrite(DE, LOW);
  278. digitalWrite(RE, LOW);
  279.  
  280. startTime = millis();
  281. while ( millis() - startTime <= TIMEOUT ) {
  282. if (mod.available() && byteCount<sizeof(values) ) {
  283. values[byteCount++] = mod.read();
  284. printHexByte(values[byteCount-1]);
  285. }
  286. }
  287. Serial.println();
  288. return (int16_t)(values[4] << 8 | values[5]);
  289.  
  290. }
  291.  
  292. void printHexByte(byte b)
  293. {
  294. Serial.print((b >> 4) & 0xF, HEX);
  295. Serial.print(b & 0xF, HEX);
  296. Serial.print(' ');
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement