Advertisement
Ogomegbunam

Modular Smart Irrigation

Jan 1st, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.76 KB | Source Code | 0 0
  1. /*
  2.  * This product was developed by AGREM SYSTEMS;
  3.  * designed to measure the Relative humidity, atmospheric temperature, Soil temperature, moisture content, EC and pH.
  4.  * The system controls a pump/valve system for smart irrigation.
  5.  *  DECEMBER 9TH, 2024.
  6.  *  CONNECTIONS
  7.  *  Dht22 = 12
  8.  *  Relay = 11
  9.  *  RO = 3
  10.  *  DI = 2
  11.  *  RE = 6
  12.  *  DE = 7
  13.  *  Temperature probe = 4
  14.  *  SDA = A4
  15.  *  SCL = A5
  16.  */
  17. #include <SoftwareSerial.h>
  18. #include <DHT22.h>
  19.  
  20. //define pin data
  21. #define pinDATA 12
  22.  
  23. DHT22 dht22(pinDATA);
  24.  
  25. #include <Wire.h>
  26. #include <LiquidCrystal_I2C.h>
  27.  
  28. LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display
  29.  
  30.  
  31. #include <OneWire.h>
  32. #include <DallasTemperature.h>
  33.  int ONE_WIRE_BUS = 4;
  34.  OneWire oneWire (ONE_WIRE_BUS);
  35.  DallasTemperature sensors (&oneWire);
  36.  
  37. #define moisturepin A0
  38.  
  39. #define relaypin 11
  40. #define RE 7
  41. #define DE 6
  42.  
  43. const uint32_t TIMEOUT = 500UL;
  44.  
  45. const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
  46. const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
  47. const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
  48. const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
  49.  
  50. byte values[11];
  51. SoftwareSerial mod(3, 2); // RO Rx pin,DI Tx pin
  52.  
  53. void setup() {
  54.   pinMode(moisturepin,INPUT);  // mositure sensor
  55.   pinMode(relaypin,OUTPUT);  // Relay module control
  56.     pinMode(12,INPUT);
  57.     Serial.begin(115200);
  58.   sensors.begin();
  59.    lcd.init();
  60.   // Print a message to the LCD.
  61.   lcd.backlight();
  62.  
  63.   mod.begin(4800);
  64.   pinMode(RE, OUTPUT);
  65.   pinMode(DE, OUTPUT);
  66.  
  67.   delay(500);
  68. }
  69.  
  70. void loop() {
  71.  
  72.  uint16_t val1, val2, val3, val4;
  73.  
  74.   Serial.println("Moisture: ");
  75.   val1 = moisture();
  76.   float Val1 = val1*0.1;
  77.   Serial.print(Val1);
  78.   Serial.println(" %");
  79.   Serial.println("-----");
  80.  
  81.   Serial.println("Temperature: ");
  82.   val2 = temperature();
  83.   float Val2 = val2*0.1;
  84.   Serial.print(Val2);
  85.   Serial.println(" *C");
  86.   Serial.println("-----");
  87.  
  88.   Serial.println("Conductivity: ");
  89.   val3 = conductivity();
  90.   Serial.print(val3);
  91.   Serial.println(" us/cm");
  92.   Serial.println("-----");
  93.  
  94.   Serial.println("Ph: ");
  95.   val4 = ph();
  96.   float Val4 = val4 * 0.1;
  97.   Serial.print(Val4);
  98.   Serial.println(" ph");
  99.   Serial.println("-----");
  100.  
  101.   //dht section
  102.   Serial.println(dht22.debug()); //optionnal
  103.  
  104.   float t = dht22.getTemperature();
  105.   float h = dht22.getHumidity();
  106.  
  107.   if (dht22.getLastError() != dht22.OK) {
  108.     Serial.print("last error :");
  109.     Serial.println(dht22.getLastError());
  110.   }
  111.  
  112.   Serial.print("h=");Serial.print(h,1);Serial.print("\t");
  113.   Serial.print("t=");Serial.println(t,1);
  114.  
  115. // AGREM SYSTEM DISPLAY SECTION
  116.  lcd.clear();
  117.  delay(1);
  118.     lcd.setCursor(0, 0);
  119.   lcd.print("Tg");
  120.     lcd.setCursor(9, 0);
  121.   lcd.print("C");
  122.     lcd.setCursor(0, 1);
  123.   lcd.print("Mg");
  124.     lcd.setCursor(9, 1);
  125.   lcd.print("%");
  126.     lcd.setCursor(0,3);
  127.   lcd.print("PHg");
  128.     lcd.setCursor(0,2);
  129.   lcd.print("ECg");
  130.     lcd.setCursor(11, 0);
  131.   lcd.print("Tp");
  132.     lcd.setCursor(19, 0);
  133.   lcd.print("C");
  134.     lcd.setCursor(11, 1);
  135.   lcd.print("Mp");
  136.     lcd.setCursor(11,2);
  137.   lcd.print("Ta");
  138.      lcd.setCursor(19,2);
  139.   lcd.print("C");
  140.     lcd.setCursor(11,3);
  141.   lcd.print("Ha");
  142.     lcd.setCursor(19,3);
  143.   lcd.print("%");
  144.    
  145.     lcd.setCursor(3, 1);
  146.   lcd.print(Val1);
  147.  
  148.     lcd.setCursor(3, 0);
  149.   lcd.print(Val2);
  150.  
  151.     lcd.setCursor(4, 2);
  152.   lcd.print(val3);
  153.  
  154.     lcd.setCursor(4, 3);
  155.   lcd.print(Val4);
  156.     lcd.setCursor(13,2);
  157.   lcd.print(t);
  158.  
  159.   lcd.setCursor(13,3);
  160.   lcd.print(h);
  161.      lcd.setCursor(13, 0);
  162.   lcd.print(sensors.getTempCByIndex(0));
  163.  
  164.   if (Val1 <= 55.00) {                    //value for minimum value to open the valve
  165.     digitalWrite(relaypin, HIGH);
  166.    Serial.println("Pump On");
  167.     lcd.setCursor(17, 1);
  168.     lcd.print("ON ");
  169.  
  170.   }
  171.   if (Val1 >= 80.00) {                   //value for maximum value to close the valve
  172.     digitalWrite(relaypin, LOW);
  173.     Serial.println("Pump Off");
  174.     lcd.setCursor(17, 1);
  175.     lcd.print("OFF");
  176.  
  177.   }
  178.  
  179. //Temperature probe
  180. sensors.requestTemperatures ();
  181.  
  182. Serial.print("Celsius temperature: ");
  183.  
  184. Serial.print(sensors.getTempCByIndex(0));
  185.  
  186. Serial.print(" - Fahrenheit temperature: ");
  187.  
  188. Serial.println (sensors.getTempFByIndex(0));
  189.  
  190. //Moisture probe
  191. int sensorValue = analogRead(moisturepin);
  192. Serial.println ("MOISTURE READING");
  193. Serial.print (sensorValue);
  194.  lcd.setCursor(13, 1);
  195. lcd.print(sensorValue);  
  196.  
  197.  delay(2000);
  198. }
  199.  
  200. int16_t moisture() {
  201.   uint32_t startTime = 0;
  202.   uint8_t  byteCount = 0;
  203.  
  204.   digitalWrite(DE, HIGH);
  205.   digitalWrite(RE, HIGH);
  206.   delay(10);
  207.   mod.write(moist, sizeof(moist));
  208.   mod.flush();
  209.   digitalWrite(DE, LOW);
  210.   digitalWrite(RE, LOW);
  211.  
  212.   startTime = millis();
  213.   while ( millis() - startTime <= TIMEOUT ) {
  214.     if (mod.available() && byteCount<sizeof(values) ) {
  215.       values[byteCount++] = mod.read();
  216.       printHexByte(values[byteCount-1]);
  217.     }
  218.   }
  219.   Serial.println();
  220.   return (int16_t)(values[3] << 8 | values[4]);
  221.  
  222. }
  223.  
  224. int16_t temperature() {
  225.   uint32_t startTime = 0;
  226.   uint8_t  byteCount = 0;
  227.  
  228.   digitalWrite(DE, HIGH);
  229.   digitalWrite(RE, HIGH);
  230.   delay(10);
  231.   mod.write(temp, sizeof(temp));
  232.   mod.flush();
  233.   digitalWrite(DE, LOW);
  234.   digitalWrite(RE, LOW);
  235.  
  236.   startTime = millis();
  237.   while ( millis() - startTime <= TIMEOUT ) {
  238.     if (mod.available() && byteCount<sizeof(values) ) {
  239.       values[byteCount++] = mod.read();
  240.       printHexByte(values[byteCount-1]);
  241.     }
  242.   }
  243.   Serial.println();
  244.   return (int16_t)(values[3] << 8 | values[4]);
  245.  
  246. }
  247.  
  248. int16_t conductivity() {
  249.   uint32_t startTime = 0;
  250.   uint8_t  byteCount = 0;
  251.  
  252.   digitalWrite(DE, HIGH);
  253.   digitalWrite(RE, HIGH);
  254.   delay(10);
  255.   mod.write(EC, sizeof(EC));
  256.   mod.flush();
  257.   digitalWrite(DE, LOW);
  258.   digitalWrite(RE, LOW);
  259.  
  260.   startTime = millis();
  261.   while ( millis() - startTime <= TIMEOUT ) {
  262.     if (mod.available() && byteCount<sizeof(values) ) {
  263.       values[byteCount++] = mod.read();
  264.       printHexByte(values[byteCount-1]);
  265.     }
  266.   }
  267.   Serial.println();
  268.   return (int16_t)(values[3] << 8 | values[4]);
  269.  
  270. }
  271.  
  272. int16_t ph() {
  273.   uint32_t startTime = 0;
  274.   uint8_t  byteCount = 0;
  275.  
  276.   digitalWrite(DE, HIGH);
  277.   digitalWrite(RE, HIGH);
  278.   delay(10);
  279.   mod.write(PH, sizeof(PH));
  280.   mod.flush();
  281.   digitalWrite(DE, LOW);
  282.   digitalWrite(RE, LOW);
  283.  
  284.   startTime = millis();
  285.   while ( millis() - startTime <= TIMEOUT ) {
  286.     if (mod.available() && byteCount<sizeof(values) ) {
  287.       values[byteCount++] = mod.read();
  288.       printHexByte(values[byteCount-1]);
  289.     }
  290.   }
  291.   Serial.println();
  292.   return (int16_t)(values[3] << 8 | values[4]);
  293.    
  294. }
  295.  
  296. void printHexByte(byte b)
  297. {
  298.   Serial.print((b >> 4) & 0xF, HEX);
  299.   Serial.print(b & 0xF, HEX);
  300.   Serial.print(' ');
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement