#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* FIREBASE_HOST = "iot-2024-b0528-default-rtdb.firebaseio.com";
const char* FIREBASE_AUTH = "d1PQjvKaWDYWxSW6I2azIuU20my1";
#define DHT_PIN 15
#define LAMP_PIN 27
#define quat_PIN 26
#define dieuhoa_PIN 25
DHT dht(DHT_PIN, DHT22);
bool lampStatus = false; // Initialize lamp status as false (OFF)
// Function to send HTTP GET request and return response code
int getDataFromFirebase(const char* databaseURL, int& ktrad, int& ktradh, int& ktraq) {
HTTPClient http;
http.begin(databaseURL);
http.addHeader("Authorization", FIREBASE_AUTH);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
int den = doc["Den"];
int quat = doc["quat"];
int dieuhoa = doc["dieu hoa"];
ktrad = den;
ktraq = quat;
ktradh = dieuhoa;
} else {
Serial.print("Error getting data from Firebase, HTTP error code: ");
Serial.println(httpCode);
}
http.end();
return httpCode;
}
void readData(void * parameter) {
int ktrad1, ktraq1, ktradh1;
while (true) {
// Read data from Firebase
Serial.println("Reading data");
int httpGetData1 = getDataFromFirebase("https://iot-2024-b0528-default-rtdb.firebaseio.com/thietbi/livingroom.json", ktrad1, ktraq1, ktradh1);
if (ktrad1 == 1) {
digitalWrite(LAMP_PIN, HIGH);
lampStatus = true;
} else {
digitalWrite(LAMP_PIN, LOW);
lampStatus = false;
}
if (ktraq1 == 1) {
digitalWrite(quat_PIN, HIGH);
} else {
digitalWrite(quat_PIN, LOW);
}
if (ktradh1 == 1) {
digitalWrite(dieuhoa_PIN, HIGH);
} else {
digitalWrite(dieuhoa_PIN, LOW);
}
vTaskDelay( 1000/ portTICK_PERIOD_MS); // Delay 1 second
}
}
void sendData(void * parameter) {
while (true) {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Serial.printf("Humidity: %.2f%%, Temperature: %.2fC\n", humidity, temperature);
// Send data to Firebase
String livingroomData = String("{\"temperature\":") + String(temperature) + String(",\"humidity\":") + String(humidity)+ String("}");
String bedroomData = String("{\"temperature\":") + String(temperature) + String(",\"humidity\":") + String(humidity) +String("}") ;
String kitchenData = String("{\"temperature\":") + String(temperature) + String(",\"humidity\":") + String(humidity) + String("}");
String workroomData = String("{\"temperature\":") + String(temperature) + String(",\"humidity\":") + String(humidity) +String("}");
Serial.println("Waiting to send data");
HTTPClient http;
http.begin("https://iot-2024-b0528-default-rtdb.firebaseio.com/livingroom.json");
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int httpResponseCode = http.PUT(livingroomData);
http.begin("https://iot-2024-b0528-default-rtdb.firebaseio.com/bedroom.json");
int httpResponseCode1 = http.PUT(bedroomData);
http.begin("https://iot-2024-b0528-default-rtdb.firebaseio.com/kitchenroom.json");
int httpResponseCode2 = http.PUT(kitchenData);
http.begin("https://iot-2024-b0528-default-rtdb.firebaseio.com/workroom.json");
int httpResponseCode3 = http.PUT(workroomData);
http.end();
Serial.println("Turn the LAMP");
vTaskDelay(100 / portTICK_PERIOD_MS); // Delay 1 seconds
}
}
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
Serial.println("Connecting to wifi.........");
delay(1000);
}
Serial.println("Connected to wifi");
dht.begin();
pinMode(LAMP_PIN, OUTPUT);
pinMode(quat_PIN, OUTPUT);
pinMode(dieuhoa_PIN, OUTPUT);
xTaskCreatePinnedToCore(
readData, // Function to implement the task
"readData", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
1, // Priority of the task
NULL, // Task handle.
1 // Core where the task should run
);
xTaskCreatePinnedToCore(
sendData, // Function to implement the task
"sendData", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
1, // Priority of the task
NULL, // Task handle.
1 // Core where the task should run
);
}
void loop() {
// Empty, everything is handled in tasks
}