Untitled
#include "FirebaseESP8266.h" #include <ESP8266WiFi.h> #include "DHT.h" #define ALEART 14 // D5 #define LIGHT 4 // D2 #define DHTPIN 4 // D1 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); #define DATABASE_URL "btl-iot-dbcbe-default-rtdb.firebaseio.com/" #define DATABASE_SECRET "EjWF18kuUqw5drrjZG2J9o0sXG2zfTRL4LWiLySV" #define WIFI_SSID "POCOF3" #define WIFI_PASSWORD "123456789" #define USER_EMAIL "sankatana02@gmail.com" #define USER_PASSWORD "123456" #define API_KEY "AIzaSyBRFPHNOpAhLfVV105d7OVjSS-3txBsBAk" FirebaseData firebaseData; // Define the FirebaseConfig data for config data FirebaseConfig firebaseConfig; // Define the FirebaseAuth data for authentication data FirebaseAuth auth; float temperature, lastUpdateTemperature = 0; float temperatureAleart = -1; int gasRate, lastUpdateGasRate = 0; int gasRateAleart = -1; bool isAleart = false; bool isNoti = true; bool isSendLocale = true; bool isGasLeak = false; bool isFire = false; unsigned long timer = millis(); // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMillis = 0; // will store last time DHT was updated // Updates DHT readings every 10 seconds const long interval = 2000; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1000); } Serial.println(); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println(); firebaseConfig.signer.test_mode = true; firebaseConfig.api_key = API_KEY; firebaseConfig.database_url = DATABASE_URL; auth.user.email = USER_EMAIL; auth.user.password = USER_PASSWORD; Firebase.reconnectWiFi(true); Firebase.reconnectNetwork(true); firebaseData.setBSSLBufferSize(1024, 1024); firebaseData.setResponseSize(1024); Firebase.setReadTimeout(firebaseData, 1000 * 60); Firebase.setwriteSizeLimit(firebaseData, "tiny"); Firebase.begin(&firebaseConfig, &auth); Serial.println("Khởi động cảm biến nhiệt độ"); dht.begin(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you updated the DHT values previousMillis = currentMillis; updateCondition(); updateTemperature(); updateGasRate(); } } void updateCondition() { // get value from firebase if (Firebase.getBool(firebaseData, F("/Arduino/isNoti"))) { bool isNotiFB = firebaseData.boolData(); if (isNotiFB != isNoti) { isNoti = isNotiFB; } Serial.print("Get isNoti from Fb: "); Serial.println(isNotiFB); } if (Firebase.getBool(firebaseData, F("/Arduino/isSendLocale"))) { bool isSendLocalFB = firebaseData.boolData(); if (isSendLocalFB != isSendLocale) { isSendLocale = isSendLocalFB; } Serial.print("Get isSendLocale from Fb: "); Serial.println(isSendLocale); } if (Firebase.getFloat(firebaseData, F("/Arduino/temperatureAleart"))) { temperatureAleart = firebaseData.floatData(); } if (Firebase.getInt(firebaseData, F("/Arduino/gasRateAleart"))) { gasRateAleart = firebaseData.floatData(); } } void updateTemperature() { // Read temperature as Celsius (the default) lastUpdateTemperature = dht.readTemperature(false); // Read temperature as Fahrenheit (isFahrenheit = true) //float newT = dht.readTemperature(true); // if temperature read failed, don't change t value if (isnan(lastUpdateTemperature)) { Serial.println("Failed to read from DHT sensor!"); } else { Serial.println("Nhiệt độ: " + String(lastUpdateTemperature)); if (shouldUpdateTemperatureData(temperature, lastUpdateTemperature)) { temperature = lastUpdateTemperature; setDataTemperature(temperature); } handleAleartFire(isNoti, isSendLocale, temperature); } } void updateGasRate() { //read gas rate lastUpdateGasRate = analogRead(A0); lastUpdateGasRate = map(gasRate, 0, 1023, 100, 0); Serial.println("Gas: " + String(gasRate)); if (shouldUpdateGasRateData(gasRate, lastUpdateGasRate)) { gasRate = lastUpdateGasRate; setDataGasRate(gasRate); } handleAleartGasLeak(isNoti, isSendLocale, gasRate); } void handleAleartFire(bool isNoti, bool isSendLocale, int temperature) { if (Firebase.getFloat(firebaseData, F("/Arduino/temperatureAleart"))) { temperatureAleart = firebaseData.floatData(); } if (shouldAleartFire(temperature, temperatureAleart)) { isFire = true; if (isNoti) { } else { } if (isSendLocale) { } else { } } else { isFire = false; } //update fire state to db Firebase.setBool(firebaseData, "/Arduino/isFire", isFire); } void handleAleartGasLeak(bool isNoti, bool isSendLocale, int gasRate) { if (Firebase.getInt(firebaseData, F("/Arduino/gasRateAleart"))) { gasRateAleart = firebaseData.intData(); } if (shouldAleartGasLeak(gasRate, gasRateAleart)) { isGasLeak = true; if (isNoti) { } else { } if (isSendLocale) { } else { } } else { isGasLeak = false; } //update gas leak state to db Firebase.setBool(firebaseData, "/Arduino/isGasLeak", isGasLeak); } void setDataTemperature(float temperature) { if (Firebase.setFloat(firebaseData, "/data/temperature", temperature)) { Serial.println("PASSED T"); Serial.print("VALUE: "); Serial.print(firebaseData.floatData()); Serial.println("------------------------------------"); Serial.println(); } else { Serial.println("FAILED"); Serial.println("REASON: " + firebaseData.errorReason()); Serial.println("------------------------------------"); Serial.println(); } } void setDataGasRate(int gasRate) { if (Firebase.setInt(firebaseData, "/data/gasRate", gasRate)) { Serial.println("PASSED Gas Rate"); Serial.print("VALUE: "); Serial.print(firebaseData.intData()); Serial.println("------------------------------------"); Serial.println(); } else { Serial.println("FAILED"); Serial.println("REASON: " + firebaseData.errorReason()); Serial.println("------------------------------------"); Serial.println(); } } bool shouldUpdateTemperatureData(float temperature, float lastUpdateTemperature) { return abs(temperature - lastUpdateTemperature) >= 0.5; } bool shouldUpdateGasRateData(int gasRate, int lastUpdateGasRate) { return abs(gasRate - lastUpdateGasRate) >= 1; } bool shouldAleartGasLeak(int gasRate, int gasRateAleart) { return gasRate >= gasRateAleart; } bool shouldAleartFire(float temperature, float temperatureAleart) { return temperature >= temperatureAleart; }
Leave a Comment