Optimizations when using low power mode (#608)

This commit is contained in:
Florian authored and GitHub committed 2020-05-07 17:28:27 -05:00
1 parent f23fcb672a
commit a82bbe684a
8 files changed
+43 -35

No files matched your search

+4
View File
@@ -13,6 +13,10 @@ OpenMQTTGateway support a low power mode for ESP32, this mode can be set by MQTT
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoBT/config" -m '{"low_power_mode":2}'` `mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoBT/config" -m '{"low_power_mode":2}'`
::: warning
If you change the default low power mode in config_BT.h to 2 and your credential are not set or not correct, the ESP32 will not connect to the broker and the only way to change the low power mode will be a new erase/upload.
:::
## M5Stick C or M5Stack ## M5Stick C or M5Stack
### Behaviour ### Behaviour
+1
View File
@@ -88,6 +88,7 @@
/*-------------DEFINE YOUR ADVANCED NETWORK PARAMETERS BELOW----------------*/ /*-------------DEFINE YOUR ADVANCED NETWORK PARAMETERS BELOW----------------*/
//#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work //#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work
#define maxConnectionRetry 10 //maximum MQTT connection attempts before going to wifimanager setup if never connected once #define maxConnectionRetry 10 //maximum MQTT connection attempts before going to wifimanager setup if never connected once
#define maxConnectionRetryWifi 5 //maximum Wifi connection attempts with existing credential at start (used to bypass ESP32 issue on wifi connect)
//set minimum quality of signal so it ignores AP's under that quality //set minimum quality of signal so it ignores AP's under that quality
#define MinimumWifiSignalQuality 8 #define MinimumWifiSignalQuality 8
+2 -2
View File
@@ -64,14 +64,14 @@ void setupM5()
wakeScreen(NORMAL_LCD_BRIGHTNESS); wakeScreen(NORMAL_LCD_BRIGHTNESS);
M5.Lcd.fillScreen(WHITE); M5.Lcd.fillScreen(WHITE);
displayIntro(M5.Lcd.width() * 0.25, (M5.Lcd.width() / 2) + M5.Lcd.width() * 0.12, (M5.Lcd.height()/2) + M5.Lcd.height() * 0.2); displayIntro(M5.Lcd.width() * 0.25, (M5.Lcd.width() / 2) + M5.Lcd.width() * 0.12, (M5.Lcd.height()/2) + M5.Lcd.height() * 0.2);
#ifdef LOG_TO_LCD #if LOG_TO_LCD
Log.begin(LOG_LEVEL_LCD, &M5.Lcd); // Log on LCD following LOG_LEVEL_LCD Log.begin(LOG_LEVEL_LCD, &M5.Lcd); // Log on LCD following LOG_LEVEL_LCD
#endif #endif
break; break;
case 1: case 1:
wakeScreen(SLEEP_LCD_BRIGHTNESS); wakeScreen(SLEEP_LCD_BRIGHTNESS);
M5.Lcd.fillScreen(WHITE); M5.Lcd.fillScreen(WHITE);
#ifdef LOG_TO_LCD #if LOG_TO_LCD
Log.begin(LOG_LEVEL_LCD, &M5.Lcd); // Log on LCD following LOG_LEVEL_LCD Log.begin(LOG_LEVEL_LCD, &M5.Lcd); // Log on LCD following LOG_LEVEL_LCD
#endif #endif
break; break;
-4
View File
@@ -593,7 +593,6 @@ void coreTask(void *pvParameters)
void lowPowerESP32() // low power mode void lowPowerESP32() // low power mode
{ {
stateMeasures(); // publish MQTT SYS data before going to sleep
Log.trace(F("Going to deep sleep for: %l s" CR), (BLEinterval / 1000)); Log.trace(F("Going to deep sleep for: %l s" CR), (BLEinterval / 1000));
deepSleep(BLEinterval * 1000); deepSleep(BLEinterval * 1000);
} }
@@ -605,7 +604,6 @@ void deepSleep(uint64_t time_in_us)
esp_sleep_enable_ext0_wakeup((gpio_num_t)SLEEP_BUTTON, LOW); esp_sleep_enable_ext0_wakeup((gpio_num_t)SLEEP_BUTTON, LOW);
#endif #endif
Log.trace(F("Deactivating ESP32 components" CR)); Log.trace(F("Deactivating ESP32 components" CR));
esp_wifi_stop(); esp_wifi_stop();
esp_bluedroid_disable(); esp_bluedroid_disable();
@@ -644,8 +642,6 @@ void changelow_power_mode(int newLowPowerMode)
void setupBT() void setupBT()
{ {
BLEinterval = TimeBtw_Read;
Minrssi = MinimumRSSI;
Log.notice(F("BLEinterval: %d" CR), BLEinterval); Log.notice(F("BLEinterval: %d" CR), BLEinterval);
Log.notice(F("Minrssi: %d" CR), Minrssi); Log.notice(F("Minrssi: %d" CR), Minrssi);
Log.notice(F("Low Power Mode: %d" CR), low_power_mode); Log.notice(F("Low Power Mode: %d" CR), low_power_mode);
+2 -2
View File
@@ -55,8 +55,8 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata);
// if not commented Home presence integration with HOME ASSISTANT is activated // if not commented Home presence integration with HOME ASSISTANT is activated
#define subjectHomePresence "home_presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) #define subjectHomePresence "home_presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name)
unsigned int BLEinterval ; //time between 2 scans unsigned int BLEinterval = TimeBtw_Read; //time between 2 scans
int Minrssi ; //minimum rssi value int Minrssi = MinimumRSSI; //minimum rssi value
struct BLEdevice{ struct BLEdevice{
char macAdr[13]; char macAdr[13];
+1 -1
View File
@@ -45,7 +45,7 @@ extern void loopM5();
#endif #endif
/*-------------------DEFINE LOG LEVEL----------------------*/ /*-------------------DEFINE LOG LEVEL----------------------*/
#define LOG_LEVEL_LCD LOG_LEVEL_WARNING // if we go down below warning the size of the text to display can make the M5 restarting #define LOG_LEVEL_LCD LOG_LEVEL_WARNING // if we go down below warning the size of the text to display can make the M5 restarting
#define LOG_TO_LCD true //comment if you want to use serial monitor for the log per default instead of the M5 screen #define LOG_TO_LCD true //set to false if you want to use serial monitor for the log per default instead of the M5 screen
/*-------------------DEFINE MQTT TOPIC FOR CONFIG----------------------*/ /*-------------------DEFINE MQTT TOPIC FOR CONFIG----------------------*/
#define subjectMQTTtoM5set "/commands/MQTTtoM5/config" #define subjectMQTTtoM5set "/commands/MQTTtoM5/config"
+32 -26
View File
@@ -462,6 +462,7 @@ void connectMQTT()
if (client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message)) if (client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message))
{ {
#if defined(ZboardM5STICKC) || defined(ZboardM5STACK) #if defined(ZboardM5STICKC) || defined(ZboardM5STACK)
if (low_power_mode < 2)
M5Display("MQTT connected", "", ""); M5Display("MQTT connected", "", "");
#endif #endif
Log.notice(F("Connected to broker" CR)); Log.notice(F("Connected to broker" CR));
@@ -681,16 +682,16 @@ void setup()
bool wifi_reconnect_bypass(){ bool wifi_reconnect_bypass(){
uint8_t wifi_autoreconnect_cnt = 0; uint8_t wifi_autoreconnect_cnt = 0;
#ifdef ESP32 #ifdef ESP32
while (WiFi.status() != WL_CONNECTED && wifi_autoreconnect_cnt < 3) { while (WiFi.status() != WL_CONNECTED && wifi_autoreconnect_cnt < maxConnectionRetryWifi) {
#else #else
while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_autoreconnect_cnt < 3) { while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_autoreconnect_cnt < maxConnectionRetryWifi) {
#endif #endif
Log.notice(F("Attempting Wifi connection with saved AP: %d" CR), wifi_autoreconnect_cnt); Log.notice(F("Attempting Wifi connection with saved AP: %d" CR), wifi_autoreconnect_cnt);
WiFi.begin(); WiFi.begin();
delay(500); delay(500);
wifi_autoreconnect_cnt++; wifi_autoreconnect_cnt++;
} }
if(wifi_autoreconnect_cnt < 3) if(wifi_autoreconnect_cnt < maxConnectionRetryWifi)
{ {
return true; return true;
} else { } else {
@@ -721,12 +722,10 @@ void disconnection_handling( int failure_number){
Log.warning(F("disconnection_handling, failed %d times" CR), failure_number); Log.warning(F("disconnection_handling, failed %d times" CR), failure_number);
if (failure_number > maxConnectionRetry && !connectedOnce) if (failure_number > maxConnectionRetry && !connectedOnce)
{ {
#if defined(ESP8266) || defined(ESP32)
#ifndef ESPWifiManualSetup #ifndef ESPWifiManualSetup
Log.error(F("Failed connecting 1st time to mqtt, reset wifi manager & erase network credentials" CR)); Log.error(F("Failed connecting 1st time to mqtt, reset wifi manager & erase network credentials" CR));
setup_wifimanager(true); setup_wifimanager(true);
#endif #endif
#endif
} }
else if (failure_number <= maxConnectionRetry + ATTEMPTS_BEFORE_BG && connectedOnce) else if (failure_number <= maxConnectionRetry + ATTEMPTS_BEFORE_BG && connectedOnce)
{ {
@@ -737,8 +736,7 @@ void disconnection_handling( int failure_number){
{ {
#ifdef ESP32 #ifdef ESP32
wifiProtocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G; wifiProtocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G;
#endif #elif ESP8266
#ifdef ESP8266
wifiProtocol = WIFI_PHY_MODE_11G; wifiProtocol = WIFI_PHY_MODE_11G;
#endif #endif
Log.warning(F("Wifi Protocol changed to WIFI_11G: %d" CR), wifiProtocol); Log.warning(F("Wifi Protocol changed to WIFI_11G: %d" CR), wifiProtocol);
@@ -748,8 +746,7 @@ void disconnection_handling( int failure_number){
{ {
#ifdef ESP32 #ifdef ESP32
wifiProtocol = WIFI_PROTOCOL_11B; wifiProtocol = WIFI_PROTOCOL_11B;
#endif #elif ESP8266
#ifdef ESP8266
wifiProtocol = WIFI_PHY_MODE_11B; wifiProtocol = WIFI_PHY_MODE_11B;
#endif #endif
Log.warning(F("Wifi Protocol changed to WIFI_11B: %d" CR), wifiProtocol); Log.warning(F("Wifi Protocol changed to WIFI_11B: %d" CR), wifiProtocol);
@@ -759,8 +756,7 @@ void disconnection_handling( int failure_number){
{ {
#ifdef ESP32 #ifdef ESP32
wifiProtocol = 0; wifiProtocol = 0;
#endif #elif ESP8266
#ifdef ESP8266
wifiProtocol = 0; wifiProtocol = 0;
#endif #endif
Log.warning(F("Wifi Protocol reverted to normal mode: %d" CR), wifiProtocol); Log.warning(F("Wifi Protocol reverted to normal mode: %d" CR), wifiProtocol);
@@ -813,7 +809,7 @@ void setup_wifi()
if (!wifiProtocol) forceWifiProtocol(); if (!wifiProtocol) forceWifiProtocol();
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
Log.trace(F("Connecting to %s" CR),wifi_ssid); Log.trace(F("Connecting to %s" CR), wifi_ssid);
#ifdef ESPWifiAdvancedSetup #ifdef ESPWifiAdvancedSetup
IPAddress ip_adress(ip); IPAddress ip_adress(ip);
IPAddress gateway_adress(gateway); IPAddress gateway_adress(gateway);
@@ -828,7 +824,8 @@ void setup_wifi()
WiFi.begin(wifi_ssid, wifi_password); WiFi.begin(wifi_ssid, wifi_password);
#endif #endif
wifi_reconnect_bypass(); if(wifi_reconnect_bypass());
Log.notice(F("Connected with saved credentials" CR));
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
@@ -1001,10 +998,21 @@ void setup_wifimanager(bool reset_settings)
wifiManager.setMinimumSignalQuality(MinimumWifiSignalQuality); wifiManager.setMinimumSignalQuality(MinimumWifiSignalQuality);
if(!wifi_reconnect_bypass())// if we didn't connect with saved credential we start Wifimanager web portal if(!wifi_reconnect_bypass())// if we didn't connect with saved credential we start Wifimanager web portal
{
#ifdef ESP32
if (low_power_mode < 2)
{ {
#if defined(ZboardM5STICKC) || defined(ZboardM5STACK) #if defined(ZboardM5STICKC) || defined(ZboardM5STACK)
M5Display("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password); M5Display("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password);
#endif #endif
} else { // in case of low power mode we put the ESP to sleep again if we didn't get connected (typical in case the wifi is down)
#ifdef ZgatewayBT
lowPowerESP32();
#endif
}
#endif
Log.notice(F("Connect your phone to WIFI AP: %s with PWD: %s" CR), WifiManager_ssid, WifiManager_password );
//fetches ssid and pass and tries to connect //fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name //if it does not connect it starts an access point with the specified name
//and goes into a blocking loop awaiting configuration //and goes into a blocking loop awaiting configuration
@@ -1023,12 +1031,10 @@ void setup_wifimanager(bool reset_settings)
} }
#if defined(ZboardM5STICKC) || defined(ZboardM5STACK) #if defined(ZboardM5STICKC) || defined(ZboardM5STACK)
if (low_power_mode < 2)
M5Display("Wifi connected", "", ""); M5Display("Wifi connected", "", "");
#endif #endif
//if you get here you are connected to the WiFi
Log.notice(F("Wifi connected" CR));
//read updated parameters //read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue());
@@ -1160,6 +1166,13 @@ void loop()
ArduinoOTA.handle(); ArduinoOTA.handle();
#endif #endif
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures)
{
timer_sys_measures = millis();
stateMeasures();
}
#endif
#ifdef ZsensorBME280 #ifdef ZsensorBME280
MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280 MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280
#endif #endif
@@ -1231,13 +1244,6 @@ void loop()
if (RFM69toMQTT()) if (RFM69toMQTT())
Log.trace(F("RFM69toMQTT OK" CR)); Log.trace(F("RFM69toMQTT OK" CR));
#endif #endif
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)))
{
timer_sys_measures = millis();
stateMeasures();
}
#endif
#ifdef ZactuatorFASTLED #ifdef ZactuatorFASTLED
FASTLEDLoop(); FASTLEDLoop();
#endif #endif
@@ -1276,7 +1282,7 @@ void stateMeasures()
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
uint32_t freeMem; uint32_t freeMem;
freeMem = ESP.getFreeHeap(); freeMem = ESP.getFreeHeap();
SYSdata["freeMem"] = freeMem; SYSdata["freemem"] = freeMem;
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
SYSdata["rssi"] = rssi; SYSdata["rssi"] = rssi;
String SSID = WiFi.SSID(); String SSID = WiFi.SSID();
@@ -1284,7 +1290,7 @@ void stateMeasures()
SYSdata["ip"] = ip2CharArray(WiFi.localIP()); SYSdata["ip"] = ip2CharArray(WiFi.localIP());
String mac = WiFi.macAddress(); String mac = WiFi.macAddress();
SYSdata["mac"] = (char *)mac.c_str(); SYSdata["mac"] = (char *)mac.c_str();
SYSdata["wifiPrt"] = (int)wifiProtocol; SYSdata["wifiprt"] = (int)wifiProtocol;
#else #else
SYSdata["ip"] = ip2CharArray(Ethernet.localIP()); SYSdata["ip"] = ip2CharArray(Ethernet.localIP());
#endif #endif
@@ -1340,7 +1346,7 @@ void stateMeasures()
#ifdef ZgatewayBT #ifdef ZgatewayBT
modules = modules + ZgatewayBT; modules = modules + ZgatewayBT;
#ifdef ESP32 #ifdef ESP32
SYSdata["low_power_mode"] = (int)low_power_mode; SYSdata["lowpowermode"] = (int)low_power_mode;
#endif #endif
#endif #endif
#ifdef ZgatewayRFM69 #ifdef ZgatewayRFM69
+1
View File
@@ -276,6 +276,7 @@ build_flags =
${com-esp.build_flags} ${com-esp.build_flags}
'-DZgatewayBT="BT"' '-DZgatewayBT="BT"'
'-DLOW_POWER_LED=2' '-DLOW_POWER_LED=2'
'-DLOW_POWER_LED_OFF=1'
'-DGateway_Name="OpenMQTTGateway_ESP32_BLE"' '-DGateway_Name="OpenMQTTGateway_ESP32_BLE"'
[env:esp32-m5stick-ble] [env:esp32-m5stick-ble]