Google Firebase with webhooks tutorial


#29

@rickkas7 the WRITE to Firebase works perfect, but when I try to read it back in and do the jsonBuffer.parseObject my int fields appear to be parsed and come into the new variables, but the bool and double types are not being parsed into their variables.


//New Parameter Variables
bool    newaug = false;
int     newCycle = 20.0;
bool    newfan = false;
bool    newign = false;
double  newLReadPgm;
double  newLReadWeb;
double  newLWritten;
char    newmode[9] = "Off"; 
int     newPB = 60.0;
int     newPMode = 2.0;
bool    newpgm = false;  
double  newPToggle;
double  newtarget = 80;  
int     newTd = 45.0;
int     newTi = 180;
double  newu = 0.15;

void getDataHandler(const char *topic, const char *data)
{
    StaticJsonBuffer<768> jsonBuffer;
    char *mutableCopy = strdup(data);
    JsonObject& root = jsonBuffer.parseObject(mutableCopy);
    Serial.printf("data: %s\r\n", data);
    free(mutableCopy);
 
    if(!root.success())
    {
        Serial.println("parse failed");
    }
    else
    {
        newCycle =      root["Cycle"];
        newLReadPgm =   root["LReadPgm"];
        newLReadWeb =   root["LReadWeb"];
        newLWritten =   root["LWritten"];
        newPB =         root["PB"];
        newPMode =      root["PMode"];
        newPToggle =    root["PToggle"];
        newTd =         root["Td"];
        newTi =         root["Ti"];
        newaug =        root["aug"];
        newfan =        root["fan"];
        newign =        root["ign"];
        strcpy(newmode, (const char*)root["mode"]);
        newpgm =        root["pgm"];
        newtarget =     root["target"];
        newu =          root["u"];

        Serial.printf("Read Parameters from Firebase: %d %.3f %.3f %.3f %d %d %.3f %d %d %d %d %d %s %d %.0f %.2f\r\n", newCycle, newLReadPgm, newLReadWeb, newLWritten, newPB, newPMode, newPToggle, newTd, newTi, newaug, newfan, newign, newmode, newpgm, newtarget, newu);
    }

here’s the value in data field from my webhook read

data: {"Cycle":60,"LReadPgm":1488858654,"LReadWeb":1488858710,"LWritten":1488858654,"PB":60,"PMode":2,"PToggle":0,"Td":45,"Ti":180,"aug":1,"fan":1,"ign":1,"mode":"Start","pgm":0,"target":90,"u":0.25}

and here’s the output from the Serial.printf at the end of my code snip

Read Parameters from Firebase: 60 0.000 0.000 0.000 60 2 0.000 45 180 0 0 0 Start 0 0 0.25

do I need something in my else statement syntax to assign the values from root


#30

This appears to be a bug/non-feature of SparkJson. I originally got 0 values for LReadPgm and LReadWeb.

Then I modified the source data to make them actually decimal values by adding a “.0” to the end, then it worked.

It seems kind of silly to me that it won’t coerse an integer to a double, but it won’t.

#include "Particle.h"
#include "SparkJson/SparkJson.h"

const char *rawJsonData = "{\"Cycle\":60,\"LReadPgm\":1488858654.0,\"LReadWeb\":1488858710.0,\"LWritten\":1488858654,\"PB\":60,\"PMode\":2,\"PToggle\":0,\"Td\":45,\"Ti\":180,\"aug\":1,\"fan\":1,\"ign\":1,\"mode\":\"Start\",\"pgm\":0,\"target\":90,\"u\":0.25}";

void parseData(char *data); // forward declaration

void setup() {
	Serial.begin(9600);

	delay(5000);
	char *dataCopy = strdup(rawJsonData);
	parseData(dataCopy);
	free(dataCopy);

}

void loop() {

}

void parseData(char *mutableData) {
	StaticJsonBuffer<1000> jsonBuffer;
	JsonObject& root = jsonBuffer.parseObject(mutableData);

	if (!root.success()) {
		Serial.println("parseObject() failed");
	    return;
	}

	int cycle = root["Cycle"];
	double  newLReadPgm = root["LReadPgm"];
	double  newLReadWeb = root["LReadWeb"];

	Serial.printlnf("cycle=%d newLReadPgm=%lf newLReadWeb=%lf", cycle, newLReadPgm, newLReadWeb);
}

Output:

cycle=60 newLReadPgm=1488858654.000000 newLReadWeb=1488858710.000000

#31

@rickkas7 I was going through your tutorial on Github just today. Thanks for the write up, which is pretty straightforward.

Looks like Firebase console changed the location of the Database Secrets section since you last made edits to your GitHub tutorial. If anyone like me is reading it, they might find it useful that Database Secrets is now located in Settings > Service Accounts > Database Secrets.

I haven’t completely gone through the tutorial yet, will let you know if I see any further problems using the deprecated Database Secerts.

EDIT: I now have Firebase working with values as Strings in less than an hour. Now time to work the JSON body tag to get values to integers/float (which I’m now using your other guide for). Thanks again!

Cheers


#32

@rickkas7 Hi, referring back to this thread, is there a way to actually write BOOL out to firebase as true / false and read it back in as true / false without treating it as a string. I’ve done this before in RPi/Python/Firebase, but can’t seem to figure it out for my current project with webhooks in Photon/C++/Firebase.


#33

There’s a new section at the end of this tutorial on using the webhook body feature to do true booleans and numbers:

It’s explained in more detail here:

Rick


#34

I’ve looked at that but can’t find a snprintf % ref for bool, I’m using %d and getting a 1 or 0 in Firebase but not a True or False. Do I have to code this in the JSON body format?


#35

bool is only supported as numeric value via %d which would render 1 or 0 for true or false
But if you want to have the literals true or false in your string, you can do this

  snprintf(buf, sizeof(buf), "This bool is %s", yourBool ? "true" : "false");

#37

I generated a library based on the firebase-webhooks tutorial:
It needs some work but maybe it can be of value as a starting point. Note I’m new both with Photon and C++ so be gentle :wink:

Usage:

firebase = new Firebase("test3rdata");
firebase->subscribe();
firebase->setCallback(readCallback);

And:

firebase->readData();
firebase->publishData("test1data", "{\"a\":10}");

Where the callback receives a JsonObject:

void readCallback(JsonObject& root) {
	r = root["r"];
	Serial.printlnf("Yes! r=%d", r);
   }

#38

@rickkas7
Hello Rickkas7.
I have an issue that i am facing in the webhook to get the data from firebase.
So, I have two photons and I have stored the data in my firebase DB with the particledevice id.json .
Now im using a get webhook to fetch data from firebase based on the device that is calling,ie: I should get the data of that particular photon only. But when i substitute the value {{{coreid}}} or {{{PARTICLE_DEVICE_ID}}} to get the data it sends back null. when i hardcode my particle id in the url, it sends the correct data. Somehow i think there is an issue with the substitution part in url.
Could you please help me with this.
Thank you!:slight_smile:


#39

The data sent to the webhook (including device ID) aren’t automatically forwarded to your destination server. But even if you explicitly send them to the server, it would be up to the server to actively embed it in the response.
3rd party servers usually don’t do that.

A webhook call is not an atomic transaction. You have the iniating call - which can be pushed from any device - as one transaction and a sencond trasaction where the server reports back to the cloud which in turn - now possibly without knowledge of the initiator, as outlined above - pushes an event to all subscribers.

If you want to have the device ID reliably embedded in the response “event” message you may want to consider appending the device ID to the event name.


#40

@rickkas7 Thanks for taking the time to make the tutorial. It seemed like it would be straight forward. Unfortunately I think Google’s changed a couple things that add a couple hurdles.

First, I couldn’t even create a project. I found out after looking around that console was having issues and wouldn’t let anyone make any new projects for about the one hour I happened to try. Bad luck on my timing I guess. https://status.firebase.google.com/ <- Check here if you’re having problems. If you see red, go to the bar for a couple hours and come back.

I waited a day and it worked, but then I had 10 blank projects. Deleting them was a little hidden. In the project settings in the project overview, after scrolling to the bottom, in light gray, there is a “Delete Project” link.

More importantly, the database wouldn’t work. Any time I tried to get into it I was given an error. I found that if I created an app in the Project Overview section and filled it with junk, then the database tab worked for me.

The Database view didn’t look right either. At the top I noticed I had the option to change the Database from “Cloud Firestore BETA” to “Realtime Database.” Then it was looking like the tutorial.

From there I grabbed your hook.json example and followed along. I got the Particle CLI publish command to put data into my Firebase database just like you showed. I think I’m on my way to having my AssetTracker put data somewhere it is stored at all times now thanks to you.

I’m still surprised that there isn’t some sort of easier/prepackaged way to store published events without being logged into the console and waiting for them to come along. I feel like I’m missing a tutorial on something.


#41

I was looking through this tutorial and seems that the part where you get the secret has changed drastically.

Cheers,


#42

I followed the tutorial and of course struggled getting the Particle CLI installed on this new Mac.

Once I got Particle CLI working I was able to create the WebHook:

{
    "event": "Environment Read",
    "url": "hsdfgsdfgsdfgsdfgsdfgsdfg.com/environmentdata/data.json",
    "requestType": "POST",
    "query": {
        "auth": "rfdgsdfgsdfgsdfgsdfgsdfgsdfgsdfgsdfg"
    },
    "json": {
        "Temperature": "{{temp}}",
        "Humididty": "{{humidity}}",
        "Voltage": "{{voltage}}",
        "Device": "{{device}}",
        "ts": "{{PARTICLE_PUBLISHED_AT}}"
    },
    "mydevices": true,
    "noDefaults": true
}

Which gives me this in the database:

Makes me wonder why the data didn’t go into the right fields. You’ll notice the empty strings.

Cheers,


#43

Hey @rickkas7,

We were wondering if this tutorial is only for the real-time database or if it also works for the new Cloud Firestore. We followed the tutorial, and we managed to send data to the real-time database, but we have not figured out how to send data to the Cloud Firestore.

We want to use the Firestore because it seems it has better sorting and searching capabilities than the real-time database.

Thanks for the help!


#44

Why the getHandler() event is not inside void loop() ?


#45

What are you refering to?
An event would never be anywhere but “come” from outside.
If you mean a function call, where do you see the getHandler() call?
There are lots of posts in this thread that don’t contain any code whatsoever, so if you could quote the post you are refering to answering would be much easier.


#46

I tried to publish values to Firebase integration but only time field is updated else every other data field in firebase is blank.
I created the webhook as shown below
{
“event”: “Send Data”,
“url”: “https://xxxxxxxxxxxxxxxx.firebaseio.com/Senddata/data.json”,
“requestType”: “PUT”,
“query”: {
“auth”: “your auth token”
},
“json”: {
“Temperature”: “{{temp}}”,
“Humidity”: “{{humidity}}”,
“Voltage”: “{{voltage}}”,
“Device”: “{{device}}”,
“ts”: “{{PARTICLE_PUBLISHED_AT}}”
},
“mydevices”: true,
“noDefaults”: true
}

Test data:
Send Data
“{“Temperature”:31,“Humidity”:85,“Voltage”:3}”

Result
hook-response/Send Data/0
{“Device”:"",“Humidity”:"",“Temperature”:"",“Voltage”:"",“ts”:“2019-08-11T06:24:16.427Z”}


#47

The part in {{ }} must match the keys in the JSON data you send from the device or test data.

In other words:

  • {{temp}} should be {{Temperature}}
  • {{humidty}} should be {{Humidity}}
  • {{voltage}} should be {{Voltage}}
  • {{device}} isn’t in the JSON data. Did you mean {{PARTICLE_DEVICE_ID}}

#48

Thank You I understood my mistake, now its working fine.


#49

Is there a way to read a list of data like stock prices from Google Sheets(after publishing the sheet then converting into json format using gsx2json) and display on I2C 16 x 2 lcd display.
Would you please write a demo code for me or make a tutorial on this.:thinking::thinking::pleading_face::pleading_face:


Particle Argon Lcd