Skip to content

Commit

Permalink
Extended documentation for deep-sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
kleini committed May 9, 2019
1 parent 5b56ebc commit 4f27a0f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions docs/advanced-usage/deep-sleep.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
Before deep sleeping, you will want to ensure that all messages are sent, including the `$online → false`. To do that, you can call `Homie.prepareToSleep()`. This will disconnect everything cleanly, so that you can call `ESP.deepSleep()`.
Before deep sleeping, you will want to ensure that all messages are sent, including property values of your `HomieNode` and the `$online → false`. To do that, you can call `Homie.prepareToSleep()`. But the event `MQTT_READY` is emitted before your `HomieNode`s `loop()` method is called. Therefore you need to postpone the call to `Homie.prepareToSleep()` until the `loop()` method of all `HomieNode`s is called and their properties are submitted over MQTT. For that the additional Timer is necessary. Then `Homie.prepareToSleep()` will disconnect everything cleanly, so that you can call Homie.doDeepSleep()`.

```c++
#include <Homie.h>
#include "Timer.h"

Timer t;

void prepareSleep() {
Homie.prepareToSleep();
}

void onHomieEvent(const HomieEvent& event) {
switch(event.type) {
case HomieEventType::MQTT_READY:
Homie.getLogger() << "MQTT connected, preparing for deep sleep..." << endl;
Homie.prepareToSleep();
Homie.getLogger() << "MQTT connected, preparing for deep sleep after 100ms..." << endl;
t.after(100, prepareSleep);
break;
case HomieEventType::READY_TO_SLEEP:
Homie.getLogger() << "Ready to sleep" << endl;
Expand All @@ -25,5 +32,6 @@ void setup() {

void loop() {
Homie.loop();
t.update();
}
```

0 comments on commit 4f27a0f

Please sign in to comment.