#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>
#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)
bool blynkSwitchState = false; // Blynk switch state initially reflects the physical switch
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
void updateBlynkSwitchState(bool state) {
Blynk.virtualWrite(V4, state ? 1 : 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(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
// Get temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
// 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
updateBlynkSwitchState(switchState);
}
void loop() {
Blynk.run();
timer.run();
bool leftSwitchState = digitalRead(SWITCH_LEFT) == LOW;
bool rightSwitchState = digitalRead(SWITCH_RIGHT) == LOW;
if (leftSwitchState != switchState) {
switchState = leftSwitchState;
digitalWrite(SWITCH_LEFT, switchState ? LOW : HIGH);
digitalWrite(SWITCH_RIGHT, !switchState ? LOW : HIGH);
updateBlynkSwitchState(switchState);
} else if (rightSwitchState != !switchState) {
switchState = !rightSwitchState;
digitalWrite(SWITCH_LEFT, switchState ? LOW : HIGH);
digitalWrite(SWITCH_RIGHT, !switchState ? LOW : HIGH);
updateBlynkSwitchState(switchState);
}
if (switchState == true) {
Blynk.virtualWrite(V3, HIGH);
// Read temperature and humidity
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
temp = event.temperature;
temp = constrain(temp, -40, 80);
lcd.setCursor(6, 1);
lcd.print(temp);
lcd.print("C");
Blynk.virtualWrite(V0, temp);
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
hum = event.relative_humidity;
hum = constrain(hum, 0, 100);
lcd.setCursor(10, 0);
lcd.print(hum);
lcd.print("%");
Blynk.virtualWrite(V1, hum);
}
if (hum < 30.0) {
if (temp > 30.0) {
servo.write(90);
digitalWrite(LEDgreen, HIGH);
digitalWrite(LEDred, LOW);
Blynk.virtualWrite(V2, 100);
} else {
servo.write(60);
digitalWrite(LEDgreen, HIGH);
digitalWrite(LEDred, LOW);
Blynk.virtualWrite(V2, 50);
}
} else {
servo.write(0);
digitalWrite(LEDgreen, LOW);
digitalWrite(LEDred, HIGH);
Blynk.virtualWrite(V2, 0);
}
} else {
servo.write(0);
digitalWrite(LEDgreen, LOW);
digitalWrite(LEDred, HIGH);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, LOW);
}
delay(1000);
}
BLYNK_WRITE(V4) {
int blynkSwitchValue = param.asInt();
if (blynkSwitchValue == 1 && switchState == false) {
switchState = true;
digitalWrite(SWITCH_LEFT, LOW);
digitalWrite(SWITCH_RIGHT, HIGH);
} else if (blynkSwitchValue == 0 && switchState == true) {
switchState = false;
digitalWrite(SWITCH_LEFT, HIGH);
digitalWrite(SWITCH_RIGHT, LOW);
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4