My electron is being used on a weather station. It worked for a while, then wouldn’t wake from sleep(nn) or sleep(SLEEP_MODE_DEEP,nn). I figured it was the antenna, so put on an antenna with 3.5 Dbi gain. The sleep(nn) works, but not the sleep(SLEEP_MODE_DEEP,nn).
I know the problem is not due to a weak signal since the cellular sleep works. This is my third Electron with problems. I am beginning to think that the Particle IoT products are not reliable enough for prime time.
Below is the simplified code. It collects data for 5 minutes then publishes it. After publishing the cellular is put to sleep for three minutes, and thus has 2 minutes to regain cellular signal before the next time to publish. This part works. After five times publishing, the electron is put to deep sleep for 5 minutes. (The real code has it sleeping overnight). The Electron never wakes up from deep sleep.
unsigned long thingspeakChannelNumber = xxxxxxxx;
char thingSpeakWriteAPIKey[] = "XXXXXXXXXXXXXX";
// Each time we loop through the main loop, we check to see if it's time to publish the data we've collected
unsigned int publishPeriod = 300000;
unsigned int timeNextPublish;
int publishCount = 0;
String api_key = "XXXXXXXXXXXXXXXXXX"; // Replace this string with a valid ThingSpeak Write API Key.
String field1 = "";
String field2 = ""; // i.e. field2 is null
String field3 = "";
String field4 = "";
String field5 = "";
String field6 = "";
String field7 = "";
String field8 = "";
String lat = "";
String lon = "";
String el = "";
String status = "";
void setup() {
// Set time zone to Eastern USA STANDARD time
Time.zone(float(-5));
timeNextPublish = millis() + publishPeriod;
publishCount = 0;
// Particle.connect();
}
void loop() {
if (publishCount < 5) { // Publish for 5 periods
// collect data etc.
if(timeNextPublish <= millis()) { // Publish every 5 minutes (300000 seconds)
publishCount += 1;
float tempF = publishCount; // Simlification just to have something to publish in example
publishToThingSpeak(tempF);
delay(20000); // make sure publish has time to complete
timeNextPublish = millis() + publishPeriod;
System.sleep(180); // Turn cellular off for 3 minutes to save power
// 2 minutes remain to regain cellular brfore next publish
}
} else { // time for deep sleep after 5 publish periods
publishCount = 0;
waitUntil(Cellular.ready); // Make sure to wakeup from previous sleep
System.sleep(SLEEP_MODE_DEEP,300); // deep sleep for 5 minutes
}
} // end loop
void publishToThingSpeak(float tempF) {
// To write multiple fields, you set the various fields you want to send
field1 = String(tempF,1);
/*
field2 = String(humidityRH,0);
field3 = String(pressureKPa,1);
field4 = String(rainInches,1);
field5 = String(windMPH,1);
field6 = String(gustMPH,1);
field7 = String(windDegrees, 0);
field8 = String(voltage,1);
*/
String TSjson;
createTSjson(TSjson);
Particle.publish("TSwriteall",TSjson,60,PRIVATE);
}
void createTSjson(String &dest)
{
// dest = "{ \"k\":\"" + api_key + "\", \"1\":\""+ field1 +"\", \"2\":\""+ field2 +"\",\"3\":\""+ field3 +"\",\"4\":\""+ field4 +"\",\"5\":\""+ field5 +"\",\"6\":\""+ field6 +"\",\"7\":\""+ field7 +"\",\"8\":\""+ field8 +"\",\"a\":\""+ lat +"\",\"o\":\""+ lon +"\",\"e\":\""+ el +"\", \"s\":\""+ status +"\"}";
dest = "{"
if(field1.length()>0){
dest = dest + "\"1\":\""+ field1 +"\",";
}
if(field2.length()>0){
dest = dest + "\"2\":\""+ field2 +"\",";
}
if(field3.length()>0){
dest = dest + "\"3\":\""+ field3 +"\",";
}
if(field4.length()>0){
dest = dest + "\"4\":\""+ field4 +"\",";
}
if(field5.length()>0){
dest = dest + "\"5\":\""+ field5 +"\",";
}
if(field6.length()>0){
dest = dest + "\"6\":\""+ field6 +"\",";
}
if(field7.length()>0){
dest = dest + "\"7\":\""+ field7 +"\",";
}
if(field8.length()>0){
dest = dest + "\"8\":\""+ field8 +"\",";
}
if(lat.length()>0){
dest = dest + "\"a\":\""+ lat +"\",";
}
if(lon.length()>0){
dest = dest + "\"o\":\""+ lon +"\",";
}
if(el.length()>0){
dest = dest + "\"e\":\""+ el +"\",";
}
if(status.length()>0){
dest = dest + "\"s\":\""+ status +"\",";
}
dest = dest + "\"k\":\"" + api_key + "\"}";
}