NodeMCU ESP8266 with DHT11 and Firebase Real-Time Database

Today we will be building similar project where we’ll use a temperature & humidity sensor DHT11 and a NodeMCU ESP8266 Module to log the temperature and humidity in real time on Google’s Firebase database server.

MCU (Microcontrollers) have not enough memory to store sensors generated data for long time, either you have to use some external memory device or can save the data on some cloud using internet. The real time databases can be used in this scenario where we just have to interface some controller which can be connected to internet and can be able to exchange data with cloud server. The server data can be useful in monitoring real time system behavior, database analytics, statistical analysis and processing, and interpretation for future use case.


What is a Time Series Database?

A Time Series Database (TSDB) is a database optimized for time-stamped or time series data. Time series data are simply measurements or events that are tracked, monitored, downsampled, and aggregated over time. This could be server metrics, application performance monitoring, network data, sensor data, events, clicks, trades in a market, and many other types of analytics data.

=> https://iotbyhvm.ooo/influxdb-time-series-datbase/


Previously we already covered some tutorials related DHT11 and NodeMCU.

Recommended:

DHT11 sensor with ESP8266/NodeMCU using Arduino IDEESP32 BLE with DHT11

Requirements

  1. NodeMCU ESP8266 Module
  2. DHT11 Temperature and Humidity sensor

Circuit Diagram

NodeMCU                     DHT11

3.3V                                VCC

GND                               GND

D4                                DATA

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.

Read more about DHT 11 Sensors :  Visit this: DHT11 vs DHT22: Overview

NodeMCU ESP8266 with DHT11 and Firebase Real-Time Database

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.

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 "yuejx9ROxxxxxxxxxxxxxxxxfQDgfdhN" // the secret key generated from firebase

You can see all data in your firebase account. Just go to “Database” section in “Your Project” at “My console” In Firebase.

Arduino IDE Sketch

#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

#define WIFI_SSID "network_name"                  // wifi name 
#define WIFI_PASSWORD "password"                 //password of wifi 
 
#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);    
  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 
  float t = dht.readTemperature();   // Read temperature as Celsius (the default)
    
  if (isnan(h) || isnan(t)) {  // Check if any reads failed and exit early (to try again).
    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 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
   
}

I hope you like this post”NodeMCU ESP8266 with DHT11 and Firebase Real-Time Database”. Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

We have other tutorials with ESP32 that you may find useful:

You may like also:

You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm

Leave a Reply

%d bloggers like this: