Table of Contents

Temperature and Humidity Monitor


Project Description


This project is meant to monitor the temperature and humidity for a certain room: the room where the sensor will be placed.

Users can download the mobile app, create an account and set limits for temperature and humidity. When the temperature is lower than the limit, the heating system will start. To simulate this behavior, an RGB LED was used and the red light was turned on. On the other hand, if the temperature is higher than the limit, the air conditioning will start. This behavior was simulated by turning on the blue light. Lastly, if the temperature is between limits, the green light will be turned on.

Also, when air conditioning or heating system is started, a notification will be sent to inform the user regarding the taken action.

Hardware Description


Following hardware components were used in order to achieve the wanted result:

Software Description


The following libraries were necessary:

#include  <WiFi.h>               // to connect ESP32 via WiFi
#include "DHT.h"                 // to read data from DHT22 sensor
#include <Firebase_ESP_Client.h> // to read/write data to Firebase

The Wi-Fi setup and connection:

WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-FI...");
while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  delay(300);
}

The DHT22 setup:

#define DHTPIN 14
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);

dht.begin();

Firebase setup:

config.api_key = API_KEY;
config.database_url = DATABASE_URL;

// Sign up with Firebase
if (Firebase.signUp(&config, &auth, "", "")){
  Serial.println("ok");
  signupOK = true;
}
else{
  Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
config.token_status_callback = tokenStatusCallback;

Firebase.begin(&config,&auth);
Firebase.reconnectWiFi(true);

RGB LED setup (all pins were set to OUTPUT mode):

#define LED_PIN_HS 25
#define LED_PIN_OK 26
#define LED_PIN_AC 33

// used to change LED color
void setLEDColor(int R, int G, int B) {
  analogWrite(LED_PIN_HS,   R);
  analogWrite(LED_PIN_OK, G);
  analogWrite(LED_PIN_AC,  B);
}

The loop() function:

// get data from sensor
float hum = dht.readHumidity();
float temp = dht.readTemperature();

if (Firebase.ready() && signupOK) {
  // write temperature read from sensor to Firebase
  if (!Firebase.RTDB.setFloat(&firebaseData, "IOT_Project/Temperature", temp)){
    Serial.println("Failed to write temperature to Firebase: " + firebaseData.errorReason());
  }
  
  // write humidity read from sensor to Firebase
  if (!Firebase.RTDB.setFloat(&firebaseData, "IOT_Project/Humidity", hum)){
    Serial.println("Failed to write humidity to Firebase: " + firebaseData.errorReason());
  }
}

// read humidity superior limit
if (Firebase.RTDB.getFloat(&firebaseData, "/IOT_Project/Limit_Humidity_Sup")) {
  limitHumSup = firebaseData.floatData();
} else {
  Serial.println("Failed to get Limit_Humidity_Sup from Firebase: " + firebaseData.errorReason());
}

// read humidity inferior limit
if (Firebase.RTDB.getFloat(&firebaseData, "/IOT_Project/Limit_Humidity_Inf")) {
    limitHumInf = firebaseData.floatData();
} else {
  Serial.println("Failed to get Limit_Humidity_Inf from Firebase: " + firebaseData.errorReason());
}

// read temperature superior limit
if (Firebase.RTDB.getFloat(&firebaseData, "/IOT_Project/Limit_Temperature_Sup")) {
    limitTempSup = firebaseData.floatData();
} else {
  Serial.println("Failed to get Limit_Temperature_Sup from Firebase: " + firebaseData.errorReason());
}

// read temperature inferior limit
if (Firebase.RTDB.getFloat(&firebaseData, "/IOT_Project/Limit_Temperature_Inf")) {
    limitTempInf = firebaseData.floatData();
} else {
  Serial.println("Failed to get Limit_Temperature_Inf from Firebase: " + firebaseData.errorReason());
}

// check humidity related to limits
if (hum > limitHumSup || hum < limitHumInf) {
  Serial.println("Humidity is not between limits");
}

// check temperature related to limits
if (temp > limitTempSup && !isACOpen) {
  isACOpen = true;
  setLEDColor(0, 0, 200);
  Serial.println("Temperature is higher than limit - AC started");
} else if (temp < limitTempInf && !isHSOpen) {
  isHSOpen = true;
  setLEDColor(200, 0, 0);
  Serial.println("Temperature is lower than limit - heating system started");
} else if (temp >= limitTempInf && temp <= limitTempSup) {
  isACOpen = false;
  isHSOpen = false;
  setLEDColor(0, 200, 0);
}

As said before, the data read from sensor is saved in Firebase and is updated in real time in the mobile app:

Mobile App


This is the mobile application interface:

References

https://www.emag.ro/placa-dezvoltare-esp-wroom-32-esp-32s-cl398/pd/D3NF9VBBM/?X-Search-Id=903d7a3aa9589563010c&X-Product-Id=44059384&X-Search-Page=1&X-Search-Position=0&X-Section=search&X-MB=0&X-Search-Action=view

https://www.emag.ro/senzor-de-temperatura-si-umiditate-am2302-dht22-ai142-s271/pd/DXSFYMMBM/

https://randomnerdtutorials.com/esp32-firebase-realtime-database/

https://randomnerdtutorials.com/esp32-esp8266-firebase-bme280-rtdb/

https://developer.android.com/codelabs/build-your-first-android-app#0

https://developer.android.com/develop/ui/views/notifications/build-notification