#define BLYNK_TEMPLATE_ID "TMPL6sSTSU4i5"
#define BLYNK_TEMPLATE_NAME "Green House Automation System"
#define BLYNK_AUTH_TOKEN "AWqFckllrZYAx4vgFjEW3oefqi212ERs"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 32
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
const int LDR_PIN =35;
const int relayPin1 =19;
const int relayPin2 =18;
const int relayPin3 =17;
const int relayPin4 =16;
const int relayPin5 =4;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
BlynkTimer timer;
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
int potValue = analogRead(34);
Blynk.virtualWrite(V2, potValue);
}
void sendSensor()
{
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("V3 Motor status: ");
Serial.println(pinValue);
digitalWrite(18, pinValue);
}
void setup() {
// put your setup code here, to run once:
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
pinMode(relayPin5, OUTPUT);
pinMode(LDR_PIN,INPUT);
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.println("Hello, ESP32!");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dht.begin();
timer.setInterval(1000L, sendSensor);
timer.setInterval(1000L, myTimerEvent);
}
void CO2Level() {
int16_t z = analogRead(25);
String msg1 = z < 800 ? "Low" : z > 1000 ? "YES" : "OK";
lcd.clear();
lcd.setCursor(0,1);
lcd.print("CO2: ");
lcd.print(msg1);
lcd.setCursor(0, 0);
lcd.print("GAS Active");
delay(1000); // print message at (0, 0)
if (z < 800){
digitalWrite(relayPin1, LOW);
} else {
digitalWrite(relayPin1, HIGH);
}
}
void MoistureLevel() {
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Soil: ");
lcd.print(msg);
delay(1000); // print message at (0, 0)
if (i<3135)
{
digitalWrite(relayPin2,LOW);
delay(500);
}
else if(i>3500)
{
digitalWrite(relayPin2,HIGH);
delay(500);
}
}
void PhLevel() {
int16_t p = analogRead(33);
lcd.clear();
lcd.setCursor(10,0);
lcd.print("PH: ");
lcd.print(String(p));
delay(1000); // print message at (0, 0)
Blynk.virtualWrite(V4, p);
if (p<8)
{
digitalWrite(relayPin3,LOW);
delay(500);
}
else if(p>7)
{
digitalWrite(relayPin3,HIGH);
delay(500);
}
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float temp = dht.readTemperature();
if (isnan(h) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if (temp < 27){
digitalWrite(relayPin5, LOW);
} else {
digitalWrite(relayPin5, HIGH);
}
}
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V0, temp);
delay(10); // this speeds up the simulation
int sensorValue=analogRead(LDR_PIN); // Read the LDR value (0-4095)
int brightness =map(sensorValue,0,4095,0,255);// Map to LED brightness (0-255)
if (sensorValue<1000)
{
digitalWrite(relayPin4,LOW);
delay(500);
}
else if(sensorValue>2000)
{
digitalWrite(relayPin4,HIGH);
delay(500);
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("°C ~ ");
Serial.print("PH: ");
Serial.print(analogRead(33));
Serial.print(" | ");
Serial.print("CO2: ");
Serial.print(800);
Serial.println("Current Flowing");
delay(1000);
} else {
Serial.println("Current not Flowing");
CO2Level();
MoistureLevel();
PhLevel();
Blynk.run();
timer.run();
}