Components Required
- NodeMCU ESP8266 Module
- DHT11 Temperature and Humidity sensor
Circuit Diagram
DHT11 Temperature and Humidity Sensor
DHT11 module features a humidity and temperature complex with a calibrated digital signal output means DHT11 sensor module is a combined module for sensing humidity and temperature which gives a calibrated digital output signal. DHT11 gives us very precise value of humidity and temperature and ensures high reliability and long term stability. This sensor has a resistive type humidity measurement component and NTC type temperature measurement component with an 8-bit microcontroller inbuilt which has a fast response and cost effective and available in 4-pin single row package.Firstly include the libraries for using ESP8266 and firebase.
#include <ESP8266WiFi.h
#include <FirebaseArduino.h
Download and install the libraries by following the below links:
https://github.com/FirebaseExtended/firebase-arduino/blob/master/src/Firebase.h
https://github.com/bblanchon/ArduinoJson
While compiling, if you get error that ArduinoJson.h library is not installed then please install it using link given above.
We will program NodeMCU to take readings from DHT11 sensor and push it to Firebase every 5 seconds of interval. We will set a path for pushing data. Right now two parameters viz. humidity and temperature are sent in same parent path and different child path.
These two parameters are very important to communicate with firebase. Setting these parameters will enable the data exchange between and ESP8266 and firebase.
#define FIREBASE_HOST "your-project.firebaseio.com" // the project name address from firebase id
#define FIREBASE_AUTH "Uejx9ROxxxxxxxxxxxxxxxxxxxxxxxxfQDDkhN" // the secret key generated from firebase
After successfully finding the credentials, just replace in the above code.
Enter your Wi-Fi SSID and Password to connect with your network.
After successfully finding the credentials, just replace in the above code.
Enter your Wi-Fi SSID and Password to connect with your network.
#define WIFI_SSID "network_name" // input your home or public wifi name
#define WIFI_PASSWORD "password" //password of wifi ssid
Define the DHT data pin in NodeMCU. You can use any Digital GPIO pin in NodeMCU
.#define DHTPIN D4
The DHT library is made for all DHT variants and comes with option that which DHT sensor you want to use for e.g. DHT11 or DHT22. Just choose the right DHT sensor and proceed
The DHT library is made for all DHT variants and comes with option that which DHT sensor you want to use for e.g. DHT11 or DHT22. Just choose the right DHT sensor and proceed
.#define DHTTYPE DHT11 // select dht type as DHT 11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
Connect to the Wi-Fi network selected and also connect to the firebase database server.
Connect to the Wi-Fi network selected and also connect to the firebase database server.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Start taking reading at pin D4 of NodeMCU.
Start taking reading at pin D4 of NodeMCU.
dht.begin();
Take humidity and temperature readings from DHT sensor and save as float value.
Take humidity and temperature readings from DHT sensor and save as float value.
float h = dht.readHumidity(); // Reading temperature or humidity takes about 250 milliseconds!
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
Just check if the DHT sensor is properly wired or it is not damaged and controller can able to read readings from it. If the readings are not showing then probably the sensor is damaged, just show an error message and return to check again without proceeding ahead.
Print sensor data in serial monitor for debugging and save the values of temperature and humidity in string form to send this to the firebase. Also note that the minimum delay required between two readings from DHT11 sensor is 2 seconds, so always use delay greater than 2 seconds. To know more about the DHT11 you can look into official datasheet.
Serial.print("Humidity: ");
Serial.print(h);
String fireHumid = String(h) + String("%"); //convert integer humidity to string humidity
Serial.print("% Temperature: ");
Serial.print(t); Serial.println("°C ");
String fireTemp = String(t) + String("°C");
delay(4000); At last, send the temperature and humidity data to firebase at path “your-project.firebaseio.com/DHT11/Humidity/”
. Firebase.pushString("/DHT11/Humidity", fireHumid); //setup path and send readings
Firebase.pushString("/DHT11/Temperature", fireTemp); //setup path and send readings
You can see all data in your firebase account. Just go to “Database” section in “Your Project” at “My console” In Firebase.
Complete code and Video for this IoT based temperature and Humidity monitoring is given below.
You can see all data in your firebase account. Just go to “Database” section in “Your Project” at “My console” In Firebase.
Complete code and Video for this IoT based temperature and Humidity monitoring is given below.
Code
#include <ESP8266WiFi.h> // esp8266 library
#include <FirebaseArduino.h> // firebase library
#include <DHT.h> // dht11 temperature and humidity sensor library
#define FIREBASE_HOST "your -project.firebaseio.com" // the project name address from firebase id
#define FIREBASE_AUTH "Uejx9ROxxxxxxxxxxxxxxxxxxxxxxxxfQDDkhN" // the secret key generated from firebase
#defineWIFI_SSID "network_name" // input your home or public wifi name
#define WIFI_PASSWORD "password" // password of wifi ssid
#define DHTPIN D4 // what digital pin we're connected to
#define DHTTYPE DHT11 // select dht type as DHT 11 or DHT22
DHT dht (DHTPIN, DHTTYPE);
void setup () {
Serial.begin (9600);
delay (1000);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD); // try to connect with wifi
Serial.print ("Connecting to");
Serial.print (WIFI_SSID);
while (WiFi.status ()! = WL_CONNECTED) {
Serial.print (".");
delay (500);
}
Serial.println ();
Serial.print ("Connected to");
Serial.println (WIFI_SSID);
Serial.print ("IP Address is:");
Serial.println (WiFi.localIP ()); // print local IP address
Firebase.begin (FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
dht.begin (); // Start reading dht sensor
}
void loop () {
float h = dht.readHumidity (); // Reading temperature or humidity takes about 250 milliseconds!
float t = dht.readTemperature (); // Read temperature as Celsius (the default)
if (isnan (h) || isnan (t)) {// Check if any reads failed and exit early
Serial.println (F ("Failed to read from DHT sensor!");
return;
}
Serial.print ("Humidity:"); Serial.print (h);
String fireHumid = String (h) + String ("%"); // convert integer humidity to string humidity
Serial.print ("% Temperature:"); Serial.print (t); Serial.println ("Â ° C");
String fireTemp = String (t) + String ("Â ° C"); // convert to integer temperature to string temperature
delay (4000);
Firebase.pushString ("/ DHT11 / Humidity", fireHumid); // setup path and send readings
Firebase.pushString ("/ DHT11 / Temperature", fireTemp); // setup path and send readings
}
No comments