#include <WiFi.h>
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
int valve = 18;
const char* ssid = "YehiaEssa";
const char* password = "MONAmona1960";
WiFiServer server(80);
int led = 2;
int rain = 34;
int rainSensorValue;
int moistureSensor = 35;
int moistureSensorValue;
int moistureSensorPrecentageValue;
int wifi_status;
void setup()
{
pinMode(led,OUTPUT);
pinMode(rain, INPUT);
pinMode(moistureSensor, INPUT);
pinMode(valve, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.println("Irrigation");
LCD.setCursor(0, 1);
LCD.println("ok4...");
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
server.begin();
}
void loop(){
irrigation_system();
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("Click <a href=\"/H\">here</a>Turn ON the system<br>");
client.print("Click <a href=\"/L\">here</a>Turn OFF the system<br>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
Serial.println(currentLine);
digitalWrite(led, HIGH); // GET /H turns the LED on
wifi_status = 1;
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(led, LOW); // GET /L turns the LED off
wifi_status = 0;
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
void irrigation_system(){
rainSensorValue = digitalRead(rain);
moistureSensorValue = analogRead(moistureSensor);
moistureSensorPrecentageValue = map(moistureSensorValue, 0, 4095, 0, 100);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Moisture level:");
LCD.setCursor(0,1);
LCD.print(String(moistureSensorPrecentageValue)+" %");
if((rainSensorValue == 1) | (moistureSensorPrecentageValue > 66) | (wifi_status == 1)){
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Low:" + String(moistureSensorPrecentageValue) + "%");
LCD.setCursor(0,1);
LCD.print("Valve Closing");
digitalWrite(valve, LOW);
LCD.setCursor(0,1);
LCD.print("Valve Closed");
delay(300);
}
else if((rainSensorValue == 0) | (moistureSensorPrecentageValue < 65)| (wifi_status == 0)){
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Low:" + String(moistureSensorPrecentageValue) + "%");
LCD.setCursor(0,1);
LCD.print("Valve Opening");
digitalWrite(valve, HIGH);
LCD.setCursor(0,1);
LCD.print("Valve opened");
delay(300);
}
// if((moistureSensorPrecentageValue > 66) ){
// LCD.clear();
// LCD.setCursor(0,0);
// LCD.print("Low:" + String(moistureSensorPrecentageValue) + "%");
// LCD.setCursor(0,1);
// LCD.print("Valve Closing");
// digitalWrite(valve, LOW);
// LCD.setCursor(0,1);
// LCD.print("Valve Closed");
// delay(300);
// }
// else if((moistureSensorPrecentageValue >= 66) | (rainSensorValue == 1 )){
// LCD.clear();
// LCD.setCursor(0,0);
// LCD.print("High:" + String(moistureSensorPrecentageValue) + "%");
// LCD.setCursor(0,1);
// LCD.print("Valve Closing");
// digitalWrite(valve, HIGH);
// LCD.setCursor(0,1);
// LCD.print("Valve Closed");
// delay(300);
// }
// delay(300);
}