#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#include <HTTPClient.h>
#include <DHT.h>
//BEDROOM
#define DHTBEDROOM_PIN 2
DHT dhtbedroom(DHTBEDROOM_PIN, DHT22);
//Garden
#define DHTGARDEN_PIN 15
DHT dhtgarden(DHTGARDEN_PIN, DHT22);
//Kitchen
#define MQ2_PIN 34
int value;
//Firebase
const char* ssid= "Wokwi-GUEST";
const char* password= "";
const char* FIREBASE_HOST ="webiotnew-default-rtdb.firebaseio.com";
const char* FIREBASE_AUTH="AIzaSyBvyeBP1QVYGbiOrdrFD9QmBLiUNzVgpmM";
const char* databaseURLBedroom="https://webiotnew-default-rtdb.firebaseio.com/bedroom.json";
const char* databaseURLGarden="https://webiotnew-default-rtdb.firebaseio.com/garden.json";
const char* databaseURLKitchen="https://webiotnew-default-rtdb.firebaseio.com/kitchen.json";
WiFiClient client;
unsigned long myChannelNumber = 2516347;
const char *myWriteAPIKey = "MAX8N1DS8BVMYG30";
int statusCode;
void setup() {
Serial.begin(115200);
ThingSpeak.begin(client);
WiFi.begin(ssid, password);
pinMode(MQ2_PIN, INPUT);
dhtbedroom.begin();
dhtgarden.begin();
}
void loop() {
//Connect to wifi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect...");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
Serial.print(".");
delay(5000);
}
}
//Bedroom
float doambedroom = dhtbedroom.readHumidity();
float nhietDobedroom = dhtbedroom.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 bedroom: %.2f%% , nhiet do bedroom: %.2fC\n", doambedroom, nhietDobedroom);
// send data to firebase = protocol HTTP
String databedroom= String("{\"nhietdo_bedroom\":") + String(nhietDobedroom) + String(",\"doam_bedroom\":") + String(doambedroom) + String("}");
HTTPClient http;
http.begin(databaseURLBedroom); //bedroom
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int httpResponseCodeBedroom = http.PUT(databedroom); //databedroom
if(httpResponseCodeBedroom > 0){
Serial.print("Data sent successfully, respon code: ");
Serial.println(httpResponseCodeBedroom);
}
else{
Serial.print("error sending data, response code: ");
Serial.println(httpResponseCodeBedroom);
}
http.end();
delay(30);
if(httpResponseCodeBedroom == 200){
String response = http.getString();
float nhietDoFirebase = response.toFloat();
}
delay(30);
//Garden
// bien doc du lieu garden
float doamgarden = dhtgarden.readHumidity();
float nhietDogarden = dhtgarden.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 garden: %.2f%% , nhiet do garden: %.2fC\n", doamgarden, nhietDogarden);
// send data to firebase = protocol HTTP
String datagarden= String("{\"nhietdo_garden\":") + String(nhietDogarden) + String(",\"doamdat\":") + String(doamgarden) + String("}");
http.begin(databaseURLGarden); //garden
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int httpResponseCodeGarden = http.PUT(datagarden);
if(httpResponseCodeGarden > 0){
Serial.print("Data sent successfully, respon code: ");
Serial.println(httpResponseCodeGarden);
}
else{
Serial.print("error sending data, response code: ");
Serial.println(httpResponseCodeGarden);
}
http.end();
delay(30);
if(httpResponseCodeGarden == 200){
String response = http.getString();
float nhietDoFirebase = response.toFloat();
}
delay(30);
//Kitchen
value = analogRead(MQ2_PIN);
value = map(value, 0, 4095, 0, 100);
float gaskitchen = value;
Serial.println("Gaskitchen:" + String(gaskitchen) + " , khoi"+ String(doamgarden) + " , nhietdo_kitchen" + String(nhietDogarden)+" oC");
String datakitchen= String("{\"khiga\":") + String(gaskitchen) + String(",\"khoi\":") + String(doamgarden) + String(",\"nhietdo_kitchen\":") + String(nhietDogarden)+ String("}");
http.begin(databaseURLKitchen); //garden
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int databaseURLKitchen = http.PUT(datakitchen);
if(databaseURLKitchen > 0){
Serial.print("Data sent successfully, respon code: ");
Serial.println(databaseURLKitchen);
}
else{
Serial.print("error sending data, response code: ");
Serial.println(databaseURLKitchen);
}
http.end();
delay(30);
if(databaseURLKitchen == 200){
String response = http.getString();
float nhietDoFirebase = response.toFloat();
}
delay(30);
//Thingspeak
ThingSpeak.setField(5, nhietDobedroom);
ThingSpeak.setField(6, nhietDogarden);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
//2 dht, 1 gas, 3 phòng link thingspeak, firebase oke