Today we will be focusing on communication between our ESP8266 NodeMCU and Firebase Realtime Database. If you already created your project in firebase, you need to go to the All Products menu from the right sidebar and expand that menu. From that menu find for Realtime Database and navigate to it. You will find an “Enable” button which will let you enable the Firebase Realtime Database for your project.
When you enable the Firebase Realtime database you will get a URL of that database which you need to copy. This URL will be the source of communication for your ESP8266 NodeMCU with the Firebase Realtime database. Save this URL in your Arduino Sketch. Now goto the Rules tab on your Firebase Realtime database and make that rules public. This would let you quickly test your firebase realtime database without auth token.
Today’s article is not about the Auth Token and the Authentication to the firebase. We are only focusing on Firebase Realtime Communication with our esp8266 NodeMCU. Once you make your rules public it will give you a red message and tell you that it is not safe. Just ignore this message. Here are How the updated rules looks like when you hit publish.
Arduino Library for Firebase Realtime Database
Now we need to open the Arduino IDE and had to install our required libraries for the ESP8266 to communicate with Firebase Realtime Database. Although there is another kind of database option available which is called Firestore, and it is document-based database like MongoDB, but our focus is only with the Realtime database. This Realtime Database let you manage your data in real time. You can instantly get all the updated data on the database.
We are using the ESP8266Firebase Library by “Rupakpoddar” on GitHub. Just navigate to the library manager in your Arduino IDE and on the top search bar type the name ESP8266Firebase, you should check the name of the author which is “Rupakpoddar“. Once confirmed, hit Install button and from the popped dialogue, select “Install All” Which will install all the dependencies for this library. This Firebase library based on ArduinoJson Library which will also be installed.
ESP8266 Firebase Realtime database Code
Now we need to write the sketch to communicate with the Firebase Realtime database. We will provide the full code which is taken from the example of the library and will explain how it is working. The most common part of the code is where we read the Firebase database. There is total two main parts while communicating with the firebase Realtime database. One is to write data to the firebase database, and second is to read from the firebase Realtime database.
Although the reading and writing process is instantaneous and you can get the update from the database as soon as it is updated just your network delay would be induced, also you can write the data very fast. But we need to put some delay while writing or reading data. It all depends on your requirements. But you should keep focus on putting some delay.
Initialize the Firebase Object
First of all you have to import the required libraries in the sketch. After that you have to connect to the internet via Wi-Fi. This is how we will do this in the Arduino Sketch for the ESP8266.
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <ESP8266Firebase.h>
#include <ESP8266WiFi.h>
#define _SSID "project" // Your WiFi SSID
#define _PASSWORD "project123" // Your WiFi Password
#define REFERENCE_URL "https://arduino-test-XXXXX.firebaseio.com/" // Your Firebase project reference url
Code language: Arduino (arduino)
Firebase object is simply the instance being initialized.
Firebase firebase(REFERENCE_URL);
Code language: Arduino (arduino)
Now to initialize the Wi-Fi you should do that in setup() function in your Arduino Sketch for the ESP8266.
void setup() {
Serial.begin(115200);
pinMode(device1, OUTPUT);
pinMode(device2, OUTPUT);
pinMode(device3, OUTPUT);
pinMode(device4, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
digitalWrite(LED_BUILTIN, HIGH);
firebase.json(true); // Make sure to add this line.
}
Code language: PHP (php)
Writing Data to Database
There are different functions available for writing the data to the database for different kind of data types.
int setString(String path, String data);
int setNum(String path, String data);
int setInt(String path, int data);
int setFloat(String path, float data);
int pushString(String path, String data);
int pushNum(String path, String data);
int pushInt(String path, int data);
int pushFloat(String path, float data);
Code language: Arduino (arduino)
Reading Data from Database
The reading data from the database is more challenging. You can get the value for individual json end. Or you can read the whole object with the Json parsing like mentioned in the examples. Here is the code for reading the values and making the decision based on this.
String data = firebase.getString("cmd"); // Get data from database.
// Deserialize the data.
// Consider using the Arduino Json assistant- https://arduinojson.org/v6/assistant/
const size_t capacity = JSON_OBJECT_SIZE(5) + 60;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, data);
String Device1 = doc["Device1"];
String Device2 = doc["Device2"];
String Device3 = doc["Device3"];
String Device4 = doc["Device4"];
// Print data
Serial.println("Device 1: "+Device1);
Serial.println("Device 2: "+Device2);
Serial.println("Device 3: "+Device3);
Serial.println("Device 4: "+Device4);
Serial.println("");
String status_list[4] = {Device1, Device2, Device3, Device4};
for (int i=0;i<4;i++) {
if (status_list[i] == "ON"){
digitalWrite(device_list[i], HIGH);
}
else{
digitalWrite(device_list[i], LOW);
}
}
Code language: JavaScript (javascript)