Advertisement
Ogomegbunam

Soil Station source code

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