// Define Blynk template and authentication constants
#define BLYNK_TEMPLATE_ID "TMPL5xy6U0g6D"
#define BLYNK_TEMPLATE_NAME "Slide and button"
#define BLYNK_AUTH_TOKEN "eeVk3fmklYTu4_woET0wBp3nsdHPvxbZ"
// Include necessary libraries
#include <LiquidCrystal_I2C.h> // Library for controlling LCDs over I2C
#include <Wire.h> // Library for I2C data communication
#include <DHT.h> // Library for DHT22 temperature and humidity sensor
#include <WiFi.h> // Library for WiFi connectivity
#include <BlynkSimpleEsp32.h> // Library for integrating ESP32 with Blynk app
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 22, 4);
// Set pin numbers and types for DHT22 sensor and relay
#define dhtPin 18
#define dhttype DHT22
DHT dht(dhtPin, dhttype);
#define RelayPin 17
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Set default temperature threshold
float tempThreshold = 20.0;
// Heat state (on/off)
bool oNOff = true;
// Blynk function to adjust temperature threshold
BLYNK_WRITE(V1) {
int value = param.asInt();
if (value == 0) {
tempThreshold = 15.0;
} else if (value == 1) {
tempThreshold = 20.0;
}
}
// Blynk function to control Heat on/off state
BLYNK_WRITE(V0) {
oNOff = param.asInt(); // 1 for ON, 0 for OFF
}
// Setup function
void setup() {
Serial.begin(115200);
pinMode(RelayPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
dht.begin();
WiFi.begin(ssid, pass);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
// Main loop
void loop() {
Blynk.run(); // keeps connection to Blynk
// Read temperature from DHT22
float temp = dht.readTemperature();
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// For Debug can remove on production
// Print temperature, threshold, and Heat state to Serial Monitor
Serial.print("Temp: ");
Serial.print(temp);
Serial.print(", Threshold: ");
Serial.print(tempThreshold);
Serial.print(", OnOff State: ");
Serial.println(oNOff);
// Display temperature on LCD
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(temp);
lcd.print(" ");
lcd.print((char)223); // Degree symbol
lcd.print("C");
// Control relay based on temperature and on/off state and print on LCD
if (oNOff) {
digitalWrite(RelayPin, temp < tempThreshold ? HIGH : LOW);
lcd.setCursor(0,2);
lcd.print(digitalRead(RelayPin) ? "Heat On" : "Heat Off ");
}
}