Streaming values of the firebase database to Arduino Node MCU is one of the best features I personally like in the NEW Firebase ESP8266 library. IOT firebase streaming with esp8266/esp32 helps in reducing code complexity for realtime monitoring firebase values, for single or multiple nodes in the firebase database.

IoT Firebase Streaming values to Esp8266

Firebase streaming functionality comes into the picture when you want to do a quick action as soon as the value changes in the firebase database console. Let me explain with an example: Let’s suppose you have 3 nodes in the firebase database named: Switch 1,2,3. Now if we want to control esp8226 i/o pins as soon as any of the node changes. Now there are two approaches we can follow in this scenario.

First One is a traditional and lengthy approach where we can add each of the firebase nodes in the void loop() function. Now loop will infinitely check for each node and we need to manually add an IF condition to check if the value is changed.

Are you tired of this First approach? No worries! Let me explain the second approach. So in the second approach, we just need to declare all paths in variable and initialize streaming on them. That’s it, now you will get a realtime callback if a value changes for any of the node/switches in the firebase database. Are you still wondering how the firebase value is changed? Well, I have already explained in the previous post for changing firebase values using a custom android app.

Firebase Streaming Code Snippet/ Transcript

Code

#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>

#define FIREBASE_HOST "esri-ee----baseio.com" //Without http:// or https:// schemes
#define FIREBASE_AUTH "qULRQbnu----pXic1UQ1FxHvM"
#define WIFI_SSID "kavin"
#define WIFI_PASSWORD "1123---321"

//Define FirebaseESP8266 data object
FirebaseData firebaseData;


unsigned long sendDataPrevMillis = 0;

String path = "/on";



void printResult(FirebaseData &data);

void setup()
{
  pinMode(4, OUTPUT);
  Serial.begin(115200);

  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);

  //Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
  firebaseData.setBSSLBufferSize(1024, 1024);

  //Set the size of HTTP response buffers in the case where we want to work with large data.
  firebaseData.setResponseSize(1024);

  if (!Firebase.beginStream(firebaseData, path))
  {
    Serial.println("------------------------------------");
    Serial.println("Can't begin stream connection...");
    Serial.println("REASON: " + firebaseData.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
}

void loop()
{
  


  if (!Firebase.readStream(firebaseData))
  {
    Serial.println("------------------------------------");
    Serial.println("Can't read stream data...");
    Serial.println("REASON: " + firebaseData.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }

  if (firebaseData.streamTimeout())
  {
    Serial.println("Stream timeout, resume streaming...");
    Serial.println();
  }

  if (firebaseData.streamAvailable())
  {
    Serial.println("------------------------------------");
    Serial.println("Stream Data available... Printing");
    Serial.println("STREAM PATH: " + firebaseData.streamPath());
    Serial.println("EVENT PATH: " + firebaseData.dataPath());
    Serial.println("DATA TYPE: " + firebaseData.dataType());
    Serial.println("EVENT TYPE: " + firebaseData.eventType());
    Serial.print("VALUE: ");
    printResult(firebaseData);
    if(firebaseData.stringData()== "1"){
      digitalWrite(4,HIGH);
      }
      else{
        digitalWrite(4,LOW);
        }
    Serial.println("------------------------------------");
    Serial.println();
  }

  
}

void printResult(FirebaseData &data)
{
  
  if (data.dataType() == "int")
    Serial.println(data.intData());
  else if (data.dataType() == "float")
    Serial.println(data.floatData(), 5);
  else if (data.dataType() == "double")
    printf("%.9lf\n", data.doubleData());
  else if (data.dataType() == "boolean")
    Serial.println(data.boolData() == 1 ? "true" : "false");
  else if (data.dataType() == "string")
    Serial.println(data.stringData());
    
  else if (data.dataType() == "json")
  {
    Serial.println();
    FirebaseJson &json = data.jsonObject();
    //Print all object data
    Serial.println("Pretty printed JSON data:");
    String jsonStr;
    json.toString(jsonStr, true);
    Serial.println(jsonStr);
    Serial.println();
    Serial.println("Iterate JSON data:");
    Serial.println();
    size_t len = json.iteratorBegin();
    String key, value = "";
    int type = 0;
    for (size_t i = 0; i < len; i++)
    {
      json.iteratorGet(i, type, key, value);
      Serial.print(i);
      Serial.print(", ");
      Serial.print("Type: ");
      Serial.print(type == JSON_OBJECT ? "object" : "array");
      if (type == JSON_OBJECT)
      {
        Serial.print(", Key: ");
        Serial.print(key);
      }
      Serial.print(", Value: ");
      Serial.println(value);
    }
    json.iteratorEnd();
  }
  else if (data.dataType() == "array")
  {
    Serial.println();
    //get array data from FirebaseData using FirebaseJsonArray object
    FirebaseJsonArray &arr = data.jsonArray();
    //Print all array values
    Serial.println("Pretty printed Array:");
    String arrStr;
    arr.toString(arrStr, true);
    Serial.println(arrStr);
    Serial.println();
    Serial.println("Iterate array values:");
    Serial.println();
    for (size_t i = 0; i < arr.size(); i++)
    {
      Serial.print(i);
      Serial.print(", Value: ");

      FirebaseJsonData &jsonData = data.jsonData();
      //Get the result data from FirebaseJsonArray object
      arr.get(jsonData, i);
      if (jsonData.typeNum == JSON_BOOL)
        Serial.println(jsonData.boolValue ? "true" : "false");
      else if (jsonData.typeNum == JSON_INT)
        Serial.println(jsonData.intValue);
      else if (jsonData.typeNum == JSON_DOUBLE)
        printf("%.9lf\n", jsonData.doubleValue);
      else if (jsonData.typeNum == JSON_STRING ||
               jsonData.typeNum == JSON_NULL ||
               jsonData.typeNum == JSON_OBJECT ||
               jsonData.typeNum == JSON_ARRAY)
        Serial.println(jsonData.stringValue);
    }
  }
}

Conclusion

So, guys, I hope from the above video explanation and code transcript you would be able to understand the concept of firebase value streaming and it’s benefits. I think it will much better to see how firebase streaming works on multiple paths and how node MCU esp8266 behaves with it. Let’s make it part of the next post.

Happy IOT Firebase Streaming !!

Categorized in: