#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 LED_PIN 12 // LED connected to GPIO 12
#define BUTTON_PIN 14 // Button connected to GPIO 14
#define PUMP_PIN 13 // Pump connected to GPIO 13 (new declaration)
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
if (humidity < 50.0 && !isPumpOn) {
digitalWrite(PUMP_PIN, HIGH); // Bật máy bơm (HIGH để kích hoạt relay)
isPumpOn = true;
Serial.println("Máy bơm đang bật");
} else if (humidity >= 50.0 && isPumpOn) {
digitalWrite(PUMP_PIN, LOW); // Tắt máy bơm (LOW để tắt relay)
isPumpOn = false;
Serial.println("Máy bơm đ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 LED, button, and pump pins
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(PUMP_PIN, OUTPUT); // Pump pin initialized as output
digitalWrite(PUMP_PIN, LOW); // Ensure pump is off at start
// Set timer to send data to Blynk every 2 seconds
timer.setInterval(2000L, sendSensorData);
}
void loop()
{
Blynk.run();
timer.run();
// LED control based on button state
if (digitalRead(BUTTON_PIN) == LOW) { // Button pressed
digitalWrite(LED_PIN, HIGH); // Turn LED ON
} else { // Button released
digitalWrite(LED_PIN, LOW); // Turn LED OFF
}
}