@jotathebest, @mariahernandez, @aguspg, @rickkas7
I have been able to figure out how to add context to my datapoints with a Web hook to Ubidots. It was not easy for two reasons:
- There are no good examples of non-gps context
- There are not great tools for debugging a web hook
My hope is that this post will help the next person trying to send non-GPS data to Ubidots using a Webhook.
Here is the approach:
- With he help of the good folks from Ubidots, I determined the correct format for a curl command that would create a new datapoint on Ubidots with “context”. It was:
curl -XPOST -H 'Content-Type: application/json;' -H 'X-Auth-Token: My Token' -d '{"SoilTemp":"19","Watering":{"value": "0", "context": {"key1":"Oops"}},"Moisture":"516"}' https://things.ubidots.com/api/v1.6/devices/My Device API Key
-
There are three ways to send Webhooks: Form, JSON and Body. Don’t waste your time on Form or JSON - they will not work for JSON which contains more than a simple one-layer key / value payload. Go straight to the “Body” method.
-
Read @rickkas7 's tutorial found here.
-
Create a whatever.json file using this template. Mine looks like this:
{
"event": "Ubidots_hook",
"url": "https://things.ubidots.com/api/v1.6/devices/My Device API Key",
"headers": {
"X-Auth-Token": "My Token"
},
"requestType": "POST",
"mydevices": true,
"noDefaults": true,
"json":{
"SoilTemp":"19",
"Watering":{
"value": "0",
"context": {
"key1":"Oops"
}
},
"Moisture":"516"
}
}
Use the steps in Rick’s tutorial for the steps to “create” and “publish” the web hook and make sure it is received by Ubidots and generates a “201” response code. See this site for the standard HTTP status codes during this step (thanks @ScruffR).
- If you have any problems at this point, Use @rickkas7 's mustache tester and redirect to a Requestb.in URL to see the JSON Payload. Mine was:
{"Moisture":"516","Watering":{"context":{"key1":"Oops"},"value":"0"},"SoilTemp":"19"}
-
Then go to Ubidots and make sure that a new data point was created for “watering” and that datapoint had a context of “Key1:Oops”.
. OK, so now, we can create a new datapoint on Ubidots from the command line using dummy data via a Webhook.
-
The next step is to replace the String values used in troubleshooting with variables which are provided in your sketch. This is actually fairly easy when you use the Body web hook. All you need to do is name the variables in the whatever.JSON
{
"event": "Ubidots_hook",
"url": "https://things.ubidots.com/api/v1.6/devices/My device",
"headers": {
"X-Auth-Token": "My Token"
},
"requestType": "POST",
"mydevices": true,
"noDefaults": true,
"responseTemplate": "{{watering.0.status_code}}",
"json":{
"SoilTemp":"{{SoilTemp}}",
"Watering":{
"value": "{{Watering}}",
"context": {
"key1":"{{key1}}"
}
},
"Moisture":"{{Moisture}}"
}
}
Then, put the key value pairs into your code:
char data[256];
snprintf(data, sizeof(data), "{\"Moisture\":%i, \"Watering\":%i, \"key1\":\"%s\", \"SoilTemp\":%i}",capValue, wateringMinutes, wateringContext, soilTemp);
Particle.publish("Ubidots_hook", data , PRIVATE);
And the new data points are created - adding context to each of the “Watering” entries.
Since I also included the response template in the web hook, I will get a simple HTTP response code (i.e. “201” when a datapoint was successfully created) back from Ubidots which will make writing the response handler a breeze. I was able to get the response template back by simply pasting the Ubidots JSON response into the Mustache tester and then worked with the response template until I got the right output.
I hope this has been helpful and you can put the full power of Web hooks to work as you integrate your Particle device with backend IOT services such as Ubidots.
Thanks,
Chip