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.


Previously we already covered some tutorials related DHT11 and NodeMCU.


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


  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:

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 ""  // 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.

Setting Up Firebase Console

If you are using Firebase first time then follow these steps.

  1. login you gmail ID (Google account) or sign up.

  2. Open your browser and go to “”

  3. At the right top corner go to “Go to Console”

  4. Click on “Add project”

  5. Insert your Project Name. and click create project.

  6. Accept the terms and condition, Create project and click on “Continue”

You have successfully created your project. Look for the Host Name and Authorization Key also known as Secret Key. For this, follow steps given below:

  1. Go to Settings Icon(Gear Icon) and click on “Project Settings”

  2. Now click on “Service Accounts”

  3. You can see two options “Firebase admin SDK” and “Database Secrets”

  4. Click on “Database Secrets”

  5. Scroll on your project name and ”Show” option appears at right side of your project

  6. Click on “Show” and now you can see secret key created for your project

  7. Copy the secret key and save it to notepad. This is your “FIREBASE_AUTH” string which we have written in Arduino program above.

  8. Now go to “Database” on left control bar and click on it

  9. Scroll down and click on “Create Database”

  10. Choose “Start in test mode” and click on “Enable”

  11. Now your database is created

  12. Now just above the database you  can see


  1. Just copy “” without any slash and https and save it again to notepad just you had saved for secret key

  2. This is your “FIREBASE_HOST” string which we have written in Arduino program

  3. Now You have  “FIREBASE_HOST” and “FIREBASE_AUTH”  both for Arduino sketch.

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 ""  // 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.print("Connecting to ");
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("Connected to ");
  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!"));
  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
  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. If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee!

Leave a Reply

%d bloggers like this: