diff --git a/docs/use/boards.md b/docs/use/boards.md index c658911..426cd7a 100644 --- a/docs/use/boards.md +++ b/docs/use/boards.md @@ -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}'` +::: 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 ### Behaviour diff --git a/main/User_config.h b/main/User_config.h index 9c7c3b3..9d2e005 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -87,7 +87,8 @@ /*-------------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 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 #define MinimumWifiSignalQuality 8 diff --git a/main/ZboardM5.ino b/main/ZboardM5.ino index ea40ddd..fd3eee7 100644 --- a/main/ZboardM5.ino +++ b/main/ZboardM5.ino @@ -64,14 +64,14 @@ void setupM5() wakeScreen(NORMAL_LCD_BRIGHTNESS); 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); - #ifdef LOG_TO_LCD + #if LOG_TO_LCD Log.begin(LOG_LEVEL_LCD, &M5.Lcd); // Log on LCD following LOG_LEVEL_LCD #endif break; case 1: wakeScreen(SLEEP_LCD_BRIGHTNESS); 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 #endif break; diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index cde2373..ad5d587 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -593,7 +593,6 @@ void coreTask(void *pvParameters) 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)); deepSleep(BLEinterval * 1000); } @@ -604,7 +603,6 @@ void deepSleep(uint64_t time_in_us) sleepScreen(); esp_sleep_enable_ext0_wakeup((gpio_num_t)SLEEP_BUTTON, LOW); #endif - Log.trace(F("Deactivating ESP32 components" CR)); esp_wifi_stop(); @@ -644,8 +642,6 @@ void changelow_power_mode(int newLowPowerMode) void setupBT() { - BLEinterval = TimeBtw_Read; - Minrssi = MinimumRSSI; Log.notice(F("BLEinterval: %d" CR), BLEinterval); Log.notice(F("Minrssi: %d" CR), Minrssi); Log.notice(F("Low Power Mode: %d" CR), low_power_mode); diff --git a/main/config_BT.h b/main/config_BT.h index e82d3e1..bbc7d1c 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -55,8 +55,8 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata); // 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) -unsigned int BLEinterval ; //time between 2 scans -int Minrssi ; //minimum rssi value +unsigned int BLEinterval = TimeBtw_Read; //time between 2 scans +int Minrssi = MinimumRSSI; //minimum rssi value struct BLEdevice{ char macAdr[13]; diff --git a/main/config_M5.h b/main/config_M5.h index 0ef9b09..a19972e 100644 --- a/main/config_M5.h +++ b/main/config_M5.h @@ -45,7 +45,7 @@ extern void loopM5(); #endif /*-------------------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_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 subjectMQTTtoM5set "/commands/MQTTtoM5/config" diff --git a/main/main.ino b/main/main.ino index a6ed5f5..59ba2be 100644 --- a/main/main.ino +++ b/main/main.ino @@ -462,7 +462,8 @@ void connectMQTT() if (client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message)) { #if defined(ZboardM5STICKC) || defined(ZboardM5STACK) - M5Display("MQTT connected", "", ""); + if (low_power_mode < 2) + M5Display("MQTT connected", "", ""); #endif Log.notice(F("Connected to broker" CR)); failure_number_mqtt = 0; @@ -681,16 +682,16 @@ void setup() bool wifi_reconnect_bypass(){ uint8_t wifi_autoreconnect_cnt = 0; #ifdef ESP32 - while (WiFi.status() != WL_CONNECTED && wifi_autoreconnect_cnt < 3) { + while (WiFi.status() != WL_CONNECTED && wifi_autoreconnect_cnt < maxConnectionRetryWifi) { #else - while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_autoreconnect_cnt < 3) { + while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_autoreconnect_cnt < maxConnectionRetryWifi) { #endif Log.notice(F("Attempting Wifi connection with saved AP: %d" CR), wifi_autoreconnect_cnt); WiFi.begin(); delay(500); wifi_autoreconnect_cnt++; } - if(wifi_autoreconnect_cnt < 3) + if(wifi_autoreconnect_cnt < maxConnectionRetryWifi) { return true; } else { @@ -721,12 +722,10 @@ void disconnection_handling( int failure_number){ Log.warning(F("disconnection_handling, failed %d times" CR), failure_number); if (failure_number > maxConnectionRetry && !connectedOnce) { - #if defined(ESP8266) || defined(ESP32) - #ifndef ESPWifiManualSetup - Log.error(F("Failed connecting 1st time to mqtt, reset wifi manager & erase network credentials" CR)); - setup_wifimanager(true); - #endif - #endif + #ifndef ESPWifiManualSetup + Log.error(F("Failed connecting 1st time to mqtt, reset wifi manager & erase network credentials" CR)); + setup_wifimanager(true); + #endif } else if (failure_number <= maxConnectionRetry + ATTEMPTS_BEFORE_BG && connectedOnce) { @@ -737,8 +736,7 @@ void disconnection_handling( int failure_number){ { #ifdef ESP32 wifiProtocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G; - #endif - #ifdef ESP8266 + #elif ESP8266 wifiProtocol = WIFI_PHY_MODE_11G; #endif Log.warning(F("Wifi Protocol changed to WIFI_11G: %d" CR), wifiProtocol); @@ -748,8 +746,7 @@ void disconnection_handling( int failure_number){ { #ifdef ESP32 wifiProtocol = WIFI_PROTOCOL_11B; - #endif - #ifdef ESP8266 + #elif ESP8266 wifiProtocol = WIFI_PHY_MODE_11B; #endif Log.warning(F("Wifi Protocol changed to WIFI_11B: %d" CR), wifiProtocol); @@ -759,8 +756,7 @@ void disconnection_handling( int failure_number){ { #ifdef ESP32 wifiProtocol = 0; - #endif - #ifdef ESP8266 + #elif ESP8266 wifiProtocol = 0; #endif Log.warning(F("Wifi Protocol reverted to normal mode: %d" CR), wifiProtocol); @@ -813,7 +809,7 @@ void setup_wifi() if (!wifiProtocol) forceWifiProtocol(); // 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 IPAddress ip_adress(ip); IPAddress gateway_adress(gateway); @@ -828,7 +824,8 @@ void setup_wifi() WiFi.begin(wifi_ssid, wifi_password); #endif - wifi_reconnect_bypass(); + if(wifi_reconnect_bypass()); + Log.notice(F("Connected with saved credentials" CR)); while (WiFi.status() != WL_CONNECTED) { @@ -1002,9 +999,20 @@ void setup_wifimanager(bool reset_settings) if(!wifi_reconnect_bypass())// if we didn't connect with saved credential we start Wifimanager web portal { - #if defined(ZboardM5STICKC) || defined(ZboardM5STACK) - M5Display("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password); + #ifdef ESP32 + if (low_power_mode < 2) + { + #if defined(ZboardM5STICKC) || defined(ZboardM5STACK) + M5Display("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password); + #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 //if it does not connect it starts an access point with the specified name //and goes into a blocking loop awaiting configuration @@ -1023,12 +1031,10 @@ void setup_wifimanager(bool reset_settings) } #if defined(ZboardM5STICKC) || defined(ZboardM5STACK) - M5Display("Wifi connected", "", ""); + if (low_power_mode < 2) + M5Display("Wifi connected", "", ""); #endif - //if you get here you are connected to the WiFi - Log.notice(F("Wifi connected" CR)); - //read updated parameters strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); @@ -1160,6 +1166,13 @@ void loop() ArduinoOTA.handle(); #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 MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280 #endif @@ -1231,13 +1244,6 @@ void loop() if (RFM69toMQTT()) Log.trace(F("RFM69toMQTT OK" CR)); #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 FASTLEDLoop(); #endif @@ -1276,7 +1282,7 @@ void stateMeasures() #if defined(ESP8266) || defined(ESP32) uint32_t freeMem; freeMem = ESP.getFreeHeap(); - SYSdata["freeMem"] = freeMem; + SYSdata["freemem"] = freeMem; long rssi = WiFi.RSSI(); SYSdata["rssi"] = rssi; String SSID = WiFi.SSID(); @@ -1284,7 +1290,7 @@ void stateMeasures() SYSdata["ip"] = ip2CharArray(WiFi.localIP()); String mac = WiFi.macAddress(); SYSdata["mac"] = (char *)mac.c_str(); - SYSdata["wifiPrt"] = (int)wifiProtocol; + SYSdata["wifiprt"] = (int)wifiProtocol; #else SYSdata["ip"] = ip2CharArray(Ethernet.localIP()); #endif @@ -1340,7 +1346,7 @@ void stateMeasures() #ifdef ZgatewayBT modules = modules + ZgatewayBT; #ifdef ESP32 - SYSdata["low_power_mode"] = (int)low_power_mode; + SYSdata["lowpowermode"] = (int)low_power_mode; #endif #endif #ifdef ZgatewayRFM69 diff --git a/platformio.ini b/platformio.ini index f6da082..02c2506 100644 --- a/platformio.ini +++ b/platformio.ini @@ -276,6 +276,7 @@ build_flags = ${com-esp.build_flags} '-DZgatewayBT="BT"' '-DLOW_POWER_LED=2' + '-DLOW_POWER_LED_OFF=1' '-DGateway_Name="OpenMQTTGateway_ESP32_BLE"' [env:esp32-m5stick-ble]