mobizt / Firebase-ESP8266

[DEPRECATED] 🔥 Firebase RTDB Arduino Library for ESP8266 and RP2040 Pico. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
411 stars 111 forks source link

HELP WANTED ! mutliple values wanted in multiStreampath #246

Closed ayyysh04 closed 3 years ago

ayyysh04 commented 3 years ago

I am working on a project and wanted a help to this.

image

Here i am streaiming path switch 1 and switch 2 using multipathstreaming. But i wanted to get both the values : pin and status when switch child gets triggered. How can i archive this? should i use json parsing ?or any other method.

My code of multiStreamingPath : FirebaseData firebaseData1;

String parentPath = "/Devices"; String childPath[1] = {"/Room1"};

size_t childPathSize = 1; void handlePin(String path,String value) { if(path == "/Board1/Switch1") { Serial.println("Switch 1 data recieved"); Serial.println(value); } else if(path=="/Board1/Switch2") { Serial.println("Switch 2 data recieved"); Serial.println(value); }

}

void streamCallback(MultiPathStreamData stream) { Serial.println(); Serial.println("Stream Data1 available");

size_t numChild = sizeof(childPath) / sizeof(childPath[0]);

for (size_t i = 0; i < numChild; i++) { if (stream.get(childPath[i])) { Serial.println("stream "); Serial.println(stream.get(childPath[i]));

Serial.println("path: "+ stream.dataPath + ", type: " + stream.type + ",value: " + stream.value);

//Custom function to make pin high and low handlePin(stream.dataPath,stream.value); } }

Serial.println(); }

void streamTimeoutCallback(bool timeout) { if (timeout) Serial.println("stream timeout, resuming...\n"); }

void setup() {

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

//Check stream is working or not if (!Firebase.beginMultiPathStream(firebaseData1, parentPath,childPath,childPathSize)) { Serial.println("cant begin stream connection\n"); Serial.println("Reason: "+firebaseData1.errorReason()); }

Firebase.setMultiPathStreamCallback(firebaseData1,streamCallback,streamTimeoutCallback); } void loop() { }

mobizt commented 3 years ago

You should assign the child path values in childPath array as what you want to get the value e.g. Room1/Switch1 and Room1/Switch2

You can also use normal stream and parse the data under /Devices manually.

Multistream is only parse the value for you which childPath used as the key to parse value from stream json payload.

ayyysh04 commented 3 years ago

Can stream.value return a json??

mobizt commented 3 years ago

Why don't test yourself?

mobizt commented 3 years ago

You should learn how Firebase works before using tool or sdk or library.

ayyysh04 commented 3 years ago

No, actually i over used my nodemcu and its not working right now

mobizt commented 3 years ago

There are many examples and documentation.

This is the repository for code related issue not for getting started to Firebase.

If you ever use or program any Firebase client app before, you will understand how to use the library.

ayyysh04 commented 3 years ago

ok thanks,i will get back to this after my new nodemcu comes

mobizt commented 3 years ago

You should use community forum for general help. I'm trying to support and spend the time but you should try it first and read the document related to what you're working.

ayyysh04 commented 3 years ago

ahh really sorry for that.

ayyysh04 commented 3 years ago

I am trying to get the json data from multipatahstream object in handlepin() function, but i cant find function for the same. database: image

Json getting from stream : image

My code :

include

include

define FIREBASE_HOST ""

define FIREBASE_AUTH ""

/Define the WiFi credentials /

define WIFI_SSID "WIFI_AP"

define WIFI_PASSWORD "WIFI_PASSWORD"

FirebaseData firebaseData1;

String parentPath = "/Devices"; String childPath[1] = {"/Room 1"};

size_t childPathSize = 1;

void handlePin(String path,FirebaseJson &value) { if(path == "/Board1/Switch1") { Serial.println("Switch 1 data recieved");
FirebaseJsonData result; value.get(result,"Board1/Switch1"); Serial.println(result.stringValue); } else if(path=="/Board1/Switch2")
{ Serial.println("Switch 2 data recieved");
FirebaseJsonData result; value.get(result,"Board1/Switch2"); Serial.println(result.stringValue); }

}

void streamCallback(MultiPathStreamData stream) { Serial.println(); Serial.println("Stream Data1 available");

size_t numChild = sizeof(childPath) / sizeof(childPath[0]);

for (size_t i = 0; i < numChild; i++) { if (stream.get(childPath[i])) { Serial.println("stream "); Serial.println(stream.get(childPath[i]));

  Serial.println("path: "+ stream.dataPath + ", type: " + stream.type + ",value: " + stream.value);

  //Custom function to make pin high and low
  handlePin(stream.dataPath,stream);
}

}

Serial.println(); }

void streamTimeoutCallback(bool timeout) { if (timeout) Serial.println("stream timeout, resuming...\n");
}

void setup() {

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

//Check stream is working or not if (!Firebase.beginMultiPathStream(firebaseData1, parentPath,childPath,childPathSize)) { Serial.println("cant begin stream connection\n"); Serial.println("Reason: "+firebaseData1.errorReason()); }

Firebase.setMultiPathStreamCallback(firebaseData1,streamCallback,streamTimeoutCallback); } void loop() { }

mobizt commented 3 years ago

Because you don't get what I mentioned above.

You should do this.

String childPath[2] = {"/Board1/Switch1", "/Board1/Switch2"};

The childPath array contains the children paths that you want to listen the value changes.

ayyysh04 commented 3 years ago

But i want to parse it as json ,because i will be adding more data under Switch . I think its not possible ,as stream.value gives String.

I think single child streaming will work?

mobizt commented 3 years ago

You can't do with multipath stream, use normal stream instead.

The multipath stream internally parses the values from json payload and don't obtain the json payload as this is the design of this function and you can use normal stream and handle payload yourself.

mobizt commented 3 years ago

stop using multipath stream and use normal stream is your solution.

ayyysh04 commented 3 years ago

yup i got that,thanks Working on it