Sending data to Google Firebase

Sending data to Google Firebase
Why should we send the data to the cloud?
The data which are taken from the sensors or other modules will be stored in the microcontroller’s memory since our microcontroller’s memory is very less and we can store only a small amount of data and this data will also last no longer. The user also cannot frequently check the microcontroller whether it is storing the data or not. So, in order to do this we can store this data in an external memory device or store the data in a cloud over the internet.
This real time database can be used in times when the microcontroller is connected over the internet and can be sent/receive data from the cloud and of course it should be integrated to a sensor/module or we can send the data without a sensor. The database can also be used to perform the analytics of the data, performance, functions based upon the values. There are many cloud platform’s like thingspeak, ubidots, Cayenne, AWS, Azure, IBM clouds, etc.
In this tutorial, we will be using google firebase to store our data. We can divide this tutorial in two parts, one is the hardware section connections with microcontroller and the second one is configuring the google firebase console.
Firstly, we will start the assembling of the hardware components. We will send the distances measured in other words how much distance we are from the obstacle, this distance we will be uploading the data to the firebase for the sake of monitoring. We have to choose an internet connectivity module, which we have mentioned in the previous articles, to refer click here.
Required Components:
1. NodeMCU ESP8266-12E Module.
2. Ultrasonic Sensor(HC SR04).
Circuit Diagram:

Programming NodeMCU:
Now, we will see the important parts in coding. We should include these libraries:
#include "ESP8266WiFi.h"
#include "FirebaseArduino.h"
For the sake of sending the data to firebase we should include the extra libraries in the arduino which are FirebaseArduino.h library for connecting the device to the firebase console and ESP8266WiFi.h for connecting the device to the internet. The ESP8266WiFi.h library will already be included in the arduino. We should include the FirebaseArduino.h library, you can download it from the below link.
https://github.com/FirebaseExtended/firebase-arduino-FirebaseArduino library
We should also include the arduinojson library, the firebasearduino will be interlinked to this json library. So, we should use the old version of this, which is version 5.13.1. You can download it from the arduino library manager and make sure you have selected the right version.

#define FIREBASE_HOST "Your-Project-Name.firebaseio.com" //the project name address from firebase id.
#define FIREBASE_AUTH "SYWDz1iRvLQZ6xxxxxxxxxxxxxxxxxP46zVnRu4" //the secret key generated from the firebase.

These are the credentials of your firebase project which I will explain in the second part.

#define WIFI_SSID "your_wifi_name"
#define WIFI_PASSWORD "your_password"

You should enter your wifi network name and password in the above mentioned place. So that your module will be connected to that specific wifi-network.

WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //connects to the wifi

This statement will try to connect to your wifi network.

const int trigP = 0; //D3 Or GPIO-0 of nodemcu
const int echoP = 2; //D4 Or GPIO-2 of nodemcu

Define the pins of the ultrasonic sensor i.e., trigger and echo pins.

while (WiFi.status()!= WL_CONNECTED) {
Serial.print (".");
delay (500);
}

The above piece of code verifies to us, whether the module is connected to the network or not. It prints “.” until the wifi network is connected.

Firebase.begin (FIREBASE_HOST, FIREBASE_AUTH); //connects to your firebase project
This statement connects to your firebase project, if and only if the entered hostname and authorization is correct.

digitalWrite(trigP, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 microsecond delay
digitalWrite(trigP, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trigP, LOW); // trigPin low
duration = pulseIn(echoP, HIGH); //Read echo pin, time in microseconds
distance = duration*0.034/2; //Calculating actual/real distance
Serial.print ("Distance: ");
Serial.print (distance);

It triggers the sensor for 10mS to send the wave and when it is received we can make a calculation based on the time and can find out the distance. The time taken by the wave to reach our sensor is calculated by the function pulseIN and with the help of that we will find out the distance we are away from the obstacle. This distance data is stored in the variable “distance” and we will be printing this distance in our serial monitor.

String firebaseDistance = String(distance) + String("cm");
delay (5000);
Firebase.pushString ("Distance", firebaseDistance); //setting up the path to send the data

The data which we have acquired is in the form of integer data, in order to send the data we should convert them into character type of data. So we have converted the data into string type and stored it in the variable “firebaseDistance”. You had made a path to your firebase project to send the data.
Code:
#include "ESP8266WiFi.h"
#include "FirebaseArduino.h"

#define FIREBASE_HOST "Your-Project-Name.firebaseio.com" //the project name address from firebase id.
#define FIREBASE_AUTH "SYWDz1iRvLQZ6xxxxxxxxxxxxxxxP46zVnRu4" //the secret key generated from the firebase.
#define WIFI_SSID "your_wifi_name"
#define WIFI_PASSWORD "your_password"

const int trigP = 0; //D3 Or GPIO-0 of nodemcu
const int echoP = 2; //D4 Or GPIO-2 of nodemcu
long duration;
int distance;

void setup () {
pinMode(trigP, OUTPUT); // Sets the trigPin as an Output
pinMode(echoP, INPUT); // Sets the echoPin as an Input
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.print (" ");
Serial.println (WIFI_SSID);
Serial.print ("IP Address is:");
Serial.println (WiFi.localIP ()); //prints the IP address
Firebase.begin (FIREBASE_HOST, FIREBASE_AUTH); //connects to your firebase project
}
void loop () {
digitalWrite(trigP, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 microsecond delay
digitalWrite(trigP, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trigP, LOW); // trigPin low

duration = pulseIn(echoP, HIGH); //Read echo pin, time in microseconds
distance = duration*0.034/2; //Calculating actual/real distance
Serial.print ("Distance: ");
Serial.print (distance);


String firebaseDistance = String(distance) + String("cm");
delay (5000);

Firebase.pushString ("Distance", firebaseDistance); //setting up the path to send the data
}
Setting up your Firebase account:
If you are a new user to this firebase console, you should follow these steps correctly.
1. Login with your gmail account and click on the console for creating the project.https://firebase.google.com/


2. After that, click on “create a project”


3. Enter your project name in step 1 and click on “continue”


4. In the step-2 enable the google analytics if it is not enabled and click continue


5. In step 3, choose the “default account for firebase” and click on create project.

A new project is created in your firebase account.

6. Now go to the settings icon, and click on “project settings”.


7. Now goto the service accounts.


8. Now click on database secrets and click on “show”.


9. After clicking on show, you are able to copy the code. This is the “Firebase AUTH” which is mentioned in the above program. Copy this code and place it in our arduino program.


10. Now click on the real time database which is present on our left control bar and create a database.


11. Choose start in test mode and click on enable.


12. Now you can see your database, in our arduino program we have mentioned firebase auth and firebase host.

So in my project the firebase host is “project-demo-3ca28.firebaseio.com”. You have to remove the “https://” and “/” at the end remaining is your firebase host.

13. So the database is created, we have to create a path for the data which is receiving by the database. As you can see in the above figure click on the “+”


14. Enter the name as “Distance”. The name in the database should be the same as mentioned in our arduino program. Keep the value as “00.0” and click “add”.


Now our project is ready, all you have to do is dump the code in NodeMCU.

Note: After dumping the code if it is not working. Push the RST button on NodeMCU.