More events in one Webhook (SOLVED)

I’m working on a workshop where we connect a couple of photons to SAP HCP (HANA Cloud Platform).

At first I tried this all with a Core. After some digging into this community I got it to work. But then I needed to buy a couple of new boards and the Core’s were not available anymore, so I moved to Photon’s. No issue so far :smile: One Photon is working fine with the DHT11.

So what did I do…

  • bought 8 Photon’s, 8 DHT11’s a couple of wires and resistors. Let’s get the party started!

  • Registered all the photon’s on one user ID

  • Created the code (please don’t judge my lack of coding)

    #include “Adafruit_DHT/Adafruit_DHT.h”

    #define DHTPIN 3
    #define DHTTYPE DHT11

    DHT dht(DHTPIN, DHTTYPE);
    char resultStr[260];

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

    void loop() {

     *//float tempHum = dht.getHumidity();*
    	*float tempVal = dht.getTempCelcius();*
    
     *sprintf(resultStr, "{\"HCP_VAL\": %f}", tempVal);*
     *Spark.publish("post_hcptemp_01_01", resultStr, 60, PRIVATE);*
    
     *delay(10000);*
    

    }

  • Created the webhook

{
“event”: “post_hcptemp_01_01”,
“url”: “https://url######”,
“requestType”: “POST”,
“headers” : {
“Authorization”: “Bearer ########”,
“Content-Type” : “application/json”
},
“json” : {
“mode”:“sync”,
“messageType”:“m0t0y0p0e1”,
“messages”:[{“sensor”:“temperature”,“value”:"{{HCP_VAL}}",“timestamp”:1413191650}]
},
“mydevices” : true
}

But then i ran into a tiny little issue. 4 Users are working on 8 particles (so every photon should be posting data to 4 different url’s with different auth bearers). That would bring us to 32 webhooks on one account. And that isn’t going to work. Why not? Well…Failed to create, server said Too many web hooks for this device
Potentially unhandled rejection [1] {“ok”:false,“error”:"Too many web hooks for
this device"} (WARNING: non-Error used) You are alloud to create 10 hooks.

So I thought…what if I push it all into one webhook. So, 32 events in one webhook and the publish event will differ for every user.

But how? Anyone got a clue on how I should put 32 events into one webhook?

No worries…already fixed it. Answering my own question, but who know maybe someone will run into the same issue/idea :smile:

{
“event”: “post_hcptemp_01_01”,
“url”: “https://url######”,
“requestType”: “POST”,
“headers” : {
“Authorization”: “Bearer ########”,
“Content-Type” : “application/json”
},
“json” : {
“mode”:“sync”,
“messageType”:“m0t0y0p0e1”,
“messages”:[{“sensor”:“luchtvochtigheid”,“value”:"{{HCP_VAL}}",“timestamp”:1413191650}]
},
“mydevices” : true,

“event”: “post_hcptemp_01_02”,
“url”: “https://url######”,
“requestType”: “POST”,
“headers” : {
“Authorization”: “Bearer ########”,
“Content-Type” : “application/json”
},
“json” : {
“mode”:“sync”,
“messageType”:“m0t0y0p0e1”,
“messages”:[{“sensor”:“luchtvochtigheid”,“value”:"{{HCP_VAL}}",“timestamp”:1413191650}]
},
“mydevices” : true
}

My guess would be that only the second event is actually triggering the webhook, since they both share the “event” key in that JSON dictionary. Have you tested if post_hcptemp_01_01 is doing anything?

Thats correct. Only the last event is being created in the webhook. :frowning:

You could maybe work around the limitation by letting each user claim the Photons they’re posting events from.

From a logical standpoint, it sounds you are better off consolidating to one type of event+hook with some distinguishing information in the JSON body (like username/device ID and subevent type). If the URLs really need to be different, perhaps you can have a middleman server that relays events based on this data to the right URLs.

Ah, looks like there is an option that will let you generate URLs based on data passed into the publish function:

https://docs.particle.io/guide/tools-and-features/webhooks/#custom-template-variables

So you could do something like:

Spark.publish("my_var", "{ \"custom-url\": \"123456\" }", 60, PRIVATE);

With a webhook definition of:

{
    "event": "my_var",
    "url": "http://my-awesome-website.particle/{{custom-url}}",
    "requestType": "POST",
    "json": {
        "my-temp": "{{my-temp}}",
        "source": "{{SPARK_CORE_ID}}"
    },
    "mydevices": true
}

I don’t see any reason why you couldn’t even just replace the entire url with “{{custon-url}}” and post webhooks completely dynamically if you wanted to, unless Particle imposes some character limitations on variable substitution.

Registering the photons onto different users would be a quick solution, but not really a pretty one since I don’t want to hassle the users with registration. I would like to maintain them from one email adress. The URL’s are all differing because they are related to the so call S-user at SAP. This username is located within the URL. I also tried to push a part (the username) of the URL into the coding, but I wasn’t able to get that working as well. That might be a solution, but how?!?! Having a server in the middle would be a solution as well but that would also be another workaround and too much trouble for a workshop.

None the less, I appreciate the thinking!

It should only be a small part of the URL, so that should be the solution. Back to coding here.

I’m also just now working on posting logs to a Slack channel with a configurable URL as the post destination, so hopefully I’ll have a working example soon.

Ok, I was able to get a Photon posting to a dynamic webhook URL (this particular example is for posting to a Slack channel):

Code:

Spark.publish("log_info", "{\"my-log\": \"Yo!\", \"my-url\":\"a/b/c\"}", 60);

Webhook definition:

{
    "eventName": "log_",
    "url": "https://hooks.slack.com/services/{{my-url}}",
    "requestType": "POST",
    "json": {
    	"username": "{{SPARK_CORE_ID}}",
        "text": "{{my-log}}"
    }
}

Hope that works for you, too!

3 Likes

That did it. Thanks a lot. There were some other variable that were bugging me. Variable URLs, bearers, etc. That should solve the it!

Thanks!

Fixed it totally:

I had been on the right track from the beginning, but the quotes were in the wrong place. %s should have been “%s” and for that reason the user did’nt end up in my URL. So now I’ve pushed in the user and device id, that are part of the URL, in there and the bearer oAuth token as well. Thanks a lot for the help @indraastra!

sprintf(resultStr, “{“HCP_USER”: “%s”, “HCP_BEARER”: “%s”, “HCP_DEVICEID”: “%s”, “HCP_SENSOR”: “Temperature”, “HCP_VAL”: %f}”, hcp_user, hcp_bearer, hcp_device, tempTmp);
Spark.publish(“post_to_hcp”, resultStr, 60, PRIVATE);