Google Firebase with webhooks tutorial


#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
#50

Guys, how do I send data like name and id to firebase using webhook?

String name = "Mira";
String serial = "3B D4 DD 0D";

void sendInfo()
{
      char data[256];
      snprintf(data, sizeof(data), "{\"Name\": %s, \"Serial\": %s}" , name.c_str(), serial.c_str());
      Particle.publish("FirebaseRFID", data, PRIVATE);  //Store data to firebase
}

I can see the event in particle console, but nothing from webhook.
I guess the format is wrong, but don’t know the right way to write them…


#51

Have you read through the thread and followed the steps shown?


#52

yes, I can send the string converted from integer like temperature value etc (like example below), but cannot if the data is already in string.

   snprintf(data, sizeof(data), "{\"temp\":%.2f, \"Air Index\":%.2f, \"humid\":%.2f}" , t, airNo, h);
   Particle.publish("Firebase", data, PRIVATE); 

#53

If it’s a string the JSON should warp it in double quotes.