Compare commits
No commits in common. "main" and "fix/improve-error-logging" have entirely different histories.
main
...
fix/improv
@ -17,7 +17,6 @@ MQTTBridge::MQTTBridge(INetworkManager* network, mesh::PacketManager* mgr, mesh:
|
|||||||
_last_status_publish(0),
|
_last_status_publish(0),
|
||||||
_last_stats_publish(0),
|
_last_stats_publish(0),
|
||||||
_connected_since(0),
|
_connected_since(0),
|
||||||
_current_backoff_ms(BACKOFF_MIN_MS),
|
|
||||||
_messages_sent(0),
|
_messages_sent(0),
|
||||||
_messages_received(0),
|
_messages_received(0),
|
||||||
_reconnect_count(0),
|
_reconnect_count(0),
|
||||||
@ -82,8 +81,7 @@ void MQTTBridge::loop() {
|
|||||||
|
|
||||||
switch (_state) {
|
switch (_state) {
|
||||||
case MQTTState::DISCONNECTED:
|
case MQTTState::DISCONNECTED:
|
||||||
// Use exponential backoff for reconnection attempts
|
if (millis() - _last_connect_attempt > 5000) {
|
||||||
if (millis() - _last_connect_attempt > _current_backoff_ms) {
|
|
||||||
attemptConnection();
|
attemptConnection();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -96,7 +94,6 @@ void MQTTBridge::loop() {
|
|||||||
_state = MQTTState::DISCONNECTED;
|
_state = MQTTState::DISCONNECTED;
|
||||||
Serial.println("[MQTT] Connection lost");
|
Serial.println("[MQTT] Connection lost");
|
||||||
_last_connect_attempt = millis();
|
_last_connect_attempt = millis();
|
||||||
// Don't reset backoff on connection loss - broker might be down
|
|
||||||
} else {
|
} else {
|
||||||
_mqtt_client.loop();
|
_mqtt_client.loop();
|
||||||
|
|
||||||
@ -108,8 +105,7 @@ void MQTTBridge::loop() {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTTState::ERROR:
|
case MQTTState::ERROR:
|
||||||
// Use backoff for error recovery too
|
if (millis() - _last_connect_attempt > 30000) {
|
||||||
if (millis() - _last_connect_attempt > _current_backoff_ms) {
|
|
||||||
_state = MQTTState::DISCONNECTED;
|
_state = MQTTState::DISCONNECTED;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -139,8 +135,7 @@ void MQTTBridge::attemptConnection() {
|
|||||||
_state = MQTTState::CONNECTING;
|
_state = MQTTState::CONNECTING;
|
||||||
_last_connect_attempt = millis();
|
_last_connect_attempt = millis();
|
||||||
|
|
||||||
Serial.printf("[MQTT] Connecting to %s:%d (backoff=%lums)...\n",
|
Serial.printf("[MQTT] Connecting to %s:%d...\n", _config.broker, _config.port);
|
||||||
_config.broker, _config.port, _current_backoff_ms);
|
|
||||||
|
|
||||||
String client_id = String(_config.client_id);
|
String client_id = String(_config.client_id);
|
||||||
if (client_id.length() == 0) {
|
if (client_id.length() == 0) {
|
||||||
@ -163,8 +158,6 @@ void MQTTBridge::attemptConnection() {
|
|||||||
_state = MQTTState::CONNECTED;
|
_state = MQTTState::CONNECTED;
|
||||||
_connected_since = millis();
|
_connected_since = millis();
|
||||||
_reconnect_count++;
|
_reconnect_count++;
|
||||||
// Reset backoff on successful connection
|
|
||||||
_current_backoff_ms = BACKOFF_MIN_MS;
|
|
||||||
Serial.println("[MQTT] Connected!");
|
Serial.println("[MQTT] Connected!");
|
||||||
|
|
||||||
subscribeToCommands();
|
subscribeToCommands();
|
||||||
@ -177,10 +170,6 @@ void MQTTBridge::attemptConnection() {
|
|||||||
Serial.printf("[MQTT] Broker: %s:%d, User: %s\n", _config.broker, _config.port,
|
Serial.printf("[MQTT] Broker: %s:%d, User: %s\n", _config.broker, _config.port,
|
||||||
strlen(_config.user) > 0 ? _config.user : "(none)");
|
strlen(_config.user) > 0 ? _config.user : "(none)");
|
||||||
_state = MQTTState::ERROR;
|
_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,12 +85,6 @@ private:
|
|||||||
unsigned long _last_stats_publish;
|
unsigned long _last_stats_publish;
|
||||||
unsigned long _connected_since;
|
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_sent;
|
||||||
uint32_t _messages_received;
|
uint32_t _messages_received;
|
||||||
uint32_t _reconnect_count;
|
uint32_t _reconnect_count;
|
||||||
|
|||||||
@ -733,21 +733,6 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
|||||||
_prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
|
_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) {
|
void MyMesh::begin(FILESYSTEM *fs) {
|
||||||
mesh::Mesh::begin();
|
mesh::Mesh::begin();
|
||||||
_fs = fs;
|
_fs = fs;
|
||||||
|
|||||||
11
src/MyMesh.h
11
src/MyMesh.h
@ -125,21 +125,21 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|||||||
|
|
||||||
#ifdef WITH_MQTT
|
#ifdef WITH_MQTT
|
||||||
WiFiManager _wifi_mgr;
|
WiFiManager _wifi_mgr;
|
||||||
MQTTBridge* _mqtt_bridge = nullptr;
|
MQTTBridge* _mqtt_bridge;
|
||||||
WebConfig* _web_config = nullptr;
|
WebConfig* _web_config;
|
||||||
WiFiConfig _wifi_config;
|
WiFiConfig _wifi_config;
|
||||||
MQTTConfig _mqtt_config;
|
MQTTConfig _mqtt_config;
|
||||||
unsigned long _last_mqtt_stats = 0;
|
unsigned long _last_mqtt_stats;
|
||||||
|
|
||||||
void initMQTT();
|
void initMQTT();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WITH_ETHERNET
|
#ifdef WITH_ETHERNET
|
||||||
EthernetManager _eth_mgr;
|
EthernetManager _eth_mgr;
|
||||||
MQTTBridge* _mqtt_bridge = nullptr;
|
MQTTBridge* _mqtt_bridge;
|
||||||
MQTTConfig _mqtt_config;
|
MQTTConfig _mqtt_config;
|
||||||
EthernetConfig _eth_config;
|
EthernetConfig _eth_config;
|
||||||
unsigned long _last_mqtt_stats = 0;
|
unsigned long _last_mqtt_stats;
|
||||||
|
|
||||||
void initEthernet();
|
void initEthernet();
|
||||||
#endif
|
#endif
|
||||||
@ -196,7 +196,6 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
|
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);
|
void begin(FILESYSTEM* fs);
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,6 @@ WiFiManager::WiFiManager()
|
|||||||
_retry_count(0),
|
_retry_count(0),
|
||||||
_initialized(false) {
|
_initialized(false) {
|
||||||
memset(&_config, 0, sizeof(_config));
|
memset(&_config, 0, sizeof(_config));
|
||||||
memset(_cached_ssid, 0, sizeof(_cached_ssid));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiManager::begin(const WiFiConfig& config) {
|
void WiFiManager::begin(const WiFiConfig& config) {
|
||||||
@ -83,9 +82,6 @@ void WiFiManager::loop() {
|
|||||||
_state = WiFiState::CONNECTED;
|
_state = WiFiState::CONNECTED;
|
||||||
_connected_since = millis();
|
_connected_since = millis();
|
||||||
_retry_count = 0;
|
_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",
|
Serial.printf("[WiFi] Connected! IP: %s, RSSI: %d dBm\n",
|
||||||
WiFi.localIP().toString().c_str(), WiFi.RSSI());
|
WiFi.localIP().toString().c_str(), WiFi.RSSI());
|
||||||
} else if (status == WL_NO_SSID_AVAIL) {
|
} else if (status == WL_NO_SSID_AVAIL) {
|
||||||
@ -188,9 +184,6 @@ void WiFiManager::startAPMode() {
|
|||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
_state = WiFiState::AP_MODE;
|
_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",
|
Serial.printf("[WiFi] AP started: SSID='%s', IP=%s\n",
|
||||||
ap_ssid.c_str(), WiFi.softAPIP().toString().c_str());
|
ap_ssid.c_str(), WiFi.softAPIP().toString().c_str());
|
||||||
} else {
|
} else {
|
||||||
@ -228,8 +221,10 @@ int32_t WiFiManager::getRSSI() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char* WiFiManager::getSSID() const {
|
const char* WiFiManager::getSSID() const {
|
||||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
if (_state == WiFiState::CONNECTED) {
|
||||||
return _cached_ssid;
|
return WiFi.SSID().c_str();
|
||||||
|
} else if (_state == WiFiState::AP_MODE) {
|
||||||
|
return WiFi.softAPSSID().c_str();
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -269,8 +264,10 @@ NetworkState WiFiManager::getState() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char* WiFiManager::getConnectionName() const {
|
const char* WiFiManager::getConnectionName() const {
|
||||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
if (_state == WiFiState::CONNECTED) {
|
||||||
return _cached_ssid;
|
return WiFi.SSID().c_str();
|
||||||
|
} else if (_state == WiFiState::AP_MODE) {
|
||||||
|
return WiFi.softAPSSID().c_str();
|
||||||
}
|
}
|
||||||
return "Not connected";
|
return "Not connected";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,7 +61,6 @@ private:
|
|||||||
unsigned long _connected_since;
|
unsigned long _connected_since;
|
||||||
uint8_t _retry_count;
|
uint8_t _retry_count;
|
||||||
bool _initialized;
|
bool _initialized;
|
||||||
char _cached_ssid[33]; // Cached SSID to avoid dangling pointer from WiFi.SSID().c_str()
|
|
||||||
|
|
||||||
void attemptConnection();
|
void attemptConnection();
|
||||||
void checkConnection();
|
void checkConnection();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user