Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added deep sleep support for ESP32 #600

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e2e25bb
Added deep sleep support for ESP32
EinfachArne May 2, 2019
58a5e93
Adjusted ESP32 deep sleep support to 8266 version
EinfachArne May 3, 2019
2ec2edd
Adjusted formating
EinfachArne May 3, 2019
bbead88
Extended documentation for deep-sleep (#579)
kleini May 10, 2019
e36b18b
Added support of different ESP32 deep sleep modes
EinfachArne May 20, 2019
c98eae2
Adjusted code structure
EinfachArne May 20, 2019
1e157d4
Fixed reorder warning (#578)
kleini Mar 24, 2019
51aa5a6
Add description for event reasons (Wi-Fi, MQTT)
stritti Jul 23, 2019
80556f7
Fixed device not reconnecting after MQTT connection loss (#611)
dakhnod Aug 3, 2019
9db189f
SSL support for MQTT connection (#577)
kleini Aug 10, 2019
88201f7
Config: fix buffer overflow when loading json config (#617)
iluminat23 Aug 10, 2019
67d6452
Migrate to ArduinoJson version 6 (#622)
mkfrey Oct 1, 2019
3a52ecc
stop stats on disconnect (#624)
bertmelis Oct 1, 2019
9ce06e2
includes are not needed if config mode is disabled
kleini Sep 26, 2019
71a766b
Fix potential MQTT topic buffer overflow (#633)
mkfrey Nov 1, 2019
91e9b63
Subscribing to messages using Homie.getMqttClient() is not reliable: …
peret2000 Nov 12, 2019
661bd74
Calling ESP.getSketchMD5() at BootNormal constructor crashes on ESP32…
nemidiy Dec 2, 2019
7818c73
Fix semicolon after define statement in LightOnOff example (#643)
bodiroga Dec 2, 2019
2093ce8
Optimize JSON code
mkfrey Oct 15, 2019
7b15526
fixed wrong WiFi DNS parsing of commit 2093ce8665366ce056d5ee5894e52c…
kleini Dec 15, 2019
104a09c
Fix topic copying for chunked messages (#645)
mkfrey Dec 19, 2019
50e37c3
Added deep sleep support for ESP32
EinfachArne May 2, 2019
201a9aa
Adjusted ESP32 deep sleep support to 8266 version
EinfachArne May 3, 2019
f9e25f8
Adjusted formating
EinfachArne May 3, 2019
6757c64
Added support of different ESP32 deep sleep modes
EinfachArne May 20, 2019
a12b8e0
Adjusted code structure
EinfachArne May 20, 2019
b1023ec
Merge branch 'develop-v3' of https://github.com/EinfachArne/homie-esp…
EinfachArne Dec 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ jobs:
- run:
name: install examples dependencies
command: platformio lib -g install Shutters@2.1.1 SonoffDual@1.1.0
- run: platformio ci ./examples/Broadcast --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/CustomSettings --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/DoorSensor --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/GlobalInputHandler --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/HookToEvents --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/IteadSonoff --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/IteadSonoffButton --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/LedStrip --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/LightOnOff --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/Ping --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/SingleButton --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/SonoffDualShutters --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/TemperatureSensor --board=esp01 --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/Broadcast --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/CustomSettings --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/DoorSensor --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/GlobalInputHandler --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/HookToEvents --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/IteadSonoff --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/IteadSonoffButton --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/LedStrip --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/LightOnOff --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/Ping --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/SingleButton --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/SonoffDualShutters --board=esp01_1m --board=nodemcuv2 --board=esp32dev
- run: platformio ci ./examples/TemperatureSensor --board=esp01_1m --board=nodemcuv2 --board=esp32dev

lint:
working_directory: ~/code
Expand Down
16 changes: 15 additions & 1 deletion docs/advanced-usage/compiler-flags.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Compiler flags can be used to remove certain functionality from homie-esp8266. This can become useful if your firmware gets too large in size and is not upgradable any more over OTA. On the other hand these compiler flags allow to remove features from homie-esp8266, that you do not require or do not use.
Compiler flags can be used to add or remove certain functionality from homie-esp8266.

Removing functionality can become useful if your firmware gets too large in size and is not upgradable any more over OTA. On the other hand these compiler flags allow to remove features from homie-esp8266, that you do not require or do not use.

Adding functionality or features is useful to enable only partly implemented features or unstable or experimental features.

**HOMIE_CONFIG**

Expand All @@ -20,4 +24,14 @@ build_flags = -D HOMIE_MDNS=0

This reduces the firmware size by about 6400 bytes.

**ASYNC_TCP_SSL_ENABLED**

This compiler flag allows to use SSL encryption for MQTT connections. All other network connections still can not be encrypted like HTTP or OTA.

```
build_flags =
-D ASYNC_TCP_SSL_ENABLED=1
-D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
```

The additional flag `PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWITH` is necessary for SSL encryptions to work properly.
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();
}
```
40 changes: 40 additions & 0 deletions docs/advanced-usage/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ void onHomieEvent(const HomieEvent& event) {
// Do whatever you want when Wi-Fi is disconnected in normal mode

// You can use event.wifiReason
/*
Wi-Fi Reason (souce: https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino)
0 SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready
1 SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP
2 SYSTEM_EVENT_STA_START < ESP32 station start
3 SYSTEM_EVENT_STA_STOP < ESP32 station stop
4 SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP
5 SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP
6 SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed
7 SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP
8 SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0
9 SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode
10 SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode
11 SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode
12 SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode
13 SYSTEM_EVENT_AP_START < ESP32 soft-AP start
14 SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop
15 SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP
16 SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP
17 SYSTEM_EVENT_AP_STAIPASSIGNED < ESP32 soft-AP assign an IP to a connected station
18 SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface
19 SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred
20 SYSTEM_EVENT_ETH_START < ESP32 ethernet start
21 SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop
22 SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up
23 SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down
24 SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP
25 SYSTEM_EVENT_MAX
*/
break;
case HomieEventType::MQTT_READY:
// Do whatever you want when MQTT is connected in normal mode
Expand All @@ -46,6 +75,17 @@ void onHomieEvent(const HomieEvent& event) {
// Do whatever you want when MQTT is disconnected in normal mode

// You can use event.mqttReason
/*
MQTT Reason (source: https://github.com/marvinroger/async-mqtt-client/blob/master/src/AsyncMqttClient/DisconnectReasons.hpp)
0 TCP_DISCONNECTED
1 MQTT_UNACCEPTABLE_PROTOCOL_VERSION
2 MQTT_IDENTIFIER_REJECTED
3 MQTT_SERVER_UNAVAILABLE
4 MQTT_MALFORMED_CREDENTIALS
5 MQTT_NOT_AUTHORIZED
6 ESP8266_NOT_ENOUGH_SPACE
7 TLS_BAD_FINGERPRINT
*/
break;
case HomieEventType::MQTT_PACKET_ACKNOWLEDGED:
// Do whatever you want when an MQTT packet with QoS > 0 is acknowledged by the broker
Expand Down
5 changes: 4 additions & 1 deletion docs/configuration/json-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Below is the format of the JSON configuration you will have to provide:
"base_topic": "devices/",
"auth": true,
"username": "user",
"password": "pass"
"password": "pass",
"ssl": true,
"ssl_fingerprint": "a27992d3420c89f293d351378ba5f5675f74fe3c"
},
"ota": {
"enabled": true
Expand All @@ -46,6 +48,7 @@ Here are the rules:
- `bssid` and `channel` have to be defined together and these settings are independand of settings related to static IP
- to define static IP, `ip` (IP address), `mask` (netmask) and `gw` (gateway) settings have to be defined at the same time
- to define second DNS `dns2` the first one `dns1` has to be defined. Set DNS without `ip`, `mask` and `gw` does not affect the configuration (dns server will be provided by DHCP). It is not required to set DNS servers.
* `ssl_fingerprint` can optionally be defined if `ssl` is enabled. The public key of the MQTT server is then verified against the fingerprint.

Default values if not provided:

Expand Down
2 changes: 1 addition & 1 deletion docs/others/limitations-and-known-issues.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SSL support

In Homie for ESP8266 v1.x, SSL was possible but it was not reliable. Due to the asynchronous nature of the v2.x, SSL is not available anymore.
In Homie for ESP8266 v1.x, SSL was possible but it was not reliable. Due to the asynchronous nature of the v2.x, SSL is not completely available anymore. Only MQTT connections can be encrypted with SSL.

# ADC readings

Expand Down
2 changes: 1 addition & 1 deletion examples/LightOnOff/LightOnOff.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Homie.h>

#define firmwareVersion "1.0.0";
#define firmwareVersion "1.0.0"
const int PIN_RELAY = 5;

HomieNode lightNode("light", "Light", "switch");
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": [
{
"name": "ArduinoJson",
"version": "^5.10.0"
"version": "^6.11.4"
},
{
"name": "AsyncMqttClient",
Expand Down
17 changes: 14 additions & 3 deletions src/Homie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ Logger& HomieClass::getLogger() {
return _logger;
}

#ifdef ESP32
//FIXME: implement for ESP32
#elif defined(ESP8266)
void HomieClass::prepareToSleep() {
Interface::get().getLogger() << F("Flagged for sleep by sketch") << endl;
if (Interface::get().ready) {
Expand All @@ -369,6 +366,20 @@ void HomieClass::prepareToSleep() {
}
}

#ifdef ESP32
void HomieClass::doDeepSleep(uint64_t time_us, gpio_num_t gpio_pin, uint8_t logic_level) {
Interface::get().getLogger() << F("💤 Device is deep sleeping...") << endl;
Serial.flush();

if (gpio_pin >= 0 && logic_level >= 0) {
esp_sleep_enable_ext0_wakeup(gpio_pin, logic_level);
} else {
esp_sleep_enable_timer_wakeup(time_us);
}
esp_deep_sleep_start();
}
#elif defined(ESP8266)

void HomieClass::doDeepSleep(uint32_t time_us, RFMode mode) {
Interface::get().getLogger() << F("💤 Device is deep sleeping...") << endl;
Serial.flush();
Expand Down
4 changes: 2 additions & 2 deletions src/Homie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class HomieClass {
static const ConfigStruct& getConfiguration();
AsyncMqttClient& getMqttClient();
Logger& getLogger();
static void prepareToSleep();
#ifdef ESP32
//FIXME implement on ESP32
static void doDeepSleep(uint64_t time_us = 0, gpio_num_t gpio_pin = (gpio_num_t)-1, uint8_t logic_level = -1);
#elif defined(ESP8266)
static void prepareToSleep();
static void doDeepSleep(uint32_t time_us = 0, RFMode mode = RF_DEFAULT);
#endif // ESP32

Expand Down
Loading