IOT with Firebase is trending now days, as it also known as Server less IOT and many of businesses selling their IOT products with backend as Firebase database. It’s really a good choice as using firebase no need to manage database, authentication, push notifications, you get all under one cloud. Firebase use backend of Google Cloud : Google cloud computing services (GCP).
Firebase Realtime Database Arduino Library for ESP8266
Google’s Firebase Realtime Database Arduino Library for ESP8266 v 2.9.1
This library supports ESP8266 MCU from Espressif. The following are platforms in which libraries are also available.
Tested Devices
- Wemos D1 Mini
- NodeMCU
- ESP-12F
- LinkNode
Features
-
Supports Read (get), Store (set), Append (push), Patch (update) and Delete Data
-
Supports Primitive data types: Integer, Float, Double, Boolean, String and JSON.
-
Supports BLOB and File Stream Data.
-
Support Read and Write Database Rules.
-
Supports ETag, Priority, Data Limits, Timestamp, Filtering, etc.
-
Supports Stream Event Callbacks
-
Supports Multiple paths Stream (under the same parent node)
-
Supports Data Backup and Restore.
-
Supports Firebase Cloud Messaging.
-
Supports SD and flash memory’s CA certificate file (for Core SDK v2.5.x).
-
Built-in easiest and non-recursive JSON parser and builder.
-
-
Setup Realtime Database Configuration
-
Install Firebase ESP8266 library.
-
Navigate to Basic example
-
Replace the firebase configs are directed in above video
-
Burn the code to ESP8266
-
Analyze the real-time values in Firebase changed by ESP8266.
-
Enjoy
#include <ESP8266WiFi.h>
#include "FirebaseESP8266.h"
#define FIREBASE_HOST "fir-----.firebaseio.com" //Change to your Firebase RTDB project ID e.g. Your_Project_ID.firebaseio.com
#define FIREBASE_AUTH "emSxQPgquE--------OpiGE7mwbVT2" //Change to your Firebase RTDB secret password
#define WIFI_SSID "k***in"
#define WIFI_PASSWORD "1****1321"
//Define Firebase Data objects
FirebaseData firebaseData1;
FirebaseData firebaseData2;
const int ledPin = 4; //GPIO4 or D2 for LED
const int swPin = 5; //GPIO5 or D1 for Switch
bool swState = false;
String path = "/Nodes";
String nodeID = "Node2"; //This is this node ID to receive control
String otherNodeID = "Node1"; //This is other node ID to control
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT);
Serial.println();
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
if (!Firebase.beginStream(firebaseData1, path + "/" + nodeID))
{
Serial.println("Could not begin stream");
Serial.println("REASON: " + firebaseData1.errorReason());
Serial.println();
}
}
void loop()
{
if (!Firebase.readStream(firebaseData1))
{
Serial.println();
Serial.println("Can't read stream data");
Serial.println("REASON: " + firebaseData1.errorReason());
Serial.println();
}
if (firebaseData1.streamTimeout())
{
Serial.println();
Serial.println("Stream timeout, resume streaming...");
Serial.println();
}
if (firebaseData1.streamAvailable())
{
if (firebaseData1.dataType() == "boolean")
{
if (firebaseData1.boolData())
Serial.println("Set " + nodeID + " to High");
else
Serial.println("Set " + nodeID + " to Low");
digitalWrite(ledPin, firebaseData1.boolData());
}
}
if (digitalRead(swPin) != swState)
{
bool _swState = swState;
swState = digitalRead(swPin);
if (Firebase.setBool(firebaseData2, path + "/" + otherNodeID, swState))
{
if (swState)
Serial.println("Set " + otherNodeID + " to High");
else
Serial.println("Set " + otherNodeID + " to Low");
}
else
{
swState = _swState;
Serial.println("Could not set " + otherNodeID);
}
}
}