diff --git a/environments.ini b/environments.ini index e0b4caa..5652c69 100644 --- a/environments.ini +++ b/environments.ini @@ -336,7 +336,7 @@ build_flags = '-DLED_SEND_RECEIVE_ON=0' '-DGateway_Name="OpenMQTTGateway_LOLIN32LITE_BLE"' ;; Low power parameters, uncomment and fill credentials -; '-DDEFAULT_LOW_POWER_MODE=2' + '-DDEFAULT_LOW_POWER_MODE=2' ; '-DTimeBtwRead=155000' ; '-DScan_duration=2000' ; '-DAttemptBLEConnect=false' @@ -460,6 +460,7 @@ build_flags = '-DLED_SEND_RECEIVE_ON=1' '-DTRIGGER_GPIO=35' '-DIR_EMITTER_GPIO=17' + '-DDEFAULT_LOW_POWER_MODE=0' '-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_BLE_IR"' board_upload.speed = 921600 custom_description = Expandable module with BLE gateway, display, and little IR emitter @@ -484,6 +485,7 @@ build_flags = '-DTRIGGER_GPIO=37' '-DSLEEP_BUTTON=38' '-DINPUT_GPIO=39' + '-DDEFAULT_LOW_POWER_MODE=0' '-DGateway_Name="OpenMQTTGateway_ESP32_M5STACK_BLE"' board_upload.speed = 921600 custom_description = Expandable module with BLE gateway and display @@ -533,6 +535,7 @@ build_flags = '-DTRIGGER_GPIO=39' '-DIR_EMITTER_INVERTED=true' '-DIR_EMITTER_GPIO=9' + '-DDEFAULT_LOW_POWER_MODE=0' '-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_C_BLE_IR"' board_upload.speed = 1500000 custom_description = Expandable module with BLE gateway, display, and little IR emitter @@ -563,6 +566,7 @@ build_flags = '-DTRIGGER_GPIO=39' '-DIR_EMITTER_INVERTED=true' '-DIR_EMITTER_GPIO=9' + '-DDEFAULT_LOW_POWER_MODE=0' '-DGateway_Name="OpenMQTTGateway_ESP32_M5STICK_CP_BLE_IR"' board_upload.speed = 1500000 custom_description = Expandable module with BLE gateway, display, and little IR emitter diff --git a/main/User_config.h b/main/User_config.h index c038bcd..4596fb0 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -287,12 +287,13 @@ static_assert(MQTT_SECURE_SELF_SIGNED_INDEX_DEFAULT < (sizeof(certs_array) / siz #endif /*------------------DEEP SLEEP parameters ------------------*/ +//DEFAULT_LOW_POWER_MODE -1 to normal mode, low power mode can't be used on this build //DEFAULT_LOW_POWER_MODE 0 to normal mode (no power consumption optimisations) //DEFAULT_LOW_POWER_MODE 1 to activate deep sleep //DEFAULT_LOW_POWER_MODE 2 to activate deep sleep (LCD is turned OFF) #ifdef ESP32 # ifndef DEFAULT_LOW_POWER_MODE -# define DEFAULT_LOW_POWER_MODE 0 +# define DEFAULT_LOW_POWER_MODE -1 # endif int lowpowermode = DEFAULT_LOW_POWER_MODE; #endif diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index e6727d7..06aef47 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -741,12 +741,13 @@ void coreTask(void* pvParameters) { BLEconnect(); } dumpDevices(); + Log.trace(F("CoreTask stack free: %u" CR), uxTaskGetStackHighWaterMark(xCoreTaskHandle)); xSemaphoreGive(semaphoreBLEOperation); } else { Log.error(F("Failed to start scan - BLE busy" CR)); } } - if (lowpowermode) { + if (lowpowermode > 0) { lowPowerESP32(); int scan = atomic_exchange_explicit(&forceBTScan, 0, ::memory_order_seq_cst); // is this enough, it will wait the full deepsleep... if (scan == 1) BTforceScan(); @@ -764,51 +765,57 @@ void coreTask(void* pvParameters) { } } +# if DEFAULT_LOW_POWER_MODE != -1 void lowPowerESP32() { // low power mode Log.trace(F("Going to deep sleep for: %l s" CR), (TimeBtwRead / 1000)); deepSleep(TimeBtwRead * 1000); } void deepSleep(uint64_t time_in_us) { -# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) +# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) sleepScreen(); esp_sleep_enable_ext0_wakeup((gpio_num_t)SLEEP_BUTTON, LOW); -# endif +# endif Log.trace(F("Deactivating ESP32 components" CR)); if (BLEDevice::getInitialized()) BLEDevice::deinit(true); esp_bt_mem_release(ESP_BT_MODE_BTDM); // Ignore the deprecated warning, this call is necessary here. -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" adc_power_off(); -# pragma GCC diagnostic pop +# pragma GCC diagnostic pop esp_wifi_stop(); esp_deep_sleep(time_in_us); } +# else +void lowPowerESP32() {} +# endif void changelowpowermode(int newLowPowerMode) { +# if DEFAULT_LOW_POWER_MODE != -1 Log.notice(F("Changing LOW POWER mode to: %d" CR), newLowPowerMode); -# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) +# if defined(ZboardM5STACK) || defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) if (lowpowermode == 2) { -# ifdef ZboardM5STACK +# ifdef ZboardM5STACK M5.Lcd.wakeup(); -# endif -# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) +# endif +# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5TOUGH) M5.Axp.SetLDO2(true); M5.Lcd.begin(); -# endif +# endif } char lpm[2]; sprintf(lpm, "%d", newLowPowerMode); M5Print("Changing LOW POWER mode to:", lpm, ""); -# endif +# endif lowpowermode = newLowPowerMode; preferences.begin(Gateway_Short_Name, false); preferences.putUInt("lowpowermode", lowpowermode); preferences.end(); // Publish the states to update the controller switch status stateMeasures(); +# endif } void setupBT() { diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 31d876c..2faad36 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -1029,6 +1029,7 @@ void pubMqttDiscovery() { stateClassNone, //State Class "false", "true" //state_off, state_on ); +# if DEFAULT_LOW_POWER_MODE != -1 createDiscovery("switch", //set Type subjectSYStoMQTT, "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId "", "", "{{ value_json.lowpowermode | bool }}", //set availability_topic,device_class,value_template, @@ -1039,6 +1040,7 @@ void pubMqttDiscovery() { stateClassNone, //State Class "false", "true" //state_off, state_on ); +# endif # endif # endif }