Compare commits
7 Commits
fix/improv
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 614594a035 | |||
| dd881f79f3 | |||
| 980a42f98b | |||
| 69a2e04cd2 | |||
| 669ef89a66 | |||
| 0905aa4bf8 | |||
| 91d144901a |
@ -17,6 +17,7 @@ MQTTBridge::MQTTBridge(INetworkManager* network, mesh::PacketManager* mgr, mesh:
|
||||
_last_status_publish(0),
|
||||
_last_stats_publish(0),
|
||||
_connected_since(0),
|
||||
_current_backoff_ms(BACKOFF_MIN_MS),
|
||||
_messages_sent(0),
|
||||
_messages_received(0),
|
||||
_reconnect_count(0),
|
||||
@ -81,7 +82,8 @@ void MQTTBridge::loop() {
|
||||
|
||||
switch (_state) {
|
||||
case MQTTState::DISCONNECTED:
|
||||
if (millis() - _last_connect_attempt > 5000) {
|
||||
// Use exponential backoff for reconnection attempts
|
||||
if (millis() - _last_connect_attempt > _current_backoff_ms) {
|
||||
attemptConnection();
|
||||
}
|
||||
break;
|
||||
@ -94,6 +96,7 @@ void MQTTBridge::loop() {
|
||||
_state = MQTTState::DISCONNECTED;
|
||||
Serial.println("[MQTT] Connection lost");
|
||||
_last_connect_attempt = millis();
|
||||
// Don't reset backoff on connection loss - broker might be down
|
||||
} else {
|
||||
_mqtt_client.loop();
|
||||
|
||||
@ -105,7 +108,8 @@ void MQTTBridge::loop() {
|
||||
break;
|
||||
|
||||
case MQTTState::ERROR:
|
||||
if (millis() - _last_connect_attempt > 30000) {
|
||||
// Use backoff for error recovery too
|
||||
if (millis() - _last_connect_attempt > _current_backoff_ms) {
|
||||
_state = MQTTState::DISCONNECTED;
|
||||
}
|
||||
break;
|
||||
@ -135,7 +139,8 @@ void MQTTBridge::attemptConnection() {
|
||||
_state = MQTTState::CONNECTING;
|
||||
_last_connect_attempt = millis();
|
||||
|
||||
Serial.printf("[MQTT] Connecting to %s:%d...\n", _config.broker, _config.port);
|
||||
Serial.printf("[MQTT] Connecting to %s:%d (backoff=%lums)...\n",
|
||||
_config.broker, _config.port, _current_backoff_ms);
|
||||
|
||||
String client_id = String(_config.client_id);
|
||||
if (client_id.length() == 0) {
|
||||
@ -158,6 +163,8 @@ void MQTTBridge::attemptConnection() {
|
||||
_state = MQTTState::CONNECTED;
|
||||
_connected_since = millis();
|
||||
_reconnect_count++;
|
||||
// Reset backoff on successful connection
|
||||
_current_backoff_ms = BACKOFF_MIN_MS;
|
||||
Serial.println("[MQTT] Connected!");
|
||||
|
||||
subscribeToCommands();
|
||||
@ -170,6 +177,10 @@ void MQTTBridge::attemptConnection() {
|
||||
Serial.printf("[MQTT] Broker: %s:%d, User: %s\n", _config.broker, _config.port,
|
||||
strlen(_config.user) > 0 ? _config.user : "(none)");
|
||||
_state = MQTTState::ERROR;
|
||||
|
||||
// Exponential backoff: double the delay (up to max)
|
||||
_current_backoff_ms = min(_current_backoff_ms * BACKOFF_MULTIPLIER, BACKOFF_MAX_MS);
|
||||
Serial.printf("[MQTTS] Next retry in %lums\n", _current_backoff_ms);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -85,6 +85,12 @@ private:
|
||||
unsigned long _last_stats_publish;
|
||||
unsigned long _connected_since;
|
||||
|
||||
// Exponential backoff for reconnection
|
||||
static constexpr uint32_t BACKOFF_MIN_MS = 1000; // Start at 1 second
|
||||
static constexpr uint32_t BACKOFF_MAX_MS = 60000; // Max 60 seconds
|
||||
static constexpr uint32_t BACKOFF_MULTIPLIER = 2; // Double each attempt
|
||||
uint32_t _current_backoff_ms;
|
||||
|
||||
uint32_t _messages_sent;
|
||||
uint32_t _messages_received;
|
||||
uint32_t _reconnect_count;
|
||||
|
||||
@ -733,6 +733,21 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
_prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
|
||||
}
|
||||
|
||||
MyMesh::~MyMesh() {
|
||||
// Clean up dynamically allocated resources
|
||||
#ifdef WITH_MQTT
|
||||
delete _web_config;
|
||||
_web_config = nullptr;
|
||||
delete _mqtt_bridge;
|
||||
_mqtt_bridge = nullptr;
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
delete _mqtt_bridge;
|
||||
_mqtt_bridge = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::begin(FILESYSTEM *fs) {
|
||||
mesh::Mesh::begin();
|
||||
_fs = fs;
|
||||
|
||||
11
src/MyMesh.h
11
src/MyMesh.h
@ -125,21 +125,21 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
|
||||
#ifdef WITH_MQTT
|
||||
WiFiManager _wifi_mgr;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
WebConfig* _web_config;
|
||||
MQTTBridge* _mqtt_bridge = nullptr;
|
||||
WebConfig* _web_config = nullptr;
|
||||
WiFiConfig _wifi_config;
|
||||
MQTTConfig _mqtt_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
unsigned long _last_mqtt_stats = 0;
|
||||
|
||||
void initMQTT();
|
||||
#endif
|
||||
|
||||
#ifdef WITH_ETHERNET
|
||||
EthernetManager _eth_mgr;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
MQTTBridge* _mqtt_bridge = nullptr;
|
||||
MQTTConfig _mqtt_config;
|
||||
EthernetConfig _eth_config;
|
||||
unsigned long _last_mqtt_stats;
|
||||
unsigned long _last_mqtt_stats = 0;
|
||||
|
||||
void initEthernet();
|
||||
#endif
|
||||
@ -196,6 +196,7 @@ protected:
|
||||
|
||||
public:
|
||||
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
|
||||
~MyMesh(); // Clean up dynamically allocated resources
|
||||
|
||||
void begin(FILESYSTEM* fs);
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ WiFiManager::WiFiManager()
|
||||
_retry_count(0),
|
||||
_initialized(false) {
|
||||
memset(&_config, 0, sizeof(_config));
|
||||
memset(_cached_ssid, 0, sizeof(_cached_ssid));
|
||||
}
|
||||
|
||||
void WiFiManager::begin(const WiFiConfig& config) {
|
||||
@ -82,6 +83,9 @@ void WiFiManager::loop() {
|
||||
_state = WiFiState::CONNECTED;
|
||||
_connected_since = millis();
|
||||
_retry_count = 0;
|
||||
// Cache SSID to avoid dangling pointer from WiFi.SSID().c_str()
|
||||
strncpy(_cached_ssid, WiFi.SSID().c_str(), sizeof(_cached_ssid) - 1);
|
||||
_cached_ssid[sizeof(_cached_ssid) - 1] = '\0';
|
||||
Serial.printf("[WiFi] Connected! IP: %s, RSSI: %d dBm\n",
|
||||
WiFi.localIP().toString().c_str(), WiFi.RSSI());
|
||||
} else if (status == WL_NO_SSID_AVAIL) {
|
||||
@ -184,6 +188,9 @@ void WiFiManager::startAPMode() {
|
||||
|
||||
if (success) {
|
||||
_state = WiFiState::AP_MODE;
|
||||
// Cache AP SSID to avoid dangling pointer
|
||||
strncpy(_cached_ssid, ap_ssid.c_str(), sizeof(_cached_ssid) - 1);
|
||||
_cached_ssid[sizeof(_cached_ssid) - 1] = '\0';
|
||||
Serial.printf("[WiFi] AP started: SSID='%s', IP=%s\n",
|
||||
ap_ssid.c_str(), WiFi.softAPIP().toString().c_str());
|
||||
} else {
|
||||
@ -221,10 +228,8 @@ int32_t WiFiManager::getRSSI() const {
|
||||
}
|
||||
|
||||
const char* WiFiManager::getSSID() const {
|
||||
if (_state == WiFiState::CONNECTED) {
|
||||
return WiFi.SSID().c_str();
|
||||
} else if (_state == WiFiState::AP_MODE) {
|
||||
return WiFi.softAPSSID().c_str();
|
||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
||||
return _cached_ssid;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@ -264,10 +269,8 @@ NetworkState WiFiManager::getState() const {
|
||||
}
|
||||
|
||||
const char* WiFiManager::getConnectionName() const {
|
||||
if (_state == WiFiState::CONNECTED) {
|
||||
return WiFi.SSID().c_str();
|
||||
} else if (_state == WiFiState::AP_MODE) {
|
||||
return WiFi.softAPSSID().c_str();
|
||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
||||
return _cached_ssid;
|
||||
}
|
||||
return "Not connected";
|
||||
}
|
||||
|
||||
@ -61,6 +61,7 @@ private:
|
||||
unsigned long _connected_since;
|
||||
uint8_t _retry_count;
|
||||
bool _initialized;
|
||||
char _cached_ssid[33]; // Cached SSID to avoid dangling pointer from WiFi.SSID().c_str()
|
||||
|
||||
void attemptConnection();
|
||||
void checkConnection();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user