Webhook POST help

Hi,

I’m trying to get webhooks to work and can’t seem to figure it out. I’ve read through the documentation several times and it is sadly lacking in examples.

The way I am set up is that every so often, my electron will do a Particle.publish() with current weather data.

The data is simply a bunch of measurements from several sensors in a comma separated list like this:

event: w
data: {"data":"1456880862,41.2083,-73.3439,86%,3.98V,19.8C,29.9%,971.6hPa,0.008059,","ttl":"60","published_at":"2016-03-02T01:07:48.560Z","coreid":"inserCoreIdHere"}

I need to build a webhook that can take this and forward it to an external api for further processing (probably something like AWS Lambda via AWS API Gateway).

Any help would be appreciated.

1 Like

Hello 75th smd junior,

Create a text file with the following code modified of course to suit your data and save somewhere (let’s call it my-webhook.json:

{
    "event":  "send-measurements",
    "url": "https://example.com/measurements",
    "requestType": "POST",
    "mydevices": "true",
    "json": {
        "measurements": {
            "sensor1": "{{data1}}",
            "sensor2": "{{data2}}"
        }
    },
    "noDefaults": true
}

Then execute the following command on your computer:

particle webhook create my-webhook.json

Add the following to your setup function in your firmware:

Particle.subscribe("hook-response/send-measurements", receivedMeasurementsResponse, MY_DEVICES);

Call this every time you want to send measurements, where data1Float and data2Float are your floating measurement numbers:

String data = String(
        "{\"data1\":" + String(data1Float, 3) +
        ",\"data2\":" + String(data2Float, 3) + "}");

Particle.publish("send-measurements", data, 60, PRIVATE);

Create following function in your firmware to catch response:

void receivedMeasurementsResponse(const char *event, const char *data)
{
 // Received my data! Now do something...
}

Read this for more details about arguments into the template:
Particle Webhooks

1 Like

A minor side note:

A "nicer" (less heap fragmenting and shorter) way to build your string would be

String data = String::format("{\"data1\":%4.3f,\"data2\":%4.3f}", data1Float, data2Float);

With printf formatting, like this, you'll be able to do your string building a bit more concise
http://www.cplusplus.com/reference/cstdio/printf/

3 Likes

I agree, thanks ScruffR.

So just for testing I have simply modified my original Particle.publish into a JSON string (I think). It will send the whole thing as a single JSON element (which was the original plan anyway to save data, as it will bw sending data very frequently).

This is the Particle.publish() comman at the moment (please ignore how messy and ugly it is right now, I plan to make this MUCH nicer once I get everything working):

Particle.publish("w", "{\"data1\":" + getGpsDateTime() + "," + getGpsLoc() + "," + fuelSoCStr + "%," + fuelVoltStr + "V," + tempStr + "C," + getHumPres() + "," + getUvIndex() + "}");

And this is my weather.json file:

{
    "event":  "w",
    "url": "http://requestb.in/1l7tujd1",
    "requestType": "POST",
    "mydevices": "true",
    "json": {
        "measurements": {
            "sensor1": "{{data1}}"
        }
    },
    "noDefaults": true
}

And this is what is being returned in dashboard.particle.io (I’m monitoring it via curl):

event: w
data: {"data":"{\"data1\":1456951141,41.2082,-73.3439,85%,3.97V,21.8C,26.7%,959.8hPa,0.016117}","ttl":"60","published_at":"2016-03-02T20:39:10.189Z","coreid":"540056000a51343334363138"}

event: hook-sent/w
data: {"data":"undefined","ttl":"60","published_at":"2016-03-02T20:39:06.263Z","coreid":"particle-internal"}

event: hook-response/w/0
data: {"data":"ok","ttl":"60","published_at":"2016-03-02T20:39:06.280Z","coreid":"particle-internal"}

I am using requestb.in (http://requestb.in/1l7tujd1) for testing, and the output it currently gives me is:

ok

And according to (http://requestb.in/1l7tujd1?inspect) it is recieving no POST parameters, with the raw body being this:

"measurements":{"sensor1":""}}

Again, any help would be greatly appreciated, and thank you for your help thus far.