In this project, I will show you how to Make an IoT IOT device using Google firebase and Your own custom Android application made using MIT App inventor.
There are many third party online servers and platforms available for showcasing the data of your IoT project. But, out of all of these platforms like ThingSpeak, Adafruit.io, Blynk, and IFTTT, etc., but today I select Firebase.
Why Choose Firebase?Firebase provides a quick way to keep sensory data collected at the device level, and it works great with the Android APIs, which is supported by AndroidThings. A lot of mobile and device programmers that I have come across struggle with server-side programming. Firebase can really help bridge that gap and make it easier. It will be interesting to see developers use its offline features. If you are new to IoT or generally any device that collects data and needs to transmit it over networks, the golden rule to be assumed is that network connectivity cannot be assumed. As a result, you will need to collect the data offline and when the network is available, transmit this over to your server. Firebase with its offline features can really make this simple for a lot of developers.
Firebase has a ton of features including Realtime Database, Authentication, Cloud Messaging, Storage, Hosting, Test Lab, and Analytics, but I’m only going to use Authentication and Real-time Database.
OK, let's move on to project construction.
I will divide this project into three parts:
- Creating a Firebase account
- Making the app
- Programming Arduino
First of all, you will need a Google account for using the firebase. Open Firebase console by clicking here. Sign in with your Google Account and click on the create button. Name the project "FirebaseIOT" and mark the checkbox where you are asked to accept the Firebase terms. After that click "Continue". Again click the Continue button on the next screen. On the next window, you have to mark two checkboxes, after that click on "Create Project".
Now, On the left side, click on the Develop Tab, and the Database. Click on "Create Database". Click "Next" and then "Done". Here, Cloud Firestore will be loaded automatically which we don't use in this project. So, click on the Firestore and change it to Realtime Database. Now we have to provide the Read and write permissions so that our Hardware and Software could be able to access the Firebase Database. To do so, head over to the Rules tab and change the permissions to "true" from false.
Now we have completed setup part of the Firebase account. One small step more to go. Create a new Branch (Bucket) named "FirebaseIOT". This branch-name must be equal with our code and Application. So, make sure you have typed it correctly. Now add three tags for storing the temperature, humidity, and LED data. the tag names are temperature, humidity, and led. These tag names should not be false.
Making the appYou don't need any coding experience to make the Android application for this project. We will make the app by drag by scratch. Like we build the LEGO. For this purpose, we use the MIT App Inventor, which is an intuitive, visual programming environment that allows everyone – even children – to build fully functional apps for smartphones and tablets. Those new to MIT App Inventor can have a simple first app up and running in less than 30 minutes. And what's more, our blocks-based tool facilitates the creation of complex, high-impact apps in significantly less time than traditional programming environments. The MIT App Inventor project seeks to democratize software development by empowering all people, especially young people, to move from technology consumption to technology creation.
So, Let's get started with MIT App inventor.
First of all, login with your Google account, accept the terms, and create a new project. Name the project as you like (It doesn't really matter).
Add Firebase option from the Experimental Tab, which is a Non-visible component used to connect our app with the firebase database. Add two buttons and six Text labels (Three in a Row), Use the Horizontal and Vertical arrangements for setting up the layout. I'm not ready to write an essay about setting up the layout. So, please watch our youtube video to understand how to place the labels and also how to use the horizontal and vertical arrangements.
Add Firebase Credentials to the App:
To Add the Firebase credentials, Click on the Firebase DB icon in which you added to your app. You can see a properties tab on the right side of the screen. Here, you have to change the Firebase token and the Firebase URL.
Firebase token is the Database Secret Key, to get the token, click on the Settings on the top left corner, and select Project Settings. Head over to the Service accounts tab and select Database secrets. You can notice that the token will be hidden. Click on the Show button and copy the secret code.
Now we need to provide the Firebase URL. The URL is found on the main page where we created our tags. Click on the Database option under Develop and choose the Realtime database. Copy the URL and paste under the Firebase URL section in the MIT app inventor.
NB: Note this token and the URL, we have to use this code in the Arduino code also.
After adding the Firebase credentials, click on the blocks button at the top right corner. Here we need to give the instructions to the Visible and non-visible components in which we added on the designer part.
We are using the "FirebaseDB1.DataChanged" option to retrieve the data whenever the data is changed in the firebase database, FirebaseDB1 will call the function to fetch the data from the database. This fetched data is stored to the predefined variables with respect to their tag names. We are "FirebaseDB1.StoreValue" to update the state of the LED. When the ON button is pressed, the data will update with 1, and when the OFF button is pressed, the data will update with 0
So, Create the Blocks as shown in the below Screenshot to do all these actions.
The setup is now complete. You can add a custom app icon to your android app. Watch the video to see how to do that. Now you are ready to build your Project. Build your app and test it on your Android device.
Programming NodeMCUOk, now we are ready to program our Microcontroller. Please make sure that you have installed the Arduino IDE and ESP8266 Board files. You need to install three Arduino libraries in order to compile the code for this project.
The first library is for using the Firebase RTDB with our NodeMCU, the second one is for reading the data from the DHT11 Temperature and Humidity Sensor in which we will upload it's data to the database. The third one is the dependency Library for the DHT11 Sensor library. The code will not compile without all these libraries. YOU MUST INSTALL THEM. To install these Libraries, you can choose two methods.
- Direct Installation
I will prefer this method because this is the easiest way. Open your Arduino IDE, click on Tools > Manage Libraries. Now, search for each library and install them.
- Install by Adding Zip library
Download the Library from the GitHub page given above. Open Arduino IDE and go to Sketch > Include Library > Add.ZIP Library and choose the downloaded files.
Open the code in Arduino IDE, You have to add your Firebase and WiFi credentials in the code
#define FIREBASE_HOST "YOUR_FIREBASE_PROJECT.firebaseio.com" //Without http:// or https:// schemes
#define FIREBASE_AUTH "YOUR_FIREBASE_DATABASE_SECRET"
#define WIFI_SSID "YOUR_WIFI_AP"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
The FIREBASE_HOST is the Firebase URL and FIREBASE_AUTH is the Firebase token. We have already copied these two. Paste it on the code.
NB: The Firebase URL shouldn't contain http:// or https://
WIFI_SSID is the name of your WiFi network and WIFI_PASSWORD is the password.
//FirebaseESP8266.h must be included before ESP8266WiFi.h
#include "FirebaseESP8266.h" // Install Firebase ESP8266 library
#include <ESP8266WiFi.h>
#include <DHT.h> // Install DHT11 Library and Adafruit Unified Sensor Library
These three lines will call the Libraries required for this code. Note that "FirebaseESP8266.h" must be included before the "ESP8266WiFi.h" otherwise, the code will show some errors and won't compile.
#define DHTPIN 2 // Connect Data pin of DHT to D2
int led = D5; // Connect LED to D5
These lines of code will define the pin Number in which the DHT sensor and the LED are connected.
I'm not going to explain the code for the DHT sensor. if you want to know more you can head over to the official documentation page from here.
Now let me explain the code we have used in this project for connecting with Firebase RTDB.
FirebaseData firebaseData;
This line will declare the Firebase Data object in the global scope.
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
The first line will initialize the Firebase connection with the provided Firebase HOST and the Token. the second line of code is optional, it will set AP reconnection.
Read DataData at a specific node in Firebase RTDB can be read through these get functions.
The functions included get
, getInt
, getFlot
, getDouble
, getBool
, getString
, getJSON
, getArray
, getBlob
, getFile
.
if (Firebase.getString(ledData, "/FirebaseIOT/led")){
Serial.println(ledData.stringData());
if (ledData.stringData() == "1") {
digitalWrite(led, HIGH);
}
else if (ledData.stringData() == "0"){
digitalWrite(led, LOW);
}
}
Here, we have used getString
to fetch the data from the database. The "if condition" will check the received data and will turn on the led if data is equal to "1" and turn off if data equal to "0".
To store data at a specific node in Firebase RTDB, use these set functions.
The function included set
, setInt
, setFlot
, setDouble
, setBool
, setString
, setJSON
, setArray
, setBlob
and setFile
.
if (Firebase.setFloat(firebaseData, "/FirebaseIOT/temperature", t))
{
Serial.println("PASSED");
Serial.println("PATH: " + firebaseData.dataPath());
Serial.println("TYPE: " + firebaseData.dataType());
Serial.println("ETag: " + firebaseData.ETag());
Serial.println("------------------------------------");
Serial.println();
}
The first line is enough to store the values to the specified tag in the database. The other lines are for debugging purposes so that you can notice any error in the connection to the Firebase DB. The program will give you updates on the serial monitor after every successful update and unsuccessful updates.
In this project, we have used the setFloat
method since the data we are fetched from the sensor is a Float data. "/FirebaseIOT/temperature"
This line denotes the location in which node/tag should the data to be updated.
If you want to learn more deeply, you may check the Official documentation.
Watch our Youtube Videos in case of any doubts.
That's all for this project. Follow us for more interesting projects.
If you have any doubts or suggestions please mail us at pibotsmakerhub@gmail.com or comment down below.
Thank You.
Comments