Help sending mixed JSON data to Ubidots

I’m having difficulty getting my data over to Ubidots via JSON webhook. It seems that the string values in my JSON are causing the problem. I’m hoping someone can point out what I’m doing wrong.
My Photon’s Particle.publish() function looks like this:

char data[256];
snprintf(data, sizeof(data), "{\"windSpeedMph\": %.2f, \"windSpeedKnots\": %.2f, \"windScale\": \"%s\", \"windDir\": \"%s\"}", windSpeedMph, windSpeedK, windScale, windDir);
Particle.publish("anemometer", data, PRIVATE);

Variables windSpeedMPH and windSpeedKnots are floats, while windScale and windDir are strings. The webhook I’ve built (in Particle Console) looks like this:

{
  "speedMPH": "{{{windSpeedMph}}}",
  "speedKnots": "{{{windSpeedKnots}}}",
  "scale": "{{{windScale}}}",
  "direction": "{{{windDir}}}"
}

This webhook response shows “400” errors for the two string values direction and scale:

{"speedknots":[{"status_code":204}],"direction":[{"status_code":400,"errors":{"value":["A valid number is required."]}}],"scale":[{"status_code":400,"errors":{"value":["A valid number is required."]}}],"speedmph":[{"status_code":204}]}

If I create a JSON webhook that sends just the two float values it gets through fine, but when I send all four values nothing gets through.
I’ve tried a webhook that has double brackets—instead of triple {{{ }}}—for windScale and windDir but that doesn’t seem to fix the issue. What am I doing wrong? Thanks in advance for any help.

You can reach out to @mariahernandez from Ubidots for advice.

I’m also sure somebody else on here will be able to spot what’s not right.

@LloydChristmas, I’m totally reaching here, but just an observation:

You defined this:

snprintf(data, sizeof(data), “{“windSpeedMph”: %.2f, “windSpeedKnots”: %.2f, “windScale”: “%s”, “windDir”: “%s”}”, windSpeedMph, windSpeedK, windScale, windDir);

and then this:

{
“speedMPH”: “{{{windSpeedMph}}}”,
“speedKnots”: “{{{windSpeedKnots}}}”,
“scale”: “{{{windScale}}}”,
“direction”: “{{{windDir}}}”
}

Should the windSpeedK variable in the first block match the name windSpeedKnots in the second? The other 3 references to variables match exactly.

Hey @LloydChristmas!

As I can note in the response of the server, you are sending a value that is not supported for the Ubidots Backend which related with the strings variables. Please, make sure those strings values don’t have any extra special character.

Ubidots API has an option to force the insertion of the data, even if one of the payloads is not accepted. In order to do this, just add this to the URL “?force=true” to make sure it gets through.

I’ll add a better explanation to our API docs to reflect this, let me know if you have any questions!

All the best,
Maria C.

I believe the problem you have is that your webhook is sending up speedMPH and speedKnots as strings, not a floats. They’re floats coming out of the device, but they’re quoted in the webhook, so they’re converted to strings before going to Ubidots.

You need to use the body definition in the webhook to send up the actual float values.

2 Likes

I haven’t tried the option Maria described with force in the url, however my webhook does work now that I send the string as context of the float values. Does the Unidots accept strings as values in of themselves?

Hello @LloydChristmas,

In the Ubidots REST API Reference you will find all documentation related with the parameters supported by Ubidots:

image

Let me know if you have another doubt!

All the best,
Maria C.

1 Like