#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* FIREBASE_HOST = "apptest-5902a-default-rtdb.firebaseio.com";
const char* FIREBASE_AUTH = "nTGibi8QdazgLBdSoSzic1jWyEzv6sjYQOOS0jHK";
const char* databaseURL = "https://apptest-5902a-default-rtdb.firebaseio.com/data.json";
#define DHT_PIN 5 // GPIO pin connected to the DHT22 sensor
#define LED_PIN 25
DHT dht(DHT_PIN, DHT22);
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int buttonLedState = HIGH;
int ledstate = 0;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
dht.begin();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN,LOW);
lcd.init();
lcd.backlight();
}
void loop() {
delay(1000); // Wait for sensor to stabilize
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (!isnan(humidity) && !isnan(temperature)) {
Serial.printf("Humidity: %.2f%%, Temperature: %.2f°C\n", humidity, temperature);
// Hiển thị LCD
lcd.setCursor(0, 0);
lcd.print("Temp : ");
lcd.setCursor(10, 0);
lcd.print(String(temperature,2));
lcd.setCursor(0, 1);
lcd.print("Humidity : ");
lcd.setCursor(10, 1);
lcd.print(String(humidity,1));
// Send data to Firebase
String data = String("{\"temperature\": ") + String(temperature) + String(", \"humidity\": ") + String(humidity)
+ String("}");
HTTPClient http;
http.begin(databaseURL);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int httpResponseCode = http.PUT(data);
if (httpResponseCode > 0) {
Serial.print("Data sent successfully, response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error sending data, response code: ");
Serial.println(httpResponseCode);
}
// Receive data from Firebase
http.begin(String(databaseURL) + "/data/Light.json");
int httpResponse = http.GET();
if (httpResponse > 0) {
String response = http.getString();
if (response == "1") {
// Perform action when light value is 1
// Code to turn on the LED or any other action
digitalWrite(LED_PIN, HIGH);
}
}
http.end();
}
else {
Serial.println("Failed to read from DHT sensor");
}
delay(1000); // Send data every 1 seconds
}