Okay, I cleaned that up on the Xenon. I got rid of the JSON generator and the Xenon still works correctly.
#include <Seeed_DHT11.h>
#define DHTPIN D4//set pin for DHT
DHT dht(DHTPIN);
int tempF = 0;
int humidity = 0;
int lasttempF = 0;//I'll use these to get rid of spikes in the data
int tempFdelta;
int lasthumidity = 0;
int humiditydelta;
unsigned long lastPubMillis = 0;
unsigned long pubInterval = 5000;
//Global variables to store messages.
char *message = "Xenon1";
char msg[128];
void setup()
{
Particle.variable("tempF", tempF);
Particle.variable("humidity", humidity);
dht.begin(); //initialize the sensor
Serial.begin(9600);
}
void loop()
{
//Use a millis timer for non-blocking code design.
if (millis() - lastPubMillis > pubInterval)
{
humidity = dht.getHumidity();
tempF = dht.getTempFarenheit();
tempFdelta = tempF-lasttempF;
tempFdelta = abs(tempFdelta);
humiditydelta = humidity-lasthumidity;
humiditydelta = abs(humiditydelta);
if(tempFdelta <2 && humiditydelta <2)
{
snprintf(msg, sizeof(msg), "{\"tempF\":\"%.d\",\"humidity\":\"%.d\"}",tempF,humidity);
Serial.println(msg);
//Send the data.
Mesh.publish("Xenon1",msg);
}
//Update the pub millis timer.
lastPubMillis = millis();
lasttempF = tempF;
lasthumidity = humidity;
}
}
I'm sorry, @ScruffR, but I'm not following. I added the line (I'm including the entire myHandler to show where I put it):
// this will be called whenever an event arrives we subscribed to
void myHandler(const char *event, const char *data){
Serial.println(data); // print out the data as it comes in for debugging
strncpy(msg, data, sizeof(msg)-1); // copy the incoming data to the global variable msg (with boundary limit)
jsonParser.clear(); // make sure the parser buffer is fresh and empty
jsonParser.addString(data); // copy the received data into the parser buffer for it to work with
if (jsonParser.parse()) { // let the parser do its job and split up the data internally
// first ask the parser for the value connected with the key 'tempF'
// if this is successful pop that value into the provided variable tempF
// if not, the function will return false which - in turn - triggers the error output
if (jsonParser.getOuterValueByKey("tempF", tempF) == false) {
Serial.println("failed to get Temperature");
}
// do the same for 'humidity'
// if(!someBoolean) is short hand for if(someBoolean == false)
if (!jsonParser.getOuterValueByKey("humidity", humidity)) {
Serial.println("failed to get Humidity");
}
}
}
But my results are still the same. i guess I just assumed that since the data was printing correctly in the console then that meant that it was actually set in the subscription handler. I don't know how to check that.