Having issues with Photon going offline after a certain interval. It seems to have corresponded with adding new firmware which included POST to data.sparkfun.com. Though not 100% that is the cause. Code seems to work fine for a while and then Photon goes to flashing cyan and stops running. If I remove the POST section it stays on and if I increase the interval it appears to last longer. No idea why however?
#include "DHT.h"
#include "SparkFun-Spark-Phant/SparkFun-Spark-Phant.h"
#define DHTPIN D4
#define DHTTYPE DHT22
#define DELAY 5000
//Setup DHT Sensor
DHT dht(DHTPIN, DHTTYPE);
int temperature = 0;
int humidity = 0;
//Setup data.sparkfun.com
const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "MY_KEY"; // Phant public key
const char privateKey[] = "MY_KEY"; // Phant private key
Phant phant(server, publicKey, privateKey); // Create a Phant object
//Global variable for data.sparkfun.com POST
int steptime = 0;
void setup() {
Serial.begin(115200);
dht.begin();
// Particle Functions
Spark.function("gettmp", getTemperature);
Spark.function("gethmd", getHumidity);
}
void loop() {
// Get temperature and humidity
temperature = (int)dht.readTemperature(true);
humidity = (int)dht.readHumidity();
//Execute data.sparkfun.com POST every 10 min
steptime++;
if(steptime == 120){
postToPhant();
steptime = 0;
}
//Delay till next loop
delay(DELAY);
}
int postToPhant()
{
phant.add("temp", temperature);
phant.add("humidity", humidity);
TCPClient client;
char response[512];
int i = 0;
int retVal = 0;
if (client.connect(server, 80))
{
Serial.println("Posting!");
client.print(phant.post());
delay(1000);
while (client.available())
{
char c = client.read();
//Serial.print(c);
if (i < 512)
response[i++] = c;
}
if (strstr(response, "200 OK"))
{
Serial.println("Post success!");
retVal = 1;
}
else if (strstr(response, "400 Bad Request"))
{
Serial.println("Bad request");
retVal = -1;
}
else
{
retVal = -2;
}
}
else
{
Serial.println("connection failed");
retVal = -3;
}
client.stop();
return retVal;
}
Although I’m not sure if it’s connected to the issues you’re seeing, Might I recommend that you use the Piettetech library for the DHT. It is known to be highly reliable, whereas other have displayed various problems. Could be that the library is causing issues.
Thanks for the suggestion. I actually had this running for several weeks without exporting the data to spark so not sure the DHT is the issue but worth a try. One question I haven’t seen answered anywhere is what a flashing cyan actually means. Docs say its server handshake, but that doesn’t really make sense in my case.
The code that really seems to kill it is the section:
Rapid flashing cyan was introduced just recently (I think with 0.4.4), I’ll try to find the description (when introduced), but it should be cloud handshake as you said.
Why would you say it doesn’t make sense in your case?
If your code (or other circumstances) kill the cloud connection, it will (usually) try to reestablish and has to go through all the hookup-blinks.
And the big part not shown in your 5-liner above is postToPhant() implementation. Anything in there could trip the cloud, especially if there are long running remote tasks,
Maybe adding a Particle.process() and a timeout in here
while (client.available())
{
char c = client.read();
//Serial.print(c);
if (i < 512)
response[i++] = c;
}
might break out of a potentially long running loop and at least service the cloud often enough.
Thanks for suggestions, I will give this a try and see if there are any changes. I did include the postToPhant() in the original post, just didn’t want to duplicate, and that function does now include your last suggestion.
int postToPhant()
{
phant.add("temp", temperature);
phant.add("humidity", humidity);
TCPClient client;
char response[512];
int i = 0;
int retVal = 0;
if (client.connect(server, 80))
{
Serial.println("Posting!");
client.print(phant.post());
delay(1000);
while (client.available())
{
char c = client.read();
//Serial.print(c);
if (i < 512)
response[i++] = c;
}
if (strstr(response, "200 OK"))
{
Serial.println("Post success!");
retVal = 1;
}
else if (strstr(response, "400 Bad Request"))
{
Serial.println("Bad request");
retVal = -1;
}
else
{
retVal = -2;
}
}
else
{
Serial.println("connection failed");
retVal = -3;
}
client.stop();
return retVal;
}