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.

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 “firebase.google.com”

  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

“https://your_project_name.firebaseio.com/”

  1. Just copy “your_project_name.firebaseio.com” 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 "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

8 thoughts on “NodeMCU ESP8266 with DHT11 and Firebase Real-Time Database

  • 25th July 2019 at 9:59 pm
    Permalink

    Hi Sir. Do you know how I can send serial data from ESP32 to the firebase real-time database.?

    Reply
  • 26th July 2019 at 1:07 am
    Permalink

    The data isn’t being uploaded to Fire base, any suggestions?

    Reply
    • 26th July 2019 at 12:32 pm
      Permalink

      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.

      Reply
    • 26th July 2019 at 1:01 pm
      Permalink

      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.

      So please ensure, Firebase setup work properly or Follow instructions for Firebase setup in this Post.(Post updated).

      Reply
  • 26th July 2019 at 1:08 pm
    Permalink

    ensure Your firebase setup work properly. Setting Up Firebase Console instruction added in this updated post

    Reply
  • Pingback:Log Temperature Sensor Data to Google Sheet using NodeMCU ESP8266

Leave a Reply

%d bloggers like this: