Check if my System.sleep() for deep sleep mode would work

Dear all

I want my device to wake up only once per day to save power (the application is for remote tracking of animals and one data point per day is ok for me). The firmware shown below is for publishing information identifying a serving cellular tower (a proxy for location) but the System.sleep() component has never been tested. Having read the documentation I prepared this draft below. Would it work or do you spot any issues with it? The Serial part is for debugging in the lab only. Any comments are appreciated

#pragma PARTICLE_NO_PREPROCESSOR
#include "Particle.h"

SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);

char cellular_data[200] = "";

int cb(int type, const char* buf, int len, char* cellular_data);

const long SLEEP_TIME_SECS = 86400;

void setup() {
    
	Serial.begin(9600);
	
}


void loop(){
    
    if ((RESP_OK == Cellular.command(cb, cellular_data, 10000, "AT+CGED=3\r\n"))
        && (strcmp(cellular_data, "") != 0))
    {
        Serial.printlnf("Cellular info: %s\r\n", cellular_data);
    }
    else
    {
        Serial.printlnf("Cellular info not found");
    }
    
    if (strlen(cellular_data) > 0) {
        Particle.publish("cellData", cellular_data, PRIVATE);
        delay(1000);             // to obey publish limit
        cellular_data[0] = '\0'; // reset string to not publish the same one again 
    }
    
    System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME_SECS); // wake up once per 1 day = 86400 seconds
}


int cb(int type, const char* buf, int len, char* cellular_data)
{
    if ((type == TYPE_PLUS) && cellular_data) {
        if(sscanf(buf, "\r\n+CGED: %[^\r]\r\n", cellular_data) == 1)
        /*nothing*/;
    }
    return WAIT;
}

@animal-navigation, sleeping with SLEEP_MODE_DEEP will reboot the Electron after it wakes so be aware of that.

You may also want to shut down the Cloud connection (Paricle.disconnect())and the cellular connection (Cellular.disconnect(), Cellular.off()) prior to sleeping to make a “clean” break from the Cloud and the Cellular tower.

2 Likes

Since you are running SYSTEM_THREAD(ENABLED) you should be waiting for Cellular.ready() before your Cellular.command() call and for Particle.connected() before your Particle.publish().

While SYSTEM_MODE(AUTOMATIC) will immediately get to spinning up the cloud connection your application code will most likely outpace that process and be done and back to sleep before the cloud connection may have been fully established.

2 Likes

@peekay123
Thanks I will attempt to embed this and the below routine that @ScruffR Scruff described

@ScruffR Thanks as always very to the point. I will try to revisit the listing and hopefully report the result