I have a Photon that goes to sleep for ~55 seconds and then wakes up, connects to Cloud, takes some measurements, Publish to Cloud, than back to sleep. It is powered from a battery that is backed up with a solar panel.
It always has a strong WiFi signal and good power.
It has been running for over 30 days, but 3 times it has been stuck breathing white and never connects. The power has to be cycled to recover.
What am I doing wrong to cause this issue or what is the recommended way to avoid it? Watch Dog? More startup delay? Check for connection?
My Code:
// INCLUDES
#include "Particle.h"
#include "Particle_MPL115A2.h"
#include "GA1A12S202.h"
#include "DS18B20.h"
#include "Adafruit_DHT_Particle.h"
// STARTUP MACRO
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // selects the u.FL external antenna
//SYSTEM_THREAD(ENABLED); // Can't use since we need Wifi up to get signal strength
//DEFINES
#define DHTPIN 3 // DHT22 Data Pin D3
#define DHTTYPE DHT22 // DHT22 (AM2302)
Particle_MPL115A2 mpl115a2 = Particle_MPL115A2(); // I2C 5V Pins D0 and D1
GA1A12S202 lux(A0); // Analog Pin A0
DS18B20 ds18b20(D2, true); // Dallas 1-Wire Pin D2
DHT dht(DHTPIN, DHTTYPE); // DHT22 1-Wire Pin D3
// CONSTANTS
const char VERSION[] = "P3:Enviro #1 - Solar"; // Device info
const uint32_t msSAMPLE_D_INTERVAL = 10000; // Sample Digital Data Rate
const uint32_t msPUBLISH_INTERVAL = 20000; // Publish Rate
const int MAXRETRY = 5;
const int SLEEP_RATE = 55; // Seconds to sleep, Startup and reconnect takes ~12 sec
const bool useTerm = false; // Enable/Disable USB-Serial Terminal with output
const bool pubData = true; // Enable/Disable Publishing of Data to Particle Cloud
const float VRef = 3.36; // this should be measured to calibrate the readings
const float voltageDivider = 0.75;
// VARIABLES
char szInfo[64];
float DHT_TempC = 0.00;
float DHT_Humidity = 0.00;
int rssi = 0;
int wifi_level = 0;
float MPL115A2_pressureKPA = 0.00;
float MPL115A2_pressurePSI = 0.00;
float MPL115A2_temperatureC = 0.00;
float GA1A12S202_Raw = 0.00;
float GA1A12S202_Lux = 0.00;
double DS18B20_temperatureC = 0.00;
uint32_t msLastSampleD = 0;
float batteryV = 0.00;
int A1_reading = 0;
void setup()
{
// USB - Serial
if (useTerm == 1){
Serial.begin(115200);
delay(1000);
Serial.println("** RESET **");
}
// Sensors
mpl115a2.begin();
dht.begin();
// Power ON Delay
delay(2000); // NEEDED, DO NOT CHANGE
}
void loop()
{
// Acquire Data
// Remove sample loop if deep sleep
//if (millis() - msLastSampleD >= msSAMPLE_D_INTERVAL){
// WiFi Signal Level
rssiWiFi();
// Data data from GA1A12S202
//GA1A12S202_Raw = lux.getRaw();
GA1A12S202_Lux = lux.getLux(true);
// Data data from MPL115A2
// Convert kPa to psi: divide kPa by 6.895
mpl115a2.getPT(&MPL115A2_pressureKPA,&MPL115A2_temperatureC);
//MPL115A2_pressurePSI = MPL115A2_pressureKPA/6.895;
// Get data from DS18B20
getDS18B20();
if ((isnan(DS18B20_temperatureC)) ||
(DS18B20_temperatureC > 75) ||
(DS18B20_temperatureC < -50)
){
delay(100);
getDS18B20();
}
// Data data from DHT22
DHT_Humidity = dht.getHumidity();
delay(100);
DHT_TempC = dht.getTempCelcius();
if((isnan(DHT_Humidity)) ||
(DHT_Humidity > 100) ||
(DHT_Humidity < 0) ||
(isnan(DHT_TempC)) ||
(DHT_TempC > 75) ||
(DHT_TempC < -50)
){
delay(2000);
DHT_Humidity = dht.getHumidity();
delay(100);
DHT_TempC = dht.getTempCelcius();
}
// Power OFF Sensors
// Measure Battery Voltage
A1_reading = analogRead(A1);
delay(250);
A1_reading = analogRead(A1);
batteryV = (((float)A1_reading / 4095.0) * VRef) / voltageDivider;
//batteryV = 1.23;
// Print Data
if (useTerm == true){
printData();
}
// Publish Data
if (pubData == true){
if (Particle.connected()){
publishData();
}
}
//*****************************************************
//* DEEP SLEEP MODE - DISABLES WIFI - NO OTA UPDATES
//*****************************************************
System.sleep(SLEEP_MODE_DEEP, SLEEP_RATE);
}
// FUNCTIONS
// Publish Data to Particle Cloud
void publishData(){
sprintf(szInfo, "%.2f:%.1f:%.0f:%.1f:%.1f:%.1f:%i:%d:%.3f",
MPL115A2_pressureKPA,
MPL115A2_temperatureC,
GA1A12S202_Lux,
DHT_Humidity,
DHT_TempC,
DS18B20_temperatureC,
rssi,
wifi_level,
batteryV
);
Particle.publish("Main_Data", szInfo, PRIVATE);
//Serial.println(szInfo);
}
void printData(){
Serial.println("----------------------------------");
Serial.print("MPL115A2 - Pressure: "); Serial.print(MPL115A2_pressurePSI, 2); Serial.println(" psi");
Serial.print("MPL115A2 - Temperature: "); Serial.print(MPL115A2_temperatureC, 1); Serial.println(" degC");
Serial.print("GA1A12S202 - Lux: "); Serial.print(GA1A12S202_Lux, 0); Serial.println(" lux");
Serial.print("DHT22 - Humidity: "); Serial.print(DHT_Humidity, 1); Serial.println(" %");
Serial.print("DHT22 - Temperature: "); Serial.print(DHT_TempC, 1); Serial.println(" degC");
Serial.print("DS18B20 - Temperature: "); Serial.print(DS18B20_temperatureC, 1); Serial.println(" degC");
Serial.print("WiFi - RSSI: "); Serial.println(rssi);
Serial.print("WiFi - Level: "); Serial.println(wifi_level);
Serial.print("Battery Voltage: "); Serial.println(batteryV);
Serial.println("----------------------------------");
}
void getDS18B20(){
float _temp;
int i = 0;
do {
_temp = ds18b20.getTemperature();
} while (!ds18b20.crcCheck() && MAXRETRY > i++);
if (i < MAXRETRY) {
DS18B20_temperatureC = _temp;
}
else {
DS18B20_temperatureC = NAN;
}
}
void rssiWiFi(){
rssi = WiFi.RSSI(); // -127 (weak) to -1dB (strong)
if (rssi < 0) {
if (rssi > -40) {
//Serial.println("Signal strength is Strong RSSI: "+String(rssi)+"dB.");
wifi_level = 5;
}
else
if (rssi > -65) {
//Serial.println("Signal strength is OK - RSSI: "+String(rssi)+"dB.");
wifi_level = 4;
}
else
if (rssi > -75) {
//Serial.println("Signal strength is OK - RSSI: "+String(rssi)+"dB.");
wifi_level = 3;
}
else
if (rssi > -90) {
//Serial.println("Signal strength is Weak - RSSI: "+String(rssi)+"dB.");
wifi_level = 2;
}
else {
//Serial.println("Signal strength is Poor - RSSI: "+String(rssi)+"dB.");
wifi_level = 1;
}
}
else {
// This case runs when WiFi is disconnected
//Serial.println("Wifi is disconnected. RSSI: "+String(rssi)+"dB.");
wifi_level = 0;
}
}