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
- NodeMCU ESP8266 Module
- 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.
- login you gmail ID (Google account) or sign up.
-
Open your browser and go to “firebase.google.com”
-
At the right top corner go to “Go to Console”
-
Click on “Add project”
-
Insert your Project Name. and click create project.
-
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:
- Go to Settings Icon(Gear Icon) and click on “Project Settings”
-
Now click on “Service Accounts”
-
You can see two options “Firebase admin SDK” and “Database Secrets”
-
Click on “Database Secrets”
-
Scroll on your project name and ”Show” option appears at right side of your project
-
Click on “Show” and now you can see secret key created for your project
-
Copy the secret key and save it to notepad. This is your “FIREBASE_AUTH” string which we have written in Arduino program above.
-
Now go to “Database” on left control bar and click on it
-
Scroll down and click on “Create Database”
-
Choose “Start in test mode” and click on “Enable”
-
Now your database is created
-
Now just above the database you can see
“https://your_project_name.firebaseio.com/”
-
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
-
This is your “FIREBASE_HOST” string which we have written in Arduino program
-
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:
- Dynamic WLAN configuration for ESP32 Board | AutoConnect
- ArduinoOTA ESP32: Wi-Fi (OTA) Wireless Update from the Arduino IDE
- ESP32 with LoRa using Arduino IDE
- How To Use Grove-LCD RGB Backlight with NodeMCU
- NodeMcu to DHT Interface in Blynk app
- How To ON/OFF a bulb by Google voice assistant
- Arduino IDE | Arduino | Open Source Hardware/Softawre | Arduino Vs RPi
- WiFi LoRA 32 (V2) ESP32 | Overview | Introduction
- DHT11 sensor with ESP8266/NodeMCU using Arduino IDE
- Arduino Support for ESP8266 with simple test code
You may like also:
- Raspberry Pi – Introduction | Overview | Setup and Management | Tutorials
- MQTT | What is MQTT | MQTT in Depth | QoS | FAQs | MQTT Introduction
- How to set up Windows 10 IoT Core on the Raspberry Pi
- Best IoT Visual Programming Tools
You may like also:
- ESP32 BLE with DHT11
- DHT11 sensor with NodeMCU using Arduino IDE
- NodeMcu to DHT Interface in Blynk app
- DHT11 vs DHT22: Overview
- Mongoose OS – an IoT firmware development framework
- How To Enable Free HTTPS on your website
- How To Create Secure MQTT Broker
- MQTT Public Brokers List
- Controlling LED with Raspberry Pi
- Home automation | IoT Products for Home Automation
Hi Sir. Do you know how I can send serial data from ESP32 to the firebase real-time database.?
yes, you can. I will be post a tutorial soon.
yes, you can. Nothing different same as Nodemcu. I will be post a tutorial soon.
The data isn’t being uploaded to Fire base, any suggestions?
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.
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).
ensure Your firebase setup work properly. Setting Up Firebase Console instruction added in this updated post
Pingback: Log Temperature Sensor Data to Google Sheet using NodeMCU ESP8266
Hola, les cuento que la línea que dice:
Firebase.pushString(“/DHT11/Humidity”, fireHumid); //setup path and send readings
Firebase.pushString(“/DHT11/Temperature”, fireTemp); //setup path and send readings
debe ser
Firebase.pushString(“DHT11/Humidity”, fireHumid); //setup path and send readings
Firebase.pushString(“DHT11/Temperature”, fireTemp); //setup path and send readings
sin “/” debe ser “DHT11/Humidity”
thanks for suggestion
I’m sure firebase host and auth in database secret but still can’t
Okay, let me check