#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* FIREBASE_HOST ="ttiot-49b50.firebaseapp.com/";
const char* FIREBASE_AUTH="AIzaSyBYzo44VbIKWQwLpfOGU-r_JyOY2BnJbFc";
const char* databaseURL="https://ttiot-49b50-default-rtdb.firebaseio.com/TT_IoT.json";
#define DHT_PIN 5
DHT dht(DHT_PIN, DHT22);
void setup(){
// mở cổng giao tiếp serial để mở thông báo bên màn hình
Serial.begin(9600);
// kết nối wifi
WiFi.begin(ssid, password);
// kiểm tra wifi
while(WiFi.status() != WL_CONNECTED){
Serial.println("Connecting to wifi.........");
}
Serial.println("Connected to wifi");
// bat dau cho cam bien doc
dht.begin();
}
void loop(){
int but = digitalRead(25);
// bien doc du lieu
float h = dht.readHumidity();
float t = dht.readTemperature();
// kiem tra du lieu xem co doc thanh cong kh( nan = error)
// thanh cong dong goi sang goi chuoi JSON
Serial.printf("do am: %.2f%% , nhiet do: %.2fC\n", h, t);
// send data to firebase = protocol HTTP
String TT_IoT= String("{\"Temp\":") + String(t) + String(",\"Hum\":") + String(h) + String("}");
HTTPClient http;
http.begin(databaseURL);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int httpResponseCode = http.PUT(TT_IoT);
if(httpResponseCode > 0){
Serial.print("Data sent successfully, respon code: ");
Serial.println(httpResponseCode);
}
else{
Serial.print("error sending data, response code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(30);
if(httpResponseCode == 200){
String response = http.getString();
float nhietDoFirebase = response.toFloat();
}
delay(30);
}