Suspend failed ESP32 MQTT and NTP uplinks for power saving
Share one bounded WiFi outage and recovery schedule across MQTT, automatic NTP, Companion station ownership and WebConfig. Preserve enabled settings and foreground access while allowing normal sleep after confirmed physical shutdown. Honor failed SDK client stops before handing off WiFi, protect clean teardown and reconfiguration, and retry transient ESP-NOW channel restoration without repeated disconnects. Add production-method, SDK failure and real sleep regressions to CI. Validation: native MQTT/NTP/clock/reconnect suites; production power and wrapper failure tests; browser, room HTTP, OTA and combined bridge regressions; Full Heltec V4 repeater, room and companion builds with capability, flash and linked-memory checks.
This commit is contained in:
1 parent
f62cc7b527
commit
6b3f7df4d0
37 files changed
+3258
-82
No files matched your search
@@ -471,7 +471,7 @@ esp_err_t PsychicMqttClient::reconnect()
|
||||
|
||||
void PsychicMqttClient::disconnect()
|
||||
{
|
||||
if (_client == nullptr)
|
||||
if (_client == nullptr || !_started)
|
||||
{
|
||||
ESP_LOGW(TAG, "MQTT client not started.");
|
||||
return;
|
||||
@@ -481,16 +481,41 @@ void PsychicMqttClient::disconnect()
|
||||
{
|
||||
ESP_LOGI(TAG, "Disconnecting MQTT client.");
|
||||
_stopMqttClient = false;
|
||||
esp_mqtt_client_disconnect(_client);
|
||||
|
||||
// Wait for all disconnect events to be processed
|
||||
while (!_stopMqttClient)
|
||||
const esp_err_t disconnect_result = esp_mqtt_client_disconnect(_client);
|
||||
if (disconnect_result == ESP_OK)
|
||||
{
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
// The SDK can lose its disconnect notification during a failed
|
||||
// transport. Give callbacks one second, then use SDK stop to close
|
||||
// the transport instead of waiting forever on that notification.
|
||||
const uint32_t started_at = static_cast<uint32_t>(millis());
|
||||
const uint32_t poll_ticks = 10 / portTICK_PERIOD_MS;
|
||||
while (!_stopMqttClient
|
||||
&& static_cast<uint32_t>(millis() - started_at) < 1000U)
|
||||
{
|
||||
vTaskDelay(poll_ticks ? poll_ticks : 1);
|
||||
}
|
||||
if (!_stopMqttClient)
|
||||
{
|
||||
ESP_LOGW(TAG, "Disconnect notification timed out; stopping MQTT client.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "MQTT disconnect request failed: %s; stopping client.",
|
||||
esp_err_to_name(disconnect_result));
|
||||
}
|
||||
}
|
||||
|
||||
esp_mqtt_client_stop(_client);
|
||||
const esp_err_t stop_result = esp_mqtt_client_stop(_client);
|
||||
if (stop_result != ESP_OK)
|
||||
{
|
||||
// A failed stop is not permission for the station owner to disable
|
||||
// WiFi or reclaim transport buffers. isStarted() keeps that handoff
|
||||
// blocked until an owning-task retry actually stops the SDK client.
|
||||
ESP_LOGW(TAG, "MQTT client stop failed: %s", esp_err_to_name(stop_result));
|
||||
return;
|
||||
}
|
||||
_connected = false;
|
||||
_started = false;
|
||||
ESP_LOGI(TAG, "MQTT client stopped.");
|
||||
}
|
||||
|
||||
@@ -366,7 +366,8 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Disconnects the MQTT client from the server.
|
||||
* This call might be blocking until the client is stopped cleanly
|
||||
* Disconnect notification waits at most one second before SDK stop.
|
||||
* A failed SDK stop leaves isStarted() true so callers retain ownership.
|
||||
*/
|
||||
void disconnect();
|
||||
|
||||
@@ -494,10 +495,10 @@ private:
|
||||
esp_mqtt_client_handle_t _client = nullptr;
|
||||
esp_mqtt_client_config_t _mqtt_cfg;
|
||||
esp_mqtt_error_codes_t _lastError;
|
||||
bool _connected = false;
|
||||
bool _stopMqttClient = false;
|
||||
std::atomic<bool> _connected{false};
|
||||
std::atomic<bool> _stopMqttClient{false};
|
||||
bool _config_dirty = true;
|
||||
bool _started = false;
|
||||
std::atomic<bool> _started{false};
|
||||
|
||||
// Runtime cap on the esp-mqtt outbox for QoS 0 async publishes (bytes).
|
||||
// 0 = disabled. Enforced in publish(); not an esp-mqtt config field.
|
||||
|
||||
Reference in new issue
Block a user