Google Firebase is a comprehensive platform for developing and managing web and mobile applications. It provides a wide range of tools and services that help developers build high-quality apps, improve user engagement, and streamline app development processes. Firebase is a cloud-based platform developed by Google, and it includes the following key features and services:
- Realtime Database: Firebase offers a NoSQL, cloud-hosted database that enables developers to store and synchronize data in real time. It's often used for applications that require live updates, such as chat apps, multiplayer games, and collaborative tools.
- Authentication: Firebase Authentication simplifies user management by providing secure authentication methods, including email and password, social media logins (e.g., Google, Facebook, Twitter), and anonymous authentication. This service ensures that only authorized users can access your app.
- Cloud Firestore: Firestore is a more scalable, flexible, and feature-rich NoSQL database offered by Firebase. It's designed for real-time syncing, offline support, and complex querying. Firestore is often used for complex data-driven applications.
- Cloud Functions: Firebase Cloud Functions lets you run serverless backend code in response to events. You can trigger functions based on database changes, authentication events, HTTP requests, and more. It's used for tasks like sending notifications, processing data, and integrating with third-party services
Firebase's real-time database and Firestore can be used to store and retrieve data from IoT devices, making it easier to build applications that require real-time monitoring and control.
Realtime DatabaseTo store IoT data in real-time, allowing instant synchronization with devices and users. It's scalable, serverless, and handles authentication. It simplifies IoT backend, enabling data storage and access without server management. Here's a concise explanation of how it's used:
- Data Storage: IoT devices send data to the Realtime Database in real time.
- Real-time Synchronization: Data is synchronized instantly, making it available to users and other devices in real time.
- Serverless: No need to manage server infrastructure; Firebase handles it.
- Authentication: Ensure that only authorized devices can access and update IoT data.
- Web and Mobile Access: Develop web and mobile apps for users to monitor and control IoT devices, seamlessly integrating with the Realtime Database.
Firebase Realtime Database simplifies IoT backend development, offering real-time updates and scalability without managing servers.
Getting StartedLet us get started on using the platform, with our existing Google Account. Like most of the previous IoT Platform tutorial, this is basic as well. Following the steps should get you through it.
Go to the Google Firebase (https://firebase.google.com/) and click on the "Get Started" button. You will be redirected to the login page, if not already logged in to the google account.
Once you are logged in, you will be redirected to the Console Page, there we can create a project which will bind our Realtime Database from our IoT Device.
Click on Create a Project, and go through 3 steps -
1. Enter Project Name
2. Enable Google Analytics (Free)
3. Select your country & Accept the terms
There you go, in a few seconds the Project will be created!
Creating DatabaseOnce you open your project, we can create a database in the Realtime Database and configure the index based on our inputs -
On the left side panel, visit Build > Realtime Database. Once open, we can click on the button 'Create Database' to create our database on realtime database panel.
Now, it'll ask you for your Database Location. The best would be to select the nearest datacenter, for lower latency. Hence, I selected asia-southeast1. Then, select the 'Locked Mode' as rules under security configurations.
There we go, our database is created. In the next step, we will create the indexes that we will be updating simultaneously from our IoT Device ESP32.
Preparing Database for IoT DataTo prepare the database, we will create key and default value on the database, which we desire to be filled through the IoT Device. Storing the device data on the Firebase Database would allow us to re use the data over any of the Firebase application connected to the realtime database. Or even firebase, if integrated.
Beside the Firebase link, we will find '+' icon which will allow to add the key and value pair, as shown below -
As a sample for the project, refer to the below defining schema to align with the method of data pushing from the IoT Device -
Similarly, we can add another schema for Device2, as well.
Once we have the database ready to be manipulated, let us grab our authentication key, so that we can use the Base URL and auth key to manage the realtime database on firebase.
Database Secret Key (Legacy Method)Now we need to save the database secrey auth key to be used during an HTTP request as a parameter for authentication to access the realtime database. Let us get ahead -
On the left-side panel, beside Project Overview, the β(gear icon) drops down the menu for Project Settings. Click on that, and navigate to the 'Service Accounts' tab.
There, under the the Legacy Credentials section, you'll see Database Secrets. Click on that and scroll below, to find your secret key.
This is all you need to use the Firebase through an HTTP request - GET/PUT.
Below is a sample of a request for GET request with the Base URL and Auth Key -
https://hackster-iot-default-rtdb.asia-southeast1.firebasedatabase.app/Device1.json?auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If I enter the above URL with my auth key, then we will be able to view the realtime database on firebase. Try the same on your side!
Next, we will use an ESP32 as an IoT Device to GET data from the firebase Database as cloud.
ESP32 - Control & Monitor DatabaseVisit the code section, for complete code, and change below variables as per requirements. Like SSID & Password of WiFi Hotspot, and serverName & secret key of Firebase Database
const char* ssid = " ";
const char* password = " ";
String serverName = "https://hackster-iot-default-rtdb.asia-southeast1.firebasedatabase.app/";
String section = "Device1.json"
const char* auth = "?auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Now, let us look into the section of code that pushes new data to the database.
We will push a random data valued between (0-99). This needs to be structured in below JSON format, therefore we will use a library to do the same.
{
"Data": 25,
"Status": "online"
}
Below are the sections of code, where we shall use the library ArduinoJSON to serialize the data in a proper JSON format before we update the database from the ESP32 Device.
char val[128];
JsonDocument out;
out["Data"] = random(0,99);
out["Status"] = "online";
serializeJson(out, val);
Run the code, and open the serial monitor to check if the data is being pushed successfully.
Above, we can view the firebase JSON data without parsing.
On Firebase, the data updates LIVE as we see the section getting highlighted on the Realtime Database.
Next, we can prepare a mobile app with a Firebase login, to view the above data update directly pulled from cloud.
Comments