Closed ayyysh04 closed 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.
Can stream.value return a json??
Why don't test yourself?
You should learn how Firebase works before using tool or sdk or library.
No, actually i over used my nodemcu and its not working right now
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.
ok thanks,i will get back to this after my new nodemcu comes
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.
ahh really sorry for that.
I am trying to get the json data from multipatahstream object in handlepin() function, but i cant find function for the same. database:
Json getting from stream :
My code :
/Define the WiFi credentials /
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() { }
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.
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?
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.
stop using multipath stream and use normal stream is your solution.
yup i got that,thanks Working on it
I am working on a project and wanted a help to this.
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() { }