Author Topic: equiv of System.sleep(D0, CHANGE);  (Read 1778 times)

shiny

  • Newbie
  • *
  • Posts: 6
equiv of System.sleep(D0, CHANGE);
« on: August 06, 2016, 08:58:00 pm »
I want my oak to sleep sleep sleep, until D0 changes.
The particle docs say System.sleep(D0, CHANGE);

but that doesn't appear to be implemented on oak.
'class OakClass' has no member named 'sleep'

What are my options?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: equiv of System.sleep(D0, CHANGE);
« Reply #1 on: August 06, 2016, 11:40:52 pm »
That will be a challenge, as I suspect the only way the Particle boards can do the sleep with interrupt wakeup is due to a feature on the different MCU chip they use.

I'm thinking the best you'll be able to do is something *similar* to the modem sleep power-saving that the ESP8266 stuff uses, but will need some modifications so that the Particle OTA integration doesn't cause it to keep rebooting into safe mode.

For example, on the ESP-01 temperature logger I have, the below code at the bottom of my main loop. The ifdef blocks allows the code to work on either the Oak or ESP8266/ESP-01 boards, depending on which I'm using. On the Oak it will use the deepSleep powersave as the reset and GPIO16 pins are available, otherwise the only real option is to switch off the wireless (as the ESP-01 boards don't have those pins readily available), which is the main power hog.

So on the Oak, it deepSleeps for five minutes, then resets. On the ESP-01, it turns off the wireless  for five minutes, and, then starts it back up.  You could use the ESP-01 side of the power save (i.e.  WiFi.mode(WIFI_OFF); stuff), and instead of a five minute wait, have a periodic check to see if a flag triggered in an interrupt function (attachInterrupt()) has been raised, then restart the wireless. The other thing to consider for the Oak is you would probably have to use SYSTEM_MODE(SEMI_AUTOMATIC), so you can control the particle connections manually. You would then use Particle.connect() to connect to particle(maybe add to startWireless function?), and Particle.disconnect() to disconnect  from particle before you power off the wifi, otherwise the Oak would probably go into safe mode after a few minutes due to the lost particle connection.

If this doesn't make sense, let me know, and I'll try and write up some sample code....

Code: [Select]
  // power save for 5 minutes
#ifdef ARDUINO_ESP8266_OAK
  ESP.deepSleep(5 * 60 * 1000000, WAKE_RF_DEFAULT);
#else
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  yield();
  delay(5 * 60000);
  WiFi.forceSleepWake();
  yield();
  startWireless();
#endif

The startWireless function looks like this (and is used in the setup loop to start the wireless/wait for wireless connection):
Code: [Select]
void startWireless()
{
 //DebugPrint("Waiting for WiFi");
#ifdef ARDUINO_ESP8266_OAK
  WiFi.begin();
#else
  WiFi.begin(ssid, password);
#endif

  while ( WiFi.status() != WL_CONNECTED)
  {
#ifdef ARDUINO_ESP8266_OAK
    digitalWrite(STATUS_LED, HIGH);
#endif
    // wait 1 seconds for connection:

    delay(1000);
   //DebugPrint(".");

#ifdef ARDUINO_ESP8266_OAK
    digitalWrite(STATUS_LED, LOW);
#endif
  }
  //DebugPrintln(" CONNECTED!"); 
}
« Last Edit: August 06, 2016, 11:47:10 pm by PeterF »