I am not much of a programming buff outside of web languages. So I need to understand what I think might be a buffer. Consider the following code:
#include <Seeed_DHT11.h>
#define DHTPIN D2
DHT dht(DHTPIN);
void setup() {
dht.begin();
}
void loop() {
delay(2000);
float h = dht.getHumidity();
float f = dht.getTempFarenheit();
float voltage = analogRead(BATT) * 0.0011224;
String device = "XPX-Xenon-01";
char data[128];
// sprintf(data, "{\"Temperature\":\%.2f\,\"Humidity\":\%.2f\,\"Battery Voltage\":\%.2f\,\"Device\":\%s\}", f, h, voltage, device);
sprintf(data, "{\"Temperature\":\%.2f\, \"Humidity\":\%.2f\, \"Battery Voltage\":\%.2f\, \"Device\":\"XPX-Xenon-1\"}", f, h, voltage);
if(isnan(h) || isnan(f))
{
Particle.publish("Not reading temp or humi");
}
Particle.publish("Environment Reading", data);
}
So before I was using a char data [64] and when uploaded to the xenon it would crash. The I arbitrarily decided to double that to 128.Voila! no more crashing and even before it is formatted in pretty JSON in the console! But I think it’s important to understand how to properly size it. So what are we counting here?
Cheers,