Help with keeping Boron asleep until it receives a suscribed event

I have a Boron which I’d like to keep asleep to save power and only wake when it receives an event on a subscribed topic over cellular, then show information on an e-ink display. This will be a solar + battery setup.

From the docs, I saw that using SystemSleepMode::STOP in the following manner will keep the device connected to cellular during sleep, and also wake on received events:

  sleepConfig
      .mode(SystemSleepMode::STOP)
      .network(NETWORK_INTERFACE_CELLULAR)
      .duration(1min);

What I’m curious about is the best-practice way to have the device stay asleep, wake on a received event and process it, then sleep automatically. I can’t seem to find much about exactly how Particle OS handles received events during this kind of sleep mode, and would like to learn more. Does it wait until the subscription handler returns before running loop and sleeping again?

Below is what I have so far, which seems to work (the LED indicates it’s receiving data from the Cloud) but it’s a bit hard to tell because the serial connection is lost when it sleeps:

SYSTEM_MODE(AUTOMATIC);

SystemSleepConfiguration sleepConfig;

void setup()
{
  Particle.subscribe("siri_wait_times", handleWaitTimeSubscription);

  waitUntil(Cellular.ready);
  waitUntil(Particle.connected);

  sleepConfig
      .mode(SystemSleepMode::STOP)
      .network(NETWORK_INTERFACE_CELLULAR)
      .duration(1min);
}

void loop()
{
  // sleep immediately
  System.sleep(sleepConfig);
}

Thanks in advance.

That's close. The duration is the amount of time to stay asleep even if you don't have an event, so you may want a duration longer than 1 minute.

In stop sleep mode, the device continues from the line after the System.sleep command when waking up. This means that the device will immediately go back to sleep with your code, because loop will run again 1 millisecond after you return from loop.

If you do have an event, your subscription callback will occur asynchronously. This means you'll need to put in some way to wait a short period of time for this notification to arrive (100 millisecond should be enough), and also account for the updating of the display before going to sleep.

2 Likes