I’m trying to make an automated plant watering system using Esp32 and Blynk, So I’m using components such as relay, soil moisture sensor, water pump, DHT11 for showing temperature and humidity value of surroundings. Everything is working both in serial monitor as well as LCD display and I can able to see them in Blynk app too. But the problem is Relay is turned on whenever the code uploaded. what may be the issue and how can i able to resolve this issue?
#define BLYNK_TEMPLATE_ID "*******"
#define BLYNK_TEMPLATE_NAME "******"
#define BLYNK_AUTH_TOKEN "********"
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//SDA --> 21
//SCL --> 22
char ssid[] = "*****";
char pass[] = "*****";
BlynkTimer timer;
DHT dht(4, DHT11);
#define sensor 33
#define relay 27
void setup() {
Serial.begin(9600);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
lcd.init();
lcd.backlight();
dht.begin();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
lcd.setCursor(1, 0);
lcd.print("System Loading");
for (int a = 0; a <= 15; a++) {
lcd.setCursor(a, 1);
lcd.print(".");
delay(200);
}
lcd.clear();
timer.setInterval(5000L, DHT11sensor);
timer.setInterval(1500L, soilMoistureSensor);
}
//Get the soil moisture values
void soilMoistureSensor() {
int value = analogRead(sensor);
value = map(value, 0, 1024, 0, 100);
value = (value - 399)* -1 ;
if (value > 100){
value = 100;
}
Blynk.virtualWrite(V0, value);
Serial.print("Moisture value: ");
Serial.println(value);
lcd.setCursor(0, 0);
lcd.print("Moisture:");
lcd.print(value);
lcd.print(" ");
}
//Get the button value
BLYNK_WRITE(V1) {
bool Relay = param.asInt();
if (Relay == 1) {
digitalWrite(relay, LOW);
lcd.setCursor(0, 1);
lcd.print("Motor is ON ");
} else {
digitalWrite(relay, HIGH);
lcd.setCursor(0, 1);
lcd.print("Motor is OFF");
}
}
void DHT11sensor() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V2, t);
Blynk.virtualWrite(V3, h);
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity:");
Serial.println(h);
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(t);
lcd.setCursor(8, 1);
lcd.print("H:");
lcd.print(h);
}
void loop() {
Blynk.run(); //Run the Blynk library
timer.run(); //Run the Blynk timer
}