Webhooks and Ubidots

Greetings @chipmc, it is pretty strange your issue, at my side is working properly:

Keep in mind that with the payload of the previous example, the only variable with context data is "Watering" so that is the one that you should check. Can you gently try again?

Regards

@jotathebest,

Thank you for encouraging me to try again. It worked! Now I need to see If I can turn this into a Particle Webhook. Do you have any good examples of using mustache templates to parse the Ubidots response?

Thanks,

Chip

OK,

So, I think I have made some progress in finding the issue.

  1. With @jotathebest and @mariahernandez help, I now have a curl command that will send the non-GPS context to Ubidots. Here is the response code I get:
{"watering": [{"status_code": 201}], "soiltemp": [{"status_code": 201}], "moisture": [{"status_code": 201}]}
  1. However, when I send that exact same string using the Webhook. I get a different result.

My code snippet:

  String data = "{\"SoilTemp\":\"19\",\"Watering\":{\"value\": \"0\", \"context\": {\"key1\":\"Oops\"}},\"Moisture\":\"516\"}";
  Particle.publish("AquaMaster", data, PRIVATE);

My JSON template in the Webhook:

{
    "SoilTemp":"{{SoilTemp}}",
    "Watering":
        {"value": "{{Watering}}", 
        "context": 
            {"key1":"{{Context}}"
            }
        },
    "Moisture":"{{Moisture}}"
}

Now I get this response in the console:

  1. When I redirect the Webhook to Requestb.in, I can see that the JSON is getting garbled:
    ‘’’
    {“Moisture”:“516”,“Watering”:{“context”:{“key1”:""},“value”:"[object Object]"},“SoilTemp”:“19”}
    ‘’’
    The Context and the value for “watering” is not being sent.

Is this a limitation of what a JSON WebHook can do? Do I need to move to the “Body” style. I don’t think this is an Ubidots issue - it seems to be a problem on the Particle side.

Any ideas?

Thanks,

Chip

Hi, unfortunately I do not have a single parser in a snippet, but you can refer to any of our functions to get values from Ubidots, there you can find a reference piece of code to reference, i.e inside the ESP8266 library: https://github.com/ubidots/ubidots-esp8266/blob/master/UbidotsMicroESP8266.cpp#L186

Hope it helps you.

@jotathebest,

Got it.

Thank you!

Chip

@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:

  1. There are no good examples of non-gps context
  2. 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:

  1. 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
  1. 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.

  2. Read @rickkas7 's tutorial found here.

  3. 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).

  1. 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"}
  1. 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.

  2. 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

2 Likes

You can also do this using the console, Products for example don’t support creating webhooks via the console using a JSON file.

Send it a string like this {“w”:33,“s”:“Pebbles”} and value and context arrive as expected the other end @ Ubidots. At the other end this http://help.ubidots.com/user-guides/using-context-to-display-text-in-ubidots-widgets might be useful so you can display that context, sadly they haven’t made any of the other widgets editable in such a way as to display context which is a shame.

Its important your context is encased in quotes, I think anything not in quotes Ubidots will attempt to read as a string representation of a number.

1 Like