So I made changes per your suggestion but I still come across the occasional unresponsiveness to command calls.
Here is the updated code. Any thing else I may be going wrong here. Also, not sure if the ongoing outrage has something to do with it.
#include <PublishQueueAsyncRK.h>
#define DELAY_BEFORE_REBOOT (3 * 1000)
SYSTEM_THREAD(ENABLED);
retained uint8_t publishQueueRetainedBuffer[2048];
PublishQueueAsync publishQueue(publishQueueRetainedBuffer, sizeof(publishQueueRetainedBuffer));
int pin = D4;
int led1 = D7;
int sensorStatus;
int powerStatus =1;
int setPhoneEEPROMLocation = 1;
int setEmailEEPROMLocation = 100;
int setAlertEEPROMLocation = 200;
int setAlertEmailPowerEEPROMLocation = 500;
int setAlertEmailSensorEEPROMLocation = 600;
int setAlertPhonePowerEEPROMLocation = 700;
int setAlertPhoneSensorEEPROMLocation = 800;
char dev_name[40];
char phone_str[40] = "Not Set";
char email_str[40] = "Not Set";
char alertPowerSms[256] = "0";
char alertPowerEmail[256] = "0";
char alertSensorEmail[256] = "0";
char alertSensorSms[256] = "0";
char alert[10];
int A = 0;
int PS = 0;
int ES = 0;
int PP = 0;
int EP= 0;
double voltage;
double power;
double charging;
unsigned int rebootDelayMillis = DELAY_BEFORE_REBOOT;
unsigned long rebootSync = millis();
bool resetFlag = false;
int SG;
double batterySoc = System.batteryCharge();
int powerSource = System.powerSource();
void setup()
{
Particle.variable("S", sensorStatus);
Particle.variable("SPS", phone_str);
Particle.variable("SES", email_str);
Particle.function("SES", setEmail);
Particle.function("SPS", setPhone);
Particle.function("EA", enableAlerts);
Particle.function("EP", enableEmailPowerAlerts);
Particle.function("ES", enableEmailSensorAlerts);
Particle.function("PP", enablePhonePowerAlerts);
Particle.function("PS", enablePhoneSensorAlerts);
Particle.variable("A", A);
Particle.variable("EP", EP);
Particle.variable("ES", ES);
Particle.variable("PP", PP);
Particle.variable("PS", PS);
Particle.variable("SG", SG);
Particle.variable("BAT", batterySoc);
Particle.variable("PWR", powerSource);
Particle.function("RT", cloudResetFunction);
getStoredInfo();
pinMode(pin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
if (digitalRead(pin) == LOW)
{
digitalWrite(led1, LOW);
}
else {
digitalWrite(led1, HIGH);
}
Particle.subscribe("particle/device/name", handler);
waitFor(Particle.connected, 600000);
Particle.publish("particle/device/name");
}
void loop()
{
batterySoc = System.batteryCharge();
powerSource = System.powerSource();
system_display_rssi();
if (powerSource == 5)
{
if (powerStatus != 0)
{
if ((A == 1) && (PP == 1))
{
snprintf(alertPowerSms, sizeof(alertPowerSms) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , phone_str );
publishQueue.publish("test1", alertPowerSms, 60, PRIVATE);
}
if ((A == 1) && (EP == 1))
{
snprintf(alertPowerEmail, sizeof(alertPowerEmail) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , email_str );
publishQueue.publish("test2", alertPowerEmail, 60, PRIVATE);
}
powerStatus = 0;
}
}
else if (powerSource < 5)
{
if (powerStatus != 1)
{
if ((A == 1) && (PP ==1)){
snprintf(alertPowerSms, sizeof(alertPowerSms) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , phone_str );
publishQueue.publish("test3", alertPowerSms, 60, PRIVATE);
}
if ((A == 1) && (EP ==1)) {
snprintf(alertPowerEmail, sizeof(alertPowerEmail) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , email_str );
publishQueue.publish("test4", alertPowerEmail, 60, PRIVATE);
}
powerStatus = 1;
}
}
if (digitalRead(pin) == HIGH)
{
if (sensorStatus != 0)
{
digitalWrite(led1, HIGH);
if ((A == 1) && (PS == 1))
{
snprintf(alertSensorSms, sizeof(alertSensorSms) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , phone_str );
publishQueue.publish("test5", alertSensorSms, 60, PRIVATE);
}
if ((A == 1) && (ES == 1))
{
snprintf(alertSensorEmail, sizeof(alertSensorEmail) , "{\"N\": \"%s\", \"T\": \"%s\"}" , dev_name , email_str );
publishQueue.publish("test6", alertSensorEmail, 60, PRIVATE);
}
sensorStatus = 0;
}
}
else if (digitalRead(pin) == LOW)
{
if (sensorStatus != 1)
{
digitalWrite(led1, LOW);
sensorStatus = 1;
}
}
if ((resetFlag) && (millis() - rebootSync >= rebootDelayMillis))
{
delay(1000);
System.reset();
}
if ((PP == 0) && (EP == 0) && (PS == 0) && (ES == 0))
{
enableAlerts("0");
}
if ((PP == 1) | (EP == 1) | (PS== 1) | (ES == 1))
{
enableAlerts("1");
}
}
int enableAlerts(String command)
{
if (command.equalsIgnoreCase("1"))
{
A = 1;
EEPROM.put(setAlertEEPROMLocation, A);
return 1;
}
else
{
A = 0;
EEPROM.put(setAlertEEPROMLocation, A);
return 0;
}
}
int enableEmailPowerAlerts(String command)
{
if (command.equalsIgnoreCase("1"))
{
EP = 1;
EEPROM.put(setAlertEmailPowerEEPROMLocation, EP);
return 1;
}
else
{
EP = 0;
EEPROM.put(setAlertEmailPowerEEPROMLocation, EP);
return 0;
}
}
int enableEmailSensorAlerts(String command)
{
if (command.equalsIgnoreCase("1"))
{
ES = 1;
EEPROM.put(setAlertEmailSensorEEPROMLocation, ES);
return 1;
}
else
{
ES = 0;
EEPROM.put(setAlertEmailSensorEEPROMLocation, ES);
return 0;
}
}
int enablePhonePowerAlerts(String command)
{
if (command.equalsIgnoreCase("1"))
{
PP = 1;
EEPROM.put(setAlertPhonePowerEEPROMLocation, PP);
return 1;
}
else
{
PP = 0;
EEPROM.put(setAlertPhonePowerEEPROMLocation, PP);
return 0;
}
}
int enablePhoneSensorAlerts(String command)
{
if (command.equalsIgnoreCase("1"))
{
PS = 1;
EEPROM.put(setAlertPhoneSensorEEPROMLocation, PS);
return 1;
}
else
{
PS = 0;
EEPROM.put(setAlertPhoneSensorEEPROMLocation, PS);
return 0;
}
}
int setPhone(const char *data)
{
strncpy(phone_str, data, sizeof(phone_str) - 1);
phone_str[sizeof(phone_str) - 1] = '\0';
EEPROM.put(setPhoneEEPROMLocation, phone_str);
}
int setEmail(const char *data)
{
strncpy(email_str, data, sizeof(email_str) - 1);
email_str[sizeof(email_str) - 1] = '\0';
EEPROM.put(setEmailEEPROMLocation, email_str);
}
void getStoredInfo()
{
EEPROM.get(setPhoneEEPROMLocation, phone_str);
EEPROM.get(setEmailEEPROMLocation, email_str );
EEPROM.get(setAlertEEPROMLocation, A);
EEPROM.get(setAlertEmailSensorEEPROMLocation, ES);
EEPROM.get(setAlertEmailPowerEEPROMLocation, EP);
EEPROM.get(setAlertPhoneSensorEEPROMLocation, PS);
EEPROM.get(setAlertPhonePowerEEPROMLocation, PP);
}
void handler(const char *topic, const char *data)
{
strncpy(dev_name, data, sizeof(dev_name) - 1);
}
int cloudResetFunction(String command)
{
resetFlag = true;
rebootSync = millis();
return 0;
}
void system_display_rssi()
{
int rssi = 0;
#if Wiring_WiFi == 1
rssi = WiFi.RSSI();
#elif Wiring_Cellular == 1
CellularSignal sig = Cellular.RSSI();
rssi = sig.getStrength();
#endif
if (rssi < 0)
{
if (rssi >= -57)
{
SG = 5;
}
else if (rssi > -68)
{
SG = 4;
}
else if (rssi > -80)
{
SG = 3;
}
else if (rssi > -92)
{
SG = 2;
}
else if (rssi > -104)
{
SG = 1;
}
}
if (rssi >= 0)
{
if (rssi >= 60)
{
SG = 5;
}
else if (rssi > 40)
{
SG = 4;
}
else if (rssi > 20)
{
SG = 3;
}
else if (rssi > 1)
{
SG = 2;
}
else if (rssi >= 0)
{
SG = 1;
}
}
}