Trouble forming JSON in the Azure / Photon example

Hi, I’m trying to replicate the ‘Hands on Particle Photon Weather Station in Azure’

My photon is connected. It is correctly reading the temperature and humidity. The webhook is created. By subscribing to the webhook, I can tell that the JSON payload is formed incorrectly.

An example is below. My JSON is Montclair and the correct one is Houston. Notice that mine is missing the name of the organization, the value of the humidity, and the location.

{"name":"ConnectTheDots","data":"{ \"s\":\"wthr\", \"u\":\"%\",\"l\":\"Houston, TX\",\"m\":\"Humidity\",\"o\":\"DXHackers\",\"v\": 34.000000,\"d\":\"Paul Bedroom\" }","ttl":"60","published_at":"2015-08-7T16:14:49.440Z","coreid":"53ff70066678505528451467"}{"name":"ConnectTheDots","data":"{ \"s\":\"wthr\", \"u\":\"%\",\"l\":\"Montclair, NJ\",\"m\":\"Humidity\",\"o\":\"P","ttl":"60","published_at":"2015-08-07T16:14:51.649Z","coreid":"2b0041000547343138333038"}

The code I am using is the same as in the lab.

//Above the void setup() line
char Org[] = ""; 
char Disp[] = "Office"; 
char Locn[] = "Montclair, NJ"; 

//insde the void loop()
char payload[255];

snprintf(payload, sizeof(payload), "{ \"s\":\"wthr\", \"u\":\"%%\",\"l\":\"%s\",\"m\":\"Humidity\",\"o\":\"%s\",\"v\": %f,\"d\":\"%s\" }", Locn, Org, h, Disp);
Spark.publish("ConnectTheDots", payload);

I have the same issue when I try to create the JSON payload with temperature. Can someone please help me correct the problem.


The publish API limits strings to 63 characters which is why the JSON is truncated at 63 characters. You’ll need to remove fields until the payload is less than 63.

Second, there is currently a limitation in the Photon firmware that prevents printf("%f") from working properly. You’ll need to use another method like String(humidity, 1) where 1 is the number of digits.

Thanks for the response @jvanier

It seems that the name is limited to 63 chars. I think ConnectTheDots is within that limitation. The rest of the data has a limit of 255 char and I’m within that limit.

Could it be something else?

Also, I cant use String(humidity, 1) since I need it to be a float. Any idea how I could get a float instead?

I think the docs are slightly ahead of the firmware release. The limit will increase from 63 to 255 with the next firmware release but it is not there yet. The truncation you see is because of the 63 char limit.

The String constructor accepts a floating point number and a precision to do the conversion to string

double humidity = 30.5;
String humidityStr = String(humidity, 1); // "30.5"
char buffer[255];
snprintf(buffer, sizeof(buffer), "{\"h\":" + humidityStr + "}");

That last line is not ideal, but it should work until sprintf("%f") is fixed.


That did the trick. Thank you, @jvanier!
I had been struggling with this for hours.

1 Like