Firebase with realtime Database for IoT applications

Duy Nguyen
Konel
Published in
6 min readApr 2, 2018

If someone like me who has begun with very limited knowledge of IT, especially web or app develop fields, Firebase from Google will be a good choice for making a good IoT application with a trusted server for interacting apps (IOS, Android OS or webapps)

Thanks to Firebase I have had a good experience in backend service including realtime database, user authentication, static hosting…So I can focus on the hardware design.

To begin, I would like to have a short introduction about some dominant features of Firebase cloud service:

- Realtime database

  • Database is stored under the format of JSON and to be synchronized with clients in realtime. The cross-platform client is the fundamental platform of this database which all clients share the same resource from Firebase server and it will automatically update when any data is stored of changed
  • Database calculates the size of the project when user upgrades or expands services. Firebase uses NoSQL type for its database, that removes the constraints when interacting with tables, fields. This helps user freely create and decorate database easier.

- Offline work

  • The application on Firebase is maintained even the interruption of internet connection. Once the data are write to the cloud, it will be store at the local database of Firebase. After the internet is reconnected, client’s activities will be updated immediately and synchronized the the server promptly.

- User authentication

  • Firebase created the authenticated function by Email, Facebook, Twitter, GitHub, Google, and anonymous. We can integrate this function for the back-end code of the application as tokens or API secret keys.

- Firebase Hosting

  • This provides an easy method to host a free website fast under the platform of NodeJs. By writing a few commands we can upload the whole website with HTML, CSS and Javascript files to the cloud under the free domain of “WEB-APP-NAME.firebaseio.com”. We can upgrade or connect the webapp to the private domain.

- Prices

  • Google Firebase provides many services with different size of storage and level of bandwidth from the free cost to $1500/month that let beginners to experience the realtime application before setting a start-up application for their infant company.

*ESP8266 and Firebase for IOT application

It is fortunate that Firebase has supported IoT application with ESP8266 board. I will show one example for how to interact with the realtime database and the practical application in Smarthome by controlling power sources in house.

1) Hardware preparation

  1. ESP8266 Wemos D1 (x1 board)

2. 4-relays control board

2) Sofware preparation

  1. Arduio IDE for programming C code to the ESP8266.
  2. Library of Firebase for ESP8266.
  3. Programming editor for webapp (any editor like ATOM, Notepad++)

We can download from this link https://github.com/firebase/firebase-arduino

3) Create an account on firebase

If we have already google account (gmail account) then just signing in with gmail account. Firebase will find your account on your computer easily.

Next, we get started for a new project. When we add a project, project name can be filled freely, then a project ID will be created by project name with a random number. We can change this ID to an easy-remembered name like “relayseppy” in my project. This is quite important because the project ID is also a part of the app url. Then click “Create Project” to begin new one.

From here we can start with several choice of IOS app, Android app or Webapp. In this artical I mention only how to set up the simple webapp with realtime database to interact with the ESP8266.

Firebase provides a short Javascript code to attach in the HTML web app for connecting to the API of my account. We can recognize that the url to link with firebase now is “https://relayseppy.firebaseio.com” — it’s not a bad name for a free hosting!!! Next, we need to prepare a website with HTML-CSS-Javascript files to upload on the server.

Create Reatime Database

By default, the database will set the rules of read and write to “null”, which means it needs the authenticated certificate for connect with the database, but we can change them to the status of “true” to allow anyone can access to the database if they know the url (https://relayseppy.firebaseio.com).

Ok now we have the reatime database which is ready to receive and store any data from the ESP8266. Below is the code for simple connection to the firebase’s database which is used to control 4 relays (We can expand to as many relays as possible, this depends on the hardware capacity)

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define Relay_1 5
#define Relay_2 4
#define Relay_3 0
#define Relay_4 2
// Set these to run example.
#define FIREBASE_HOST “relay-seppy.firebaseio.com”
#define FIREBASE_AUTH “”
#define WIFI_SSID “Viet Dung”
#define WIFI_PASSWORD “3vdung64”
int state_1 = 1;
int state_2 = 1;
int state_3 = 1;
int state_4 = 1;
int state_5 = 1;
int state_6 = 1;
int state_7 = 1;
int state_8 = 1;
void setup() {
Serial.begin(115200);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
digitalWrite(Relay_1, state_1);
digitalWrite(Relay_2, state_2);
digitalWrite(Relay_3, state_3);
digitalWrite(Relay_4, state_4);
// digitalWrite(Relay_5, state_5);
// digitalWrite(Relay_6, state_6);
// digitalWrite(Relay_7, state_7);
// digitalWrite(Relay_8, state_8);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print(“connecting”);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(“.”);
delay(500);
}
Serial.println();
Serial.print(“connected: “);
Serial.println(WiFi.localIP());

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
int n = 0;void loop() {
state_1 =Firebase.getInt(“Relay_1”);
state_2 =Firebase.getInt(“Relay_2”);
state_3 =Firebase.getInt(“Relay_3”);
state_4 =Firebase.getInt(“Relay_4”);
// state_5 =Firebase.getInt(“Relay_5”);
// state_6 =Firebase.getInt(“Relay_6”);
// state_7 =Firebase.getInt(“Relay_7”);
// state_8 =Firebase.getInt(“Relay_8”);
// get value
Serial.print(“Relay_1: “);
Serial.println(state_1);
digitalWrite(Relay_1, !state_1);

// get value
Serial.print(“Relay_2: “);
Serial.println(state_2);
digitalWrite(Relay_2, !state_2);

// get value
Serial.print(“Relay_3: “);
Serial.println(state_3);
digitalWrite(Relay_3, !state_3);

// get value
Serial.print(“Relay_4: “);
Serial.println(state_4);
digitalWrite(Relay_4, !state_4);

// // get value
// Serial.print(“Relay_5: “);
// Serial.println(state_5);
// digitalWrite(Relay_5, state_5);
//
// // get value
// Serial.print(“Relay_6: “);
// Serial.println(state_6);
// digitalWrite(Relay_6, state_6);
// // get value
// Serial.print(“Relay_7: “);
// Serial.println(state_7);
// digitalWrite(Relay_7, state_7);
//
// // get value
// Serial.print(“Relay_8: “);
// Serial.println(state_8);
// digitalWrite(Relay_8, state_8);
Serial.println(“________________________”);
delay(100);
}

To demonstrate the realtime operation, I created a simple webapp which can has the interface of 4 buttons corresponding to 4 relays. The system works very well under good speed of the Internet.

From this application we can make a simple IoT control for the household electrical appliances: fan, light, TV, air-conditioner…

To sum up: Firebase cloud with the realtime database provides a free and simple solution for newbie in IoT applications. If people who are scare of reading bunch of tutorial with theory about how to connect server, database sercurity, etc. Firebase tutorials are very simple and short, this inspires lazy people like me to keep studying when I am in the middle of vast ocean of IoT field.

— — — — — — Keep working-Keep learning-Keep Earning — — — — —

Seppy

--

--