Program 3: Writ a C program to Send Real-Time Sensor Data to Google
Firebase with ESP8266.
Components required
1. NodeMCU ESP2866
2. DHT11 Sensor
3. Breadboard
4. Jumper Wires
5. Micro USB Cable
Setting Up Hardware & NodeMCU DHT11 Circuit
Here is a circuit diagram for connecting DHT11 Humidity Temperature Sensor
with NodeMCU ESP8266 Board. Connect the positive terminal of DHT11 to
3.3V & negative terminal to GND. Connect the digital output pin to D2 of
NodeMCU.
You can assemble the circuit on breadboard and you can see the
image below.
Installing Libraries
FirebaseArduino is a library to simplify connecting to the Firebase database
from Arduino clients.
The library cannot work standalone. So you need to add the ArduinoJSON
library as well.
1. ArduinoJSON Library
So first go to the library manager and search for “JSON” & install the library as
shown in the figure below.
Note: The latest JSON library might not work with the code. So you may need
to downgrade the library to version v5.13.5.
2. Google FirebaseExtended Library
Now you need to install Google Firebase library as well. So, download the library
in google by typing “firebase arduino library” or click the below link
https://github.com/FirebaseExtended/firebase-arduino and add it to Library folder
of Aurdino after extraction.
Setting up Google Firebase
To get Humidity and Temperature using Google Firebase & Nodemcu ESP8266,
you need to setup the Google Firebase first.
Step 1: If you have Gmail id then you are already Sign Up for firebase. But if you
don’t have Gmail id then first Sign Up for Gmail: https://gmail.com/
Step 2:Now visit https://firebase.google.com/ and click on Go to Console on the
Top Right.
Step 3: Click on “Create a Project”.
Step 4: Give your “Project name”, then tick the “I Accept the Firebase
Terms” & finally Click on “Continue”.
Step 5: Now, another window will appear. So click on “Continue”.
Step 6: Select the “google Analytics Account” that is made using the
Gmail ID. And then click on “Create Project”.
Step 7: Your project is ready now. So you will get the following
window. Click on “Continue”.
Step 8: Now click on “Project Setting”.
Step 9: Under Project Setting, Click on “Service Accounts”. Copy the
secret key from below. The code is required in the Arduino Code.
Step 10: Click on “Create Database”.
Step 11: Choose “Start in Test Mode” and then click on “Next”.
Step 12: Now click on “Done” & from left side click on “Database”.
Once the setup is done you will get the following window.
Note: 1. Change the board “esp8266 version to 2.7.4” (Go to File->Preferences-
>URL-> copy and paste the URL
https://arduino.esp8266.com/stable/package_esp8266com_index.json) then go to
Tools->board->board manager->type “ esp8266” then select the version 2.7.4 and
install it.
2. Change the fingerprint in “firebase Arduino master” folder because the
fingerprint will update every month. To update fingerprint go to google.com->type
“GRC SSL” click on the first link ->copy the firebase real time database URL in
search bar and click on the button “ fingerprint site”. It will take you to another
webpage where you can find the updated fingerprint then copy it and update in the
file “FirebaseHttpClient” (Arduino->libraries->firebase Arduino master->src->
FirebaseHttpClient).
Source Code/Program: Send Sensor Data to Google
Firebase with ESP8266
// First, we include the library for Firebase & DHT11 Sensor
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <DHT.h>
// Then we define the two
parameters FIREBASE_HOST & FIREBASE_AUTH. We get these parameters
from Google Firebase Setup. These two parameters are very important to
communicate with firebase. This enables the data exchange between the ESP8266
and the firebase.
#define FIREBASE_HOST "******************.firebaseio.com"
#define FIREBASE_AUTH "***********************************"
// Then we define the WiFi SSID & Password. Replace SSID and password with
your network SSID and password. The Nodemcu will connect to the network &
communicates with Google Firewall.
#define WIFI_SSID "***********"
#define WIFI_PASSWORD "**************"
// Then we define the DHT Sensor type & create the DHT Sensor instances.
#define DHTPIN D2 // Digital pin connected to DHT11
#define DHTTYPE DHT11 // Initialize dht type as DHT
11
DHT dht(DHTPIN, DHTTYPE);
// These lines are for making the NodeMCU ESP8266 Board connect to the Wifi
Network. Once connected, the serial monitor will display the Connection Status
and prints the IP Address.
void setup()
Serial.begin(115200);
dht.begin(); //reads dht sensor data
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");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); //prints local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to
the firebase
void loop()
float h = dht.readHumidity(); // Read Humidity from sensor
float t = dht.readTemperature(); // Read temperature from sensor
if (isnan(h) || isnan(t)) // Checking sensor working
Serial.println(F("Failed to read from DHT sensor!"));
return;
Serial.print("Humidity: ");
Serial.print(h);
String fireHumid = String(h) + String("%"); //Humidity integer to
string conversion
Serial.print("% Temperature: ");
Serial.print(t);
Serial.println("°C ");
String fireTemp = String(t) + String("°C"); //Temperature integer to string
conversion
delay(5000);
// we are sending the data to google firebase using the path provided by the code.
Firebase.pushString("/DHT11/Humidity", fireHumid); //setup path to send
Humidity readings
Firebase.pushString("/DHT11/Temperature", fireTemp); //setup path to send
Temperature readings
// These verify whether the data is sent to the Google Firebase or not.
if (Firebase.failed())
Serial.print("pushing /logs failed:");
Serial.println(Firebase.error());
return;
}