#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#include <HTTPClient.h>
#include <DHT.h>
//BEDROOM, có nhiệt độ độ ẩm, nhiệt độ máy lạnh = nhiệt độ phòng
#define DHTBEDROOM_PIN 2
DHT dhtbedroom(DHTBEDROOM_PIN, DHT22);
//Garden, có nhiệt độ độ ẩm, pH= cảm biến ánh sáng (fake)
#define DHTGARDEN_PIN 15
DHT dhtgarden(DHTGARDEN_PIN, DHT22);
#define LDRGARDEN_PIN 35
int ldrgardenvalue;
//Kitchen, có gas, nhiệt độ = nhiệt độ garden (fake), khói = độ ẩm garden (fake)
#define DHTKITCHEN_PIN 4
DHT dhtkitchen(DHTKITCHEN_PIN, DHT22);
#define MQ2_PIN 34
int value;
//Livingroom, có nhiệt độ độ ẩm, độ bụi = cảm biến ánh sáng (fake)
#define DHTLIVINGROOM_PIN 14
DHT dhtlivingroom(DHTLIVINGROOM_PIN, DHT22);
#define LDRLIVINGROOM_PIN 26
int ldrlivingroomvalue;
//Firebase
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";
const char* databaseURLLivingroom="https://webiotnew-default-rtdb.firebaseio.com/livingroom.json";
//Thingspeak
unsigned long myChannelNumber = 2516347;
const char *myWriteAPIKey = "MAX8N1DS8BVMYG30";
int statusCode;
//Wifi
const char* ssid= "Wokwi-GUEST";
const char* password= "";
WiFiClient client;
void setup() {
Serial.begin(115200);
ThingSpeak.begin(client);
WiFi.begin(ssid, password);
pinMode(MQ2_PIN, INPUT);
pinMode(LDRGARDEN_PIN, INPUT);
dhtbedroom.begin();
dhtgarden.begin();
dhtkitchen.begin();
dhtlivingroom.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.println("do am bedroom: " + String(doambedroom) + " , nhiet do bedroom:"+ String(nhietDobedroom) + " , nhiet do may lanh:"+ String(nhietDobedroom));
// send data to firebase = protocol HTTP
String databedroom= String("{\"nhietdo_bedroom\":") + String(nhietDobedroom) + String(",\"doam_bedroom\":") + String(doambedroom)+ String(",\"maylanh_bedroom\":") + String(nhietDobedroom) + 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
ldrgardenvalue = analogRead(LDRGARDEN_PIN);
ldrgardenvalue = map(ldrgardenvalue, 0, 4095, 0, 100);
float ldrgarden = ldrgardenvalue;
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.println("do am garden: " + String(doamgarden) + " , nhiet do garden:"+ String(nhietDogarden) + " , pH:"+ String(ldrgardenvalue));
// send data to firebase = protocol HTTP
String datagarden= String("{\"nhietdo_garden\":") + String(nhietDogarden) + String(",\"doamdat\":") + String(doamgarden) + String(",\"pH\":") + String(ldrgarden) + 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;
float doamkitchen = dhtkitchen.readHumidity();
float nhietDokitchen = dhtkitchen.readTemperature();
Serial.println("Gas:" + String(gaskitchen) + " , khoi: "+ String(doamgarden) + " , nhietdo_kitchen: " + String(nhietDogarden)+" oC");
String datakitchen= String("{\"khiga\":") + String(gaskitchen) + String(",\"khoi\":") + String(doamkitchen) + String(",\"nhietdo_kitchen\":") + String(nhietDokitchen)+ 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);
//Livingroom
ldrlivingroomvalue = analogRead(LDRLIVINGROOM_PIN);
ldrlivingroomvalue = map(ldrlivingroomvalue, 0, 4095, 0, 100);
float ldrlivingroom = ldrlivingroomvalue;
float nhietdomaylanh = dhtlivingroom.readHumidity();
float nhietDolivingroom = dhtlivingroom.readTemperature();
// kiem tra du lieu xem co doc thanh cong kh( nan = error)
// thanh cong dong goi sang goi chuoi JSON
Serial.println("nhiet do phong khach: " + String(nhietDolivingroom) + " , nhiet do may lanh:"+ String(nhietdomaylanh) + " , do bui:"+ String(ldrlivingroomvalue));
// send data to firebase = protocol HTTP
String datalivingroom= String("{\"nhietdo_livingroom\":") + String(nhietDolivingroom) + String(",\"maylanh_livingroom\":") + String(nhietdomaylanh)+ String(",\"dobui\":") + String(ldrlivingroom) + String("}");
http.begin(databaseURLLivingroom); //bedroom
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", FIREBASE_AUTH);
int databaseURLLivingroom = http.PUT(datalivingroom); //databedroom
if(databaseURLLivingroom > 0){
Serial.print("Data sent successfully, respon code: ");
Serial.println(databaseURLLivingroom);
}
else{
Serial.print("error sending data, response code: ");
Serial.println(databaseURLLivingroom);
}
http.end();
delay(30);
if(databaseURLLivingroom == 200){
String response = http.getString();
float nhietDoFirebase = response.toFloat();
}
delay(30);
//Thingspeak
ThingSpeak.setField(1, nhietDobedroom);
ThingSpeak.setField(2, doambedroom);
ThingSpeak.setField(3, nhietDokitchen);
ThingSpeak.setField(4, gaskitchen);
ThingSpeak.setField(5, nhietDolivingroom);
ThingSpeak.setField(6, ldrlivingroom);
ThingSpeak.setField(7, doamgarden);
ThingSpeak.setField(8, ldrgarden);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}