IOT

IOT with Firebase Connecting NodeMCU with Google Firebase

IOT with Firebase Connecting NodeMCU with Google Firebase

Google Firebase helps developers build real-time applications for the Internet of Things but is not without limitations.

Internet of Things developers share a lot of common implementation requirements across a wide range of IoT applications. Collecting data, delivering low latency content, and securing communications between devices and back-end services are just three of those common needs. while meeting common needs can be challenging at times, IoT development platforms such as Google Firebase provide services that allow developers to meet many of these requirements.
Let’s Get Started With Tutorial :
Here I will begin my large IOT with Firebase Tutorial using Arduino NodeMCU that many people have been asking for. I decided to teach IOT with Firebase through problem-solving. This first video covers basic setup guide for connecting Arduino NodeMCU with Google Firebase which includes step by step configuration of Firebase Console & Arduino IDE, and much more.
This tutorial is light on problems, I hope you enjoy it.

software setup

  1. Install Arduino 1.6.9
  2. Install Arduino ESP8266 core
  3. Download FirebaseArduino library
  4. Start Arduino
  5. Click Sketch > Include Library > Add .ZIP Library...
  6. Choose firebase-arduino-master.zip downloaded in step 3.
Here I will suggest to only use Arduino ide 1.6.9 version on Linux machine then only this tutorial will work like charm otherwise you will get an error.
Now Include firebase library to Arduino ide navigate to : Sketch>Include Library > Add .zip Library > {Library _location}
For checking whether the library is installed or not just navigate to Sketch>Include Library >Manage Library & search for firebase library now we are ready to rock. Let’s run our first example on Arduino just navigate to File>Examples>Firebase Arduino>Firebase_Demo_ESP8266.
Code :
#include <ESP8266WiFi.h
>
#include<FirebaseArduino.h>
#define FIREBASE_HOST “home-fc25a.firebaseio.com” //Your Firebase Project URL goes here without “http:” , “\” and “/”
#define FIREBASE_AUTH “VeZTDftVyMAHF5toTrnSKaGeGV7oVj1OVaXFaE32” //Your Firebase Database Secret goes here
#define WIFI_SSID “raja” //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD “rinku142” //Password of your wifi network

#define Relay1 14 //D5
#define Relay2 12 //D6
#define Relay3 13 //D7
#define Relay4 15 //D8
int rel1,rel2,rel3,rel4;

void setup()
{
Serial.begin(115200); // Select the same baud rate if you want to see the datas on Serial Monitor
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,HIGH);
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);
Firebase.setInt(“S1″,0); //Here the varialbe”FB1″,”FB2″,”FB3” and “FB4” needs to be the one which is used in our Firebase and MIT App Inventor
Firebase.setInt(“S2”,0);
Firebase.setInt(“S3”,0);
Firebase.setInt(“S4”,0);

}
void firebasereconnect()
{
Serial.println(“Trying to reconnect”);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop()
{
if (Firebase.failed())
{
Serial.print(“setting number failed:”);
Serial.println(Firebase.error());
firebasereconnect();
return;
}

rel1=Firebase.getString(“S1”).toInt(); //Reading the value of the varialble Status from the firebase
rel2=Firebase.getString(“S2”).toInt(); //Reading the value of the varialble Status from the firebase
rel3=Firebase.getString(“S3”).toInt(); //Reading the value of the varialble Status from the firebase
rel4=Firebase.getString(“S4”).toInt(); //Reading the value of the varialble Status from the firebase

if(rel1==1) // If, the Status is 1, turn on the Relay1
{
digitalWrite(Relay1,LOW);
Serial.println(“Relay 1 ON”);
}
if(rel1==0) // If, the Status is 0, turn Off the Relay1
{
digitalWrite(Relay1,HIGH);
Serial.println(“Relay 1 OFF”);
}
if(rel2==1) // If, the Status is 1, turn on the Relay2
{
digitalWrite(Relay2,LOW);
Serial.println(“Relay 2 ON”);
}
if(rel2==0) // If, the Status is 0, turn Off the Relay2
{
digitalWrite(Relay2,HIGH);
Serial.println(“Relay 2 OFF”);
}
if(rel3==1) // If, the Status is 1, turn on the Relay3
{
digitalWrite(Relay3,LOW);
Serial.println(“Relay 3 ON”);
}
if(rel3==0) // If, the Status is 0, turn Off the Relay3
{
digitalWrite(Relay3,HIGH);
Serial.println(“Relay 3 OFF”);
}
if(rel4==1) // If, the Status is 1, turn on the Relay4
{
digitalWrite(Relay4,LOW);
Serial.println(“Relay 4 ON”);
}
if(rel4==0) // If, the Status is 0, turn Off the Relay4
{
digitalWrite(Relay4,HIGH);
Serial.println(“Relay 4 OFF”);
}
}

 

Configuration

  1. Start Arduino
  2. Open File > Examples > FirebaseArduino > FirebaseRoom_ESP8266
  3. In:above FirebaseRoom_ESP8266 Replace WIFI_SSID and WIFI_PASSWORD with WiFi credentials
  4. Go to https://firebase.google.com/console/ and create a new Firebase Project
  5. Go to Database
  6. Copy the Database hostname (Database URL without https:// and trailing /)
  7. In:Above code FirebaseRoom_ESP8266 replace FIREBASE_HOST with the Database Hostname
  8. Go to ⚙ > Project Settings > Database > Database secrets
  9. Click Firebase Secrets > Show
  10. Copy the Database Secret
  11. In:FirebaseRoom_ESP8266 Replace FIREBASE_AUTH with Database Secret
  12. Select the board Board > ESP8266 Modules > NodeMCU 1.0
  13. Select the serial port Port > /dev/tty...
  14. Select the upload speed Upload Speed > 115200
  15. Click Sketch > Upload

About the author

Tips Tricks India

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.