#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL60Kt8qFNm"
#define BLYNK_TEMPLATE_NAME "Automatic Plant Watering System"
#define BLYNK_AUTH_TOKEN "U9_CSMoClK34xevtnP0pb7MuhExBQ8FF"
#include <LiquidCrystal.h>
#include <DHT_U.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WebServer.h>
#define LEDred 23
#define LEDgreen 22
#define DHTPIN 12
#define SERVOPIN 14
#define SWITCH_LEFT 18
#define SWITCH_RIGHT 19
// DHT parameters
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
// Servo motor
Servo servo;
LiquidCrystal lcd(2, 0, 4, 16, 17, 5);
float temp, hum;
bool switchState = false; // Initial state, assuming the switch is at 18 (Left)
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Your Wi-Fi SSID
char pass[] = ""; // Your Wi-Fi Password
BlynkTimer timer;
WebServer server(80); // Create a web server on port 80
double latitude = 0.0;
double longitude = 0.0;
void setup() {
pinMode(LEDred, OUTPUT);
pinMode(LEDgreen, OUTPUT);
pinMode(SWITCH_LEFT, INPUT_PULLUP); // Left switch
pinMode(SWITCH_RIGHT, INPUT_PULLUP); // Right switch
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup servo
servo.attach(SERVOPIN, 500, 2400);
servo.write(0);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
String message1 = " Automatic";
String message2 = "Watering System";
for (int i = 0; i < message1.length(); i++) {
lcd.print(message1.charAt(i));
delay(100);
}
lcd.setCursor(0, 1);
for (int i = 0; i < message2.length(); i++) {
lcd.print(message2.charAt(i));
delay(100);
}
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity=");
lcd.setCursor(0, 1);
lcd.print("Temp=");
// Setup Blynk virtual pins
Blynk.virtualWrite(V0, 0); // Initialize gauge for temperature
Blynk.virtualWrite(V1, 0); // Initialize gauge for humidity
Blynk.virtualWrite(V2, 0); // Initialize gauge for Water Level
Blynk.virtualWrite(V3, 0); // Initialize LED widget
Blynk.virtualWrite(V4, switchState); // Initialize Switch
Blynk.syncVirtual(V4); // Sync switch state with the Blynk app
// Read initial switch state
switchState = digitalRead(SWITCH_LEFT) == LOW; // Assuming LOW state when pressed
// Start the web server
server.on("/", HTTP_GET, []() {
String html = "<!DOCTYPE html><html><head><title>Simple Map with Leaflet</title><meta charset='utf-8' />";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<link rel='stylesheet' href='https://unpkg.com/leaflet/dist/leaflet.css' />";
html += "<style>#map { height: 400px; }</style></head><body>";
html += "<div id='map'></div>";
html += "<script src='https://unpkg.com/leaflet/dist/leaflet.js'></script>";
html += "<script>var map = L.map('map').setView([" + String(latitude, 6) + ", " + String(longitude, 6) + "], 13);";
html += "L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: 'OpenStreetMap' }).addTo(map);";
html += "L.marker([" + String(latitude, 6) + ", " + String(longitude, 6) + "]).addTo(map).bindPopup('You are here!').openPopup();";
html += "map.setView([" + String(latitude, 6) + ", " + String(longitude, 6) + "], 13);</script>";
html += "</body></html>";
server.send(200, "text/html", html);
});
server.begin();
}
void loop() {
Blynk.run();
timer.run();
server.handleClient();
// Read temperature and humidity
sensors_event_t event;
dht.temperature().getEvent(&event);
temp = event.temperature;
dht.humidity().getEvent(&event);
hum = event.relative_humidity;
// Update LCD display
lcd.setCursor(9, 0);
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(9, 1);
lcd.print(hum);
lcd.print(" %");
// Update Blynk virtual pins
Blynk.virtualWrite(V0, temp);
Blynk.virtualWrite(V1, hum);
// Control servo motor
if (switchState) {
servo.write(90);
} else {
servo.write(0);
}
// Update switch state
switchState = digitalRead(SWITCH_LEFT) == LOW;
// Update Blynk virtual pin for switch
Blynk.virtualWrite(V4, switchState);
// Get Wi-Fi location
WiFiClient client;
HTTPClient http;
http.begin(client, "http://ip-api.com/json");
int httpCode = http.GET();
if (httpCode > 0) {
String response = http.getString();
DynamicJsonDocument jsonDoc(2048);
jsonDoc.parse(response);
latitude = jsonDoc["lat"];
longitude = jsonDoc["lon"];
}
http.end();
// Update web server with new location
server.on("/", HTTP_GET, []() {
String html = "<!DOCTYPE html><html><head><title>Simple Map with Leaflet</title><meta charset='utf-8' />";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<link rel='stylesheet' href='https://unpkg.com/leaflet/dist/leaflet.css' />";
html += "<style>#map { height: 400px; }</style></head><body>";
html += "<div id='map'></div>";
html += "<script src='https://unpkg.com/leaflet/dist/leaflet.js'></script>";
html += "<script>var map = L.map('map').setView([" + String(latitude, 6) + ", " + String(longitude, 6) + "], 13);";
html += "L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: 'OpenStreetMap' }).addTo(map);";
html += "L.marker([" + String(latitude, 6) + ", " + String(longitude, 6) + "]).addTo(map).bindPopup('You are here!').openPopup();";
html += "map.setView([" + String(latitude, 6) + ", " + String(longitude, 6) + "], 13);</script>";
html += "</body></html>";
server.send(200, "text/html", html);
});
}