#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include "DHTesp.h"
#include <WiFi.h>
#include "ThingSpeak.h"
#define DHTPIN 2 // Chân kết nối với cảm biến DHT22
#define DHTTYPE DHT22 // Loại cảm biến DHT
DHT dht(DHTPIN, DHTTYPE);
// Địa chỉ I2C của màn hình LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin = 25; // ESP32 connected to button's pin
const int LEDPin = 26; // ESP32 pin connected to LED's pin
const int relay = 5; // Pin for the relay
// WiFi credentials
const char* name = "Wokwi-GUEST"; // Thay bằng tên WiFi của bạn
const char* password = ""; // Thay bằng mật khẩu WiFi của bạn
// ThingSpeak details
unsigned long myChannelNumber = 2599113; // Thay bằng Channel ID của bạn
const char* myApiKey = "2V6XAV3CW95WEDJC"; // Thay bằng API key của bạn
WiFiClient client;
DHTesp dhtSensor;
// variables will change:
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(115200); // Choose your desired baud rate
lcd.init();
lcd.clear();
lcd.backlight();
dht.begin();
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LEDPin, OUTPUT);
pinMode(relay, OUTPUT);
currentButtonState = digitalRead(buttonPin);
// Connect to WiFi
WiFi.begin(name, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("WiFi not connected");
}
Serial.println("WiFi connected!");
Serial.println("Local IP: " + String(WiFi.localIP()));
// Initialize ThingSpeak library
WiFi.mode(WIFI_STA); // Set WiFi mode to station (client)
ThingSpeak.begin(client);
}
void loop() {
// Read temperature and humidity
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Control relay based on temperature threshold
if (data.temperature > 50) {
digitalWrite(relay, LOW);
} else {
digitalWrite(relay, HIGH);
}
// Check if reading is valid
if (isnan(data.humidity) || isnan(data.temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Set ThingSpeak fields
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
// Control LED based on thresholds
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40) {
digitalWrite(LEDPin, HIGH);
} else {
digitalWrite(LEDPin, LOW);
}
// Send data to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print readings and success/error messages to serial monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
// Display readings on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(data.temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(data.humidity);
lcd.print(" %");
// Delay for some time (adjust as needed)
delay(5000); // Example: Delay for 5 seconds
// Read button state and toggle LED
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(buttonPin); // read new state
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
ledState = !ledState;
// control LED according to the toggled state
digitalWrite(LEDPin, ledState);
}
}