Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This product was developed by AGREM SYSTEMS;
- * designed to measure the Relative humidity, atmospheric temperature, Soil temperature, moisture content, EC and pH.
- * The system controls a pump/valve system for smart irrigation.
- * DECEMBER 9TH, 2024.
- * CONNECTIONS
- * Dht22 = 12
- * Relay = 11
- * RO = 3
- * DI = 2
- * RE = 6
- * DE = 7
- * Temperature probe = 4
- * SDA = A4
- * SCL = A5
- */
- #include <SoftwareSerial.h>
- #include <DHT22.h>
- //define pin data
- #define pinDATA 12
- DHT22 dht22(pinDATA);
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
- #include <OneWire.h>
- #include <DallasTemperature.h>
- int ONE_WIRE_BUS = 4;
- OneWire oneWire (ONE_WIRE_BUS);
- DallasTemperature sensors (&oneWire);
- #define moisturepin A0
- #define relaypin 11
- #define RE 7
- #define DE 6
- const uint32_t TIMEOUT = 500UL;
- const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
- const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
- const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
- const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
- byte values[11];
- SoftwareSerial mod(3, 2); // RO Rx pin,DI Tx pin
- void setup() {
- pinMode(moisturepin,INPUT); // mositure sensor
- pinMode(relaypin,OUTPUT); // Relay module control
- pinMode(12,INPUT);
- Serial.begin(115200);
- sensors.begin();
- lcd.init();
- // Print a message to the LCD.
- lcd.backlight();
- mod.begin(4800);
- pinMode(RE, OUTPUT);
- pinMode(DE, OUTPUT);
- delay(500);
- }
- void loop() {
- uint16_t val1, val2, val3, val4;
- Serial.println("Moisture: ");
- val1 = moisture();
- float Val1 = val1*0.1;
- Serial.print(Val1);
- Serial.println(" %");
- Serial.println("-----");
- Serial.println("Temperature: ");
- val2 = temperature();
- float Val2 = val2*0.1;
- Serial.print(Val2);
- Serial.println(" *C");
- Serial.println("-----");
- Serial.println("Conductivity: ");
- val3 = conductivity();
- Serial.print(val3);
- Serial.println(" us/cm");
- Serial.println("-----");
- Serial.println("Ph: ");
- val4 = ph();
- float Val4 = val4 * 0.1;
- Serial.print(Val4);
- Serial.println(" ph");
- Serial.println("-----");
- //dht section
- Serial.println(dht22.debug()); //optionnal
- float t = dht22.getTemperature();
- float h = dht22.getHumidity();
- if (dht22.getLastError() != dht22.OK) {
- Serial.print("last error :");
- Serial.println(dht22.getLastError());
- }
- Serial.print("h=");Serial.print(h,1);Serial.print("\t");
- Serial.print("t=");Serial.println(t,1);
- // AGREM SYSTEM DISPLAY SECTION
- lcd.clear();
- delay(1);
- lcd.setCursor(0, 0);
- lcd.print("Tg");
- lcd.setCursor(9, 0);
- lcd.print("C");
- lcd.setCursor(0, 1);
- lcd.print("Mg");
- lcd.setCursor(9, 1);
- lcd.print("%");
- lcd.setCursor(0,3);
- lcd.print("PHg");
- lcd.setCursor(0,2);
- lcd.print("ECg");
- lcd.setCursor(11, 0);
- lcd.print("Tp");
- lcd.setCursor(19, 0);
- lcd.print("C");
- lcd.setCursor(11, 1);
- lcd.print("Mp");
- lcd.setCursor(11,2);
- lcd.print("Ta");
- lcd.setCursor(19,2);
- lcd.print("C");
- lcd.setCursor(11,3);
- lcd.print("Ha");
- lcd.setCursor(19,3);
- lcd.print("%");
- lcd.setCursor(3, 1);
- lcd.print(Val1);
- lcd.setCursor(3, 0);
- lcd.print(Val2);
- lcd.setCursor(4, 2);
- lcd.print(val3);
- lcd.setCursor(4, 3);
- lcd.print(Val4);
- lcd.setCursor(13,2);
- lcd.print(t);
- lcd.setCursor(13,3);
- lcd.print(h);
- lcd.setCursor(13, 0);
- lcd.print(sensors.getTempCByIndex(0));
- if (Val1 <= 55.00) { //value for minimum value to open the valve
- digitalWrite(relaypin, HIGH);
- Serial.println("Pump On");
- lcd.setCursor(17, 1);
- lcd.print("ON ");
- }
- if (Val1 >= 80.00) { //value for maximum value to close the valve
- digitalWrite(relaypin, LOW);
- Serial.println("Pump Off");
- lcd.setCursor(17, 1);
- lcd.print("OFF");
- }
- //Temperature probe
- sensors.requestTemperatures ();
- Serial.print("Celsius temperature: ");
- Serial.print(sensors.getTempCByIndex(0));
- Serial.print(" - Fahrenheit temperature: ");
- Serial.println (sensors.getTempFByIndex(0));
- //Moisture probe
- int sensorValue = analogRead(moisturepin);
- Serial.println ("MOISTURE READING");
- Serial.print (sensorValue);
- lcd.setCursor(13, 1);
- lcd.print(sensorValue);
- delay(2000);
- }
- int16_t moisture() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(moist, sizeof(moist));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[3] << 8 | values[4]);
- }
- int16_t temperature() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(temp, sizeof(temp));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[3] << 8 | values[4]);
- }
- int16_t conductivity() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(EC, sizeof(EC));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[3] << 8 | values[4]);
- }
- int16_t ph() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(PH, sizeof(PH));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[3] << 8 | values[4]);
- }
- void printHexByte(byte b)
- {
- Serial.print((b >> 4) & 0xF, HEX);
- Serial.print(b & 0xF, HEX);
- Serial.print(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement