Electron Deep Sleep and never wake up?

Hey all,

I’ve got an interesting problem with SLEEP_MODE_DEEP – my electron doesn’t want to wake up from deep sleep unless I specifically put a numerical value in the call to System.sleep.

For example:

const unsigned long TIME_BETWEEN_PUBLISH_SEC = 10 * 60;
...
System.sleep(SLEEP_MODE_DEEP, TIME_BETWEEN_PUBLISH_SEC);

And the device never wakes up.

System.sleep(SLEEP_MODE_DEEP, 600);

And the device behaves as expected… any thoughts? Not terribly troublesome to go edit the value, but not what I’d prefer.

Thanks!

Turns out it didn’t like the value because it was unsigned.

That’s an interesting find since that should not matter at all!
You say const unsigned long doesn’t wake but const long does?

This would need further investigation and if it turns out to be that way a bug report might be required.
@BDub?

Yeah, I thought it was a bit weird since the example I’ve based my code off (wake on move) has unsigned longs that are used for non-deep sleep.

So either the deep-sleep code is very different or my device is…

It is declared long in the source (wiring/src/spark_wiring_system.cpp):

void SystemClass::sleep(Spark_Sleep_TypeDef sleepMode, long seconds, SleepNetworkFlag network)
{
    system_sleep(sleepMode, seconds, network.flag(), NULL);
}

void SystemClass::sleep(uint16_t wakeUpPin, InterruptMode edgeTriggerMode, long seconds, SleepNetworkFlag network)
{
    system_sleep_pin(wakeUpPin, edgeTriggerMode, seconds, network.flag(), NULL);
}

Maybe a github issue should be raised to make it unsigned long since negative values are nonsensical.

But even so, if only passing in a positive number there is no difference in the binary value (irrespective of unsigned long or long or even int/unsigned int), so any difference in behaviour is absolultely unexpected and illogical.
For that reason alone an issue should be raised, since it might have an impact on other things too as long the root cause is not clear.


Issue filed
https://github.com/spark/firmware/issues/1143

1 Like

I did some more testing lastnight, tried to trim my code down to the least amount of code to reproduce the issue, but I was unable to reproduce the issue.

I’d also made a few other changes to my program after finding out that it worked fine with a signed value.

I’ll keep poking at deep sleep and unsigned longs, this was the second time I’d experienced this, unfortunately using the Web IDE it is really easy to get into the bad practice of not versioning your projects.

1 Like

Was not able to confirm this issue… if you have more info, please update here, thanks! https://github.com/spark/firmware/issues/1143

Hi there

I also have this issue using v0.7.0-rc.3 - Electron device goes to sleep and never wakes.
Using threading

void sleep_deeply(long seconds_to_sleep){

    DEBUG(" sleeping deeply for %d secs", seconds_to_sleep);
    delay(1000);
    System.sleep(SLEEP_MODE_DEEP,seconds_to_sleep); // SLEEPS BUT NEVER WAKES
    //System.sleep(WKP, RISING, seconds_to_sleep);           //WORKS as expected. 
}

From where are you calling that function?
What sleep duration are you passing?

I have tried it in the setup() and in the main loop.

I have tried 10secs and 60 Secs

The code posted above is called from another function - main_check_psu_input_volts(false); - which checks the battery health and determines if it is supposed to sleep or not. The argument (false) tells us whether it was called from setup or the main loop and is in there for testing, I was wondering if you could call sleep in the setup() function and if this was the cause of the unit not waking, it’s not the problem.

Here is my entire main loop()

    void loop(){
	static uint32_t last_time = 0;
	
	
	if (flg_send_now == true){
		flg_send_now = false;
		udp.sendPacket((uint8_t *) &pkt_to_send, 22, ai_ip_dest[ai_primary_dest].server_ip, ai_ip_dest[ai_primary_dest].server_udp_port);
		
	}
	

	if (last_time < Time.now()){
		last_time = Time.now();
		_uptime++; //this is not fail safe, what if we get held up in a loop for longer than 1 sec, but it will increment
		if (_uptime == 1){						//things to run on the first time into the loop. 
			//main_check_psu_input_volts(false);	//if the health is too low we will also go back to sleep in this function if we are not booting here. 					
		}
		
		 if (_uptime %600 == 0){		//every 10 minutes
			OPITO_DEBUG ("Uptime = %d secs, Memory = %d bytes", _uptime , System.freeMemory());	
			
			cloud_sync_time ();
			main_check_psu_input_volts(false);
		 }
	}

	
	if (Particle.connected()) {
		Particle.process();
	}
    }

Here is my check and sleep function

    void main_check_psu_input_volts(bool device_booting ){
	uint32_t psu_voltage = 0;

	psu_voltage = analogRead(PSU_VOLTS);
	psu_voltage  = calcPSUVoltage_mV(psu_voltage);
	
	if (device_booting == false){
		if (battery_get_soc() <= 2000){
			sleep_deeply(60);
		}
	}
	
	battery_get_battv();
    }

Thanks for taking a look

I tried your sleep function with this test sketch and the Electron wakes on time and WKP just the same.

SYSTEM_THREAD(ENABLED)
SYSTEM_MODE(SEMI_AUTOMATIC)

#define RADIO Cellular

void sleep_deeply(long seconds_to_sleep){
    delay(1000);
    System.sleep(SLEEP_MODE_DEEP,seconds_to_sleep); // SLEEPS BUT NEVER WAKES
}

void setup() 
{
    pinMode(D7, OUTPUT);
    RADIO.on();
    RADIO.connect();
    waitUntil(RADIO.ready);
    digitalWrite(D7, HIGH);
}

void loop()
{
    Particle.connect();
    waitUntil(Particle.connected);
    sleep_deeply(60);
}

Hmm… Appreciate you taking a look,

I might try putting a Thread Lock in my sleep deeply function, and see if that helps.

Hi, I have loaded a local compile as I need to get to the bottom of this.

There is something wrong somewhere.

It’s gone to “sleep” but the RGB led is breathing blue (I don’t connect to anything)

It never wakes up

When I pause the processor as per the screen shot, you get the callstack as per above.

This is how I call it

	SINGLE_THREADED_BLOCK(){
		System.sleep(SLEEP_MODE_DEEP,seconds_to_sleep);
	}

I also wonder if the RTC is setting the wake interrupt, as I did have it stuck in a _wfi() at one point (without the Single Threaded Block) it never woke then either.

Hi,

I also have the same problem. I wrote a small piece of code to try and determine
the problem but no success ?

SYSTEM_THREAD(ENABLED)
SYSTEM_MODE(MANUAL);
long sleep;
void setup()
{
Serial.begin(9600);
delay(5000);
Serial.println(“Start1”);
}
void loop()
{
Serial.begin(9600);
Serial.println(“Start2”);
delay(1000);
sleep = 5;
System.sleep(SLEEP_MODE_SOFTPOWEROFF, sleep);
}

I have a another Electron running with 0.6.2 that working, but 0.6.3 seems to be the problem

Any help will be appreciated

Reduced it more but no luck

SYSTEM_MODE(MANUAL);
void setup()
{
Serial.begin(9600);
delay(5000);
Serial.println(“Start1”);
}
void loop()
{
Serial.begin(9600);
Serial.println(“Start2”);
delay(1000);
System.sleep(SLEEP_MODE_SOFTPOWEROFF, 15);
}

Hi,

After a week of debugging I found the reason my Electron would not wake up. ! My 3V3 was connected to WKP via NC switch ! I thought the switch was NO.

5 Likes

I’m experiencing this exact issue. I have a NO switch connected and on its fault condition it closes but the fault can last for days so I’m wanting the device to sleep again. But as I’ve found out on github issue threads, If the WKP pin is held high when a device already in deep sleep tries to wake, then the device will never wake up and require a manual reset. hehe.

As a work around I’m using a soft sleep for the fault condition which is working fine. And as I’m wanting the device to wake on a negative edge in this circumstance it is correct not to use the deep sleep mode while the fault line is held high.