#define BLYNK_TEMPLATE_NAME "IoT"
#define BLYNK_TEMPLATE_ID "TMPL6n9dQ8Jp-"
#define BLYNK_AUTH_TOKEN "rPjBOvhd2gZEF65KT2he9jhhetQZUeZf"
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// DHT22 settings
#define DHTPIN 23 // DHT22 Data pin connected to GPIO 23 on ESP32
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LCD settings
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16x2 display
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin settings
#define PUMP_LED_PIN 13 // LED mô phỏng máy bơm nối với chân GPIO 13
BlynkTimer timer;
// Global variables to store previous sensor values
float lastTemperature = -100.0;
float lastHumidity = -100.0;
bool isPumpOn = false; // Track pump status
void sendSensorData()
{
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if the readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Update Blynk only if values have changed
if (temperature != lastTemperature) {
Blynk.virtualWrite(V0, temperature); // Send temperature to Virtual Pin V0
lastTemperature = temperature;
}
if (humidity != lastHumidity) {
Blynk.virtualWrite(V1, humidity); // Send humidity to Virtual Pin V1
lastHumidity = humidity;
}
// Display only if the value changes
if (temperature != lastTemperature || humidity != lastHumidity) {
// Display temperature and humidity on LCD
lcd.setCursor(6, 0);
lcd.print(" ");
lcd.setCursor(6, 0);
lcd.print(temperature, 1);
lcd.print(" C");
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(humidity, 1);
lcd.print(" %");
}
// Print to Serial Monitor (optional for debugging)
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.println(F("°C"));
// Control the pump based on humidity, using LED to simulate the pump
if (humidity < 50.0 && !isPumpOn) {
digitalWrite(PUMP_LED_PIN, HIGH); // Bật LED mô phỏng máy bơm
isPumpOn = true;
Blynk.virtualWrite(V2, 1); // Gửi giá trị 1 đến Blynk khi máy bơm bật
Serial.println("Máy bơm (LED) đang bật");
} else if (humidity >= 50.0 && isPumpOn) {
digitalWrite(PUMP_LED_PIN, LOW); // Tắt LED mô phỏng máy bơm
isPumpOn = false;
Blynk.virtualWrite(V2, 0); // Gửi giá trị 0 đến Blynk khi máy bơm tắt
Serial.println("Máy bơm (LED) đang tắt");
}
}
void setup()
{
// Initialize serial, sensors, and LCD
Serial.begin(115200);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
// Initialize Wi-Fi and Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Set up pump LED pin (to simulate pump activity)
pinMode(PUMP_LED_PIN, OUTPUT);
digitalWrite(PUMP_LED_PIN, LOW); // Ensure pump (LED) is off at start
// Set timer to send data to Blynk every 2 seconds
timer.setInterval(2000L, sendSensorData);
}
void loop()
{
Blynk.run();
timer.run();
}