Advertisement
Ogomegbunam

Soil station 1602 two pages

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