Nodemcu Sending DHT11 Sensor Data to Firebase

Today we will be building a project where we’ll use a temperature & humidity sensor DHT11 and a NodeMCU ESP8266 Module to log the sensor data temperature and humidity in real time on Google’s Firebase database server.
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 will 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. There are lots of IoT Hardware Platforms and Cloud platforms available to serve this purpose but for this project we are going to use Google Firebase specifically.
We will divide this tutorial project in two sections. Firstly, we will start with assembling the hardware components and uploading firmware into it. And secondly we will use Firebase to setup with NodeMCU to exchange real time data. If you are new to ESP8266 or Firebase follow our previous tutorial on controlling LED using Firebase.
Components Required
- NodeMCU ESP8266 Module
- DHT11 Temperature and Humidity sensor
Configure the connection as shown in the circuit diagram below.

Programming NodeMCU ESP8266 Live Temperature and Humidity Monitoring
This project needs 3 library files which can be downloaded from these links below.
https://github.com/FirebaseExtended/firebase-arduino/blob/master/src/Firebase.h
http://downloads.arduino.cc/libraries/github.com/bblanchon/ArduinoJson-5.13.1.zip
https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTstable
The complete code for this project will be given at the end of this tutorial. Here we are explaining few important parts of the code. Firstly include the libraries for using ESP8266 and firebase and DHT11 sensor library.
#include <ESP8266WiFi.h
#include <FirebaseArduino.h
#include <dht.h>
Secondly, these two parameters are very important to communicate with firebase. Setting these parameters will enable the data exchange between and ESP8266 and firebase. To find these parameters for your project, follow our previous tutorial on Firebase Setup.
#define FIREBASE_HOST "my-project-bba83.firebaseio.com"
#define FIREBASE_AUTH "YGItxxkytY9ZAESVdXEfPjoBEXMi05nFNR7hV4Yh"
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
Start taking reading at pin D4 of NodeMCU.
int chk = DHT.read11(DHT11_PIN);
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("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
Serial.println(dataIn);
delay(5000);
Finally, send the temperature and humidity data to firebase at path “my-project-bba83.firebaseio.com”.
Firebase.setFloat("temperature", DHT.temperature);
Firebase.setFloat("humidity", DHT.humidity);
You can see all data in your firebase account. Just go to “Database” section in “Your Project” at “My console” In Firebase.

Complete Code
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN D4
// Set these to run example.
#define FIREBASE_HOST "my-project-bba83.firebaseio.com"
#define FIREBASE_AUTH "YGItxxkytY9ZAESVdXEfPjoBEXMi05nFNR7hV4Yh"
#define WIFI_SSID "myWifi"
#define WIFI_PASSWORD "123456789"
void setup() {
Serial.begin(9600);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop() {
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
Firebase.setFloat("temperature", DHT.temperature);
Firebase.setFloat("humidity", DHT.humidity);
delay(5000);
}