smart irrigation through wifi module
#include "DHT.h"
#include
#include
SoftwareSerial wifi(2, 3);
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int buz = A2;
#define RELAY_PIN 6
#define SOIL_SENSOR A0
bool autoMode = false; // Flag for auto mode
void setup() {
wifi.begin(115200);
Serial.begin(9600);
pinMode(buz, OUTPUT);
digitalWrite(buz, LOW);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
dht.begin();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" SMART IRRIGATION");
lcd.setCursor(0, 1);
lcd.print(" SYSTEM READY");
delay(2000);
wifi_init();
delay(3000);
}
void loop() {
int h = dht.readHumidity();
int t = dht.readTemperature();
int s = analogRead(SOIL_SENSOR);
int mos = map(s, 1023, 0, 0, 100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:" + String(t) + "C");
lcd.setCursor(10, 0);
lcd.print("H:" + String(h) + "%");
lcd.setCursor(0, 1);
lcd.print("Soil: " + String(mos) + "%");
delay(1000);
upload_iot(t, h, mos);
// Always check for command
int cmd = read_iot();
// Command Handling:
if (cmd == '1') {
autoMode = true; // Enter auto mode
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CMD:1 RECEIVED");
lcd.setCursor(0, 1);
lcd.print("AUTO MODE ON");
delay(1500);
} else if (cmd == '2') {
autoMode = false; // Exit auto mode
digitalWrite(RELAY_PIN, HIGH);
// Pump OFF
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CMD:2 RECEIVED");
lcd.setCursor(0, 1);
lcd.print("PUMP: OFF");
delay(1500);
digitalWrite(buz,1);
delay(2000);
digitalWrite(buz,0);
}
// If autoMode is active, check soil and control pump
if (autoMode) {
if (mos < 60) {
digitalWrite(RELAY_PIN, LOW); // Pump ON
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AUTO: Soil<60%");
lcd.setCursor(0, 1);
lcd.print("PUMP: ON");
} else {
digitalWrite(RELAY_PIN, HIGH); // Pump OFF
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AUTO: Soil>60%");
lcd.setCursor(0, 1);
lcd.print("PUMP: OFF");
}
delay(1500);
}
}
void wifi_init() {
wifi.println("AT+RST");
delay(2000);
wifi.println("AT+CWMODE=1");
delay(1000);
wifi.print("AT+CWJAP=");
wifi.write('"');
wifi.print("DIMPUL"); // SSID
wifi.write('"');
wifi.write(',');
wifi.write('"');
wifi.print("1234567890"); // Password
wifi.write('"');
wifi.println();
delay(1000);
}
void upload_iot(int x, int y, int z) {
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
wifi.println(cmd);
delay(1500);
String getStr = "GET /update?api_key=REIKT92SEF7HAZ4W&field1=";
getStr += String(x);
getStr += "&field2=";
getStr += String(y);
getStr += "&field3=";
getStr += String(z);
getStr += "\r\n\r\n";
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
wifi.println(cmd);
delay(1500);
wifi.println(getStr);
delay(1500);
}
int read_iot() {
int ch;
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
wifi.println(cmd);
delay(1500);
String getStr = "GET /channels/2878613/fields/1/last?api_key=UVJX2NA46R1BKRP1\r\n\r\n";
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
wifi.println(cmd);
delay(1500);
wifi.print(getStr);
delay(200);
ch = 0;
if (wifi.find("~")) {
delay(10);
ch = wifi.read();
}
return ch;
}
Comments
Post a Comment