Debug of system.sleep with SLEEP_MODE_DEEP

Hi all,
I am trying to do a very very simple loop to verify system.sleep with SLEEP_MODE_DEEP mode works the way I think it does. I am using an electron and using the Arduino monitor to see the COM3 port. Here is my code:

#include "SHT1x/SHT1x.h"

void setup()
{
    Serial.begin(9600); // Open serial connection to report values to host
    Serial.print("Start Cycle");    
    delay(5000);
}

void loop()
{
    float temp_c;
    float temp_f;
    float humidity;
    
    // Read values from the sensor
    temp_c = sht1x.readTemperatureC();
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();
Serial.print("   Cycle   ");
delay(5000);
System.sleep(SLEEP_MODE_DEEP, 300);
}

It works perfectly the first time, I get both the Start Cycle and the Cycle messages I expect. The electron then goes to sleep like its supposed to and wakes up 5 min later like it supposed to. But then nothing else on the COM port, and then the electron goes back to sleep for another 5 min. Since it HAS to see the Serial.print commands before going back to sleep and its obviously looping, the only thing I can think of the deep sleep messes up the COM port? Thanks for any help anyone can give!

SLEEP_MODE_DEEP should disconnect the USB serial port as soon as sleep begins. On the Mac, at least, the Particle CLI serial monitor disconnects at that point, as does the screen serial console. So I would expect to not get any serial data after deep sleep until you reconnect to the computer to the Photon or Electron USB serial port.

The stop mode sleep modes (like pin + time) that continue after the sleep ends do not reset the USB serial port, and in theory communication can continue after waking up from sleep. In practice, I have found this does not always work. Even though the terminal connection was still open, data would not flow occasionally after sleep. Putting another Serial.begin(9600) after a sleep call that can proceed through (rather than reset) after waking up solved that problem for me.

3 Likes