In this post, we will learn how to Send Sensor Data to Android Using Google Firebase & NodeMCU ESP8266.
Overview: Send Sensor Data to Android Using Google Firebase & ESP8266
In my earlier post, we learned how to send and receive data using Google firebase with NodeMCU ESP8266. We learned how to upload and download data to/from a Firebase database with Arduino IDE and ESP8266 module. Storing data (like sensors data) to a database that can be accessed from anywhere by the internet may be very useful. Firebase makes storing and retrieving data easy.
You can check the previous tutorial before proceeding for this new tutorial:
1. LED Control using Google Firebase & ESP8266
2. Send Real-Time Sensor Data to Google Firebase with ESP8266
In this tutorial we will learn How to send Sensor Data to Android Application Using Google Firebase & Nodemcu ESP8266. We will use DHT11 Humidity & Temperature Sensor to read the Temperature Humidity data of surrounding. We will send these sensor data to Google Firebase Database. We will then retrieve data from Google Firebase to Android Application. The Android application used here is designed using MIT App Inventor. Designing apps using MIT App Inventor is pretty easy. We will learn that too here.
What is Google Firebase?
Google Firebase is a Google-backed application development software used for creating, managing,and modifying data generated from any android/IOS application, web services, IoT sensors & Hardware. To learn more about the Google Firebase Console, you can read the official Google Firebase Documentation from Google Firebase
Firebase Auth which is a service that can authenticate users using only client-side code. It supports social login providers Facebook, GitHub, Twitter and Google (and Google Play Games). Moreover, it includes a user management system whereby developers can enable user authentication with email and password login stored with Firebase.
Bill of Materials
Following are the components required for making this project. All the components can be easily purchased from Amazon. The component purchase link is given below.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
| 2 | DHT11 Sensor | 1 | Amazon | AliExpress |
| 3 | Micro-USB Cable | 1 | Amazon | AliExpress |
| 4 | Jumper Wires | 1 | Amazon | AliExpress |
| 5 | Breadboard | 1 | Amazon | AliExpress |
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/GPIO4 of NodeMCU.
If you want to learn more about the DHT11 Humidity Temperature Sensor, you can follow this post: Interfacing DHT11 Humidity Temperature Sensor with Arduino
You can assemble the circuit on breadboard as I used a breadboard for Assembly too. You can see the image below.
Now let us learn how we can Send Sensor Data to Android Using Google Firebase & ESP8266.
Installing Libraries
FirebaseArduino is a library to simplify connecting to the Firebase database from Arduino clients. It is a full abstraction of Firebase’s REST API exposed through C++ calls in a wiring friendly way. All JSON parsing is handled by the library and you may deal in pure C/Arduino types.
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 from below link and add it to Library folder after extraction.
3. DHT11 Library
To read the temperature and humidity we need any temperature humidity sensor. For that DHT11 is the best and cheap sensor. We need to install DHT11 Library for that. Download the library from below and add to the Arduino IDE.
Setting up Google Firebase Console Database
Now the main thing that we need to do is Setting up Google Firebase Console Database. Once the setup is done, we can then Send Real-Time Sensor Data to Google Firebase with Nodemcu ESP8266.
But I won’t be explaining here how you can set up the Google Firebase Console Database because I have already explained the entire process in the previous tutorial. You can check the following tutorial to learn how to do the setup.
Once the setup is done you will get the following window.
Now we need to modify the Rules. For that click on rules and set all parameter to “true” which is initially set as “false”.
Designing Android App with MIT App Inventor 2
Now we need to design an Android app where we can monitor the sensor data in real-time. First, we will design the Android Application and then we will Send Sensor Data to Android Phone Using Google Firebase & ESP8266.
For desiging the Android App you can simply visit http://ai2.appinventor.mit.edu/. Then create an account and start a new project. The design view screen is shown below in the figure.
On the database part you need to enter the “Firebase URL” while leaving the “FirebaseToken” & “Projectbucket” blank.
While, if you look at the designer part of the block it looks something like this.
All you need to do is, Download the .aia file and import it to the MIT App Inventor Project. And then simply copy the URL of your google firebase database.
Source Code: Send Sensor Data to Android Using Google Firebase & ESP8266
|
1 2 3 |
#define FIREBASE_HOST "mydhtsensor-26aa7.firebaseio.com" #define WIFI_SSID "Alexahome" #define WIFI_PASSWORD "12345678" |
Before uploading the code, make sure to do the changes to FIREBASE_HOST. You can get this parameter from Google Firebase Console. Check the previous tutorial to learn more.
Also change the WIFI_SSID & WIFI_PASSWORD from the given code and replace it with your wifi SSID & Password.
Check the complete code here.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <ESP8266WiFi.h> #include <FirebaseArduino.h> #include "DHT.h" #define FIREBASE_HOST "sensorandroid-42dd6.firebaseio.com" #define WIFI_SSID "Alexahome" // Change the name of your WIFI #define WIFI_PASSWORD "xstevedore1" // Change the password of your WIFI #define DHTPIN D2 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); 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()); Firebase.begin(FIREBASE_HOST); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); // Reading temperature as Celsius (the default) Serial.print("Humidity: "); Serial.print(h); Serial.print("% Temperature: "); Serial.print(t); Serial.println("°C "); Firebase.setFloat ("Temp",t); Firebase.setFloat ("Humidity",h); delay(3000); } |
Code Explanation
First, we include the library for Firebase & DHT11 Sensor
|
1 2 3 |
#include <ESP8266WiFi.h> #include <FirebaseArduino.h> #include "DHT.h" |
Then we define the FIREBASE_HOST. We get this from Google Firebase Setup. This enables the data exchange between the ESP8266 and the firebase.
|
1 |
#define FIREBASE_HOST "*********************.firebaseio.com" |
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.
|
1 2 |
#define WIFI_SSID "Alexahome" #define WIFI_PASSWORD "12345678" |
Then we define the DHT Sensor type & create the DHT Sensor instances.
|
1 2 3 |
#define DHTPIN D2 #define DHTTYPE DHT11 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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()); |
This statement lets the Nodemcu connect with the firebase server. If the host address is correct then it will connect successfully
|
1 |
Firebase.begin(FIREBASE_HOST); |
Using this we are retrieving the humidity and temperature value from DHT11 Sensor.
|
1 2 |
float h = dht.readHumidity(); float t = dht.readTemperature(); |
Now we are sending the data to google firebase using the path provided by the code.
|
1 2 |
Firebase.setFloat ("Temp",t); Firebase.setFloat ("Humidity",h); |
Send Sensor Data to Android Using Google Firebase & ESP8266
Once the code is uploaded, you can open the Serial Monitor. The serial monitor will display the temperature and humidity logged data.
Similarly you can check the Google Firebase Database, it will show the real-time data of Temperature and Humidity logged in from NodeMCU ESP8266.
Now open the Android Application in your smartphone to see the logged data on App Screen.

















1 Comment
i dont understand why here we don’t use FIREBASE_AUTH in firebase.begin() function while its used in the other examples