NodeMCU Firebase’s database integration
Some time looking for simple integration of databases on IoT devices is either little challenging or you will find many different cloud-based solutions which look like expensive solutions for a small IoT project. This blog will help you to have a simple and quick solution for any such application which needs back-end integration with NodeMCU.
This blog covers these three important topics that enable you to have fully functional NodeMCU with Firebase.
- NodeMCU
- Firebase real-time database
- Integration on Android App
NodeMCU
NodeMCU(Node Microcontroller unit) is an open-source, low-cost, and Lua based IoT platform.
It has a memory of 128kBytes and storage 4Mbytes. It has a very inexpensive system-on-a-chip(SoC) CPU called ESP8266.
ESP8266 is a microcontroller with WiFi capability. ESP8266 12-E NodeMCU Kit is one of the most used ESP8266 development boards.
NodeMCU/ESP8266 kit has 17 GPIO pins.
Official website: http://www.nodemcu.com/index_cn.html
Firebase integration with NodeMCU
To set up a firebase database connection on NodeMCU, You can use this library Firebase-ESP8266. Here are the steps to follow to set up the basic configurations:
- Install “FirebaseESP8266.h” from the Arduino manager.
- Add FIREBASE_HOST and FIREBASE_AUTH. You will get the host of your real-time database from Firebase’s real-time dashboard and Auth will be presented in project details ie. Project overview -> Setting icon -> Project setting ->Service accounts tab -> Database secrets. Copy secret specific to your database and paste for FIREBASE_AUTH.
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h> //1. Install this library
#define FIREBASE_HOST "https://arduino-iot-f1ef8.firebaseio.com/"
#define FIREBASE_AUTH "bFIy425243dfkjh34DSD24524g38PVzm"
#define WIFI_SSID "WiFi05G"
#define WIFI_PASSWORD "MangoWorld"
void setup() {
pinMode(D0, OUTPUT);
Serial.begin(9600);
// 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);
}
FirebaseData firebaseData;
void loop() {
if (Firebase.getBool(firebaseData, "/led_status")) {
if (firebaseData.dataType() == "boolean") {
if(firebaseData.boolData() == 1) {
digitalWrite(D0, LOW);
} else {
digitalWrite(D0, HIGH);
}
} else {
Serial.println("Check if you have same dataType: " + firebaseData.dataType());
}
} else {
Serial.println(firebaseData.errorReason());
}
}
Firebase:
Firebase is Google’s mobile platform which provides SDKs for real-time database, analytic, dynamic linking, cloud store, FCM, and many more to helps the developer quickly develop high-quality apps.
Firebase provides a real-time database and mobile-back-end as a service. You’ll be using a Firebase real-time database to communicate with a NodeMCU broker.
How to set up a Firebase account and real-time database:
- Get started with the Firebase console and sign In.
- Upon sign-in, follow the instruction shown below in images about “Creating a project in Firebase console”.
- After clicking on the “Create project” button, it will take a few seconds i.e. ~30 sec to create and set up the project.
- Upon the “Continue” button click, you will navigate to the project dashboard page.
5. In the left pane, under “Develop”, choose “Database” and click on “Create database”.
6. You see the “Create database” overlay for the setting up of your database as shown in the images below “Setting up the database”.
7. Choose “Start in test mode” and click “Next”. Now you can either use the default one or any other region from the dropdown for the Cloud Firestore location.
8. Upon the “Done” button click, database provisioning completes, and then you will navigate to the Database dashboard.
9. By default, the “Cloud Firestore” dashboard opens. To change to the real-time database, you need to select a “Realtime database” from the drop-down given next to “Dashboard”.
10. Click on “+” to add a new field which is a key-value pair. Enter “led_status” as name and “1” as value. You can use true/false as well instead of 0 or 1 if you want to represent the state of the LED. You will update this value from a mobile application that will turn on/off the nodeMCU’s LED light.
This was all about setting up a real-time database on the Firebase console and the next step will be the integration of the Android mobile application.
Realtime database implementation on the mobile application
To implement a realtime database on the mobile application, follow the below steps:
- Create a new Android if you don’t have an existing one. You can choose the “Empty Activity” template and configure the project.
- Now navigate to Firebase console’s “Project Dashboard” and create an app by selecting the Android icon as shown below in the image “Create an Android application”.
2. To register an application, enter application details like the application package name, Nickname(optional), and debug signing certificate(optional).
3. Now follow further instructions on this page to add the firebase config file and firebase SDK into your Android application project.
4. Upon following instructions, installation verification will be completed on running the Android application.
5. Your application has been added to the “Project Dashboard” as shown in the image below.
6. Now add a toggle button in the “activity_main” layout XML file.
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
7. Create a Firebase database instance and get the reference of the field you created on Firebase console’s real-time database i.e. led_status.
val firebaseDatabase = FirebaseDatabase.getInstance()
val reference = firebaseDatabase.getReference("led_status")
8. Now to turn on/off the LED light, you need to update the value of reference i.e. “led_status”. To do this, add toggle button change listener, and update the value 1 if the toggle button is ON and set 0 if the toggle button is OFF.
toggleButton.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
reference.setValue(1)
} else {
reference.setValue(0)
}
}
9. Run the application to install it.
10. Upon installation, you will see value in the Firebase real-time database is updating as you click on the toggle button.
That’s all in for now. Please feel free to comment below if you find any difficulty in the integration of NodeMCU with the firebase realtime database, I would be happy to help.
Happy learning!!!