Browse Source

Reworked MQTT-Reconnect and slight mem-improvement

master
Torsten Stauder 5 years ago
parent
commit
249f2c1d4c
  1. 6
      src/logmessages.h
  2. 4
      src/logmessages_EN.h
  3. 31
      src/main.cpp
  4. 2
      src/settings.h

6
src/logmessages.h

@ -138,7 +138,7 @@ static const char ssidNotFoundInNvs[] PROGMEM = "SSID wurde im NVS nicht gefunde
static const char wifiPwdNotFoundInNvs[] PROGMEM = "WLAN-Passwort wurde im NVS nicht gefunden.";
static const char wifiStaticIpConfigNotFoundInNvs[] PROGMEM = "Statische WLAN-IP-Konfiguration wurde im NVS nicht gefunden.";
static const char wifiHostnameNotSet[] PROGMEM = "Keine Hostname-Konfiguration im NVS gefunden.";
static const char mqttConnFailed[] PROGMEM = "Verbindung fehlgeschlagen, versuche erneut in Kürze erneut";
static const char mqttConnFailed[] PROGMEM = "Verbindung fehlgeschlagen, versuche in Kürze erneut";
static const char restoredHostnameFromNvs[] PROGMEM = "Hostname aus NVS geladen";
static const char currentVoltageMsg[] PROGMEM = "Aktuelle Batteriespannung";
static const char voltageTooLow[] PROGMEM = "Batteriespannung niedrig";
@ -151,3 +151,7 @@ static const char voltageCheckIntervalFromNVS[] PROGMEM = "Zyklus für Spannungs
static const char warningLowVoltageFromNVS[] PROGMEM = "Spannungslevel (Batterie) fuer Warnung via Neopixel aus NVS geladen";
static const char unableToRestoreLastRfidFromNVS[] PROGMEM = "Letzte RFID konnte nicht aus NVS geladen werden";
static const char restoredLastRfidFromNVS[] PROGMEM = "Letzte RFID wurde aus NVS geladen";
static const char failedOpenFileForWrite[] PROGMEM = "Öffnen der Datei für den Schreibvorgang fehlgeschlagen";
static const char fileWritten[] PROGMEM = "Schreibe Datei";
static const char writeFailed[] PROGMEM = "Schreibvorgang fehlgeschlagen";
static const char writingFile[] PROGMEM = "Schreibe Datei";

4
src/logmessages_EN.h

@ -151,3 +151,7 @@ static const char voltageCheckIntervalFromNVS[] PROGMEM = "Restored interval of
static const char warningLowVoltageFromNVS[] PROGMEM = "Restored battery-voltage-level for warning via Neopixel from NVS";
static const char unableToRestoreLastRfidFromNVS[] PROGMEM = "Unable to restore last RFID from NVS";
static const char restoredLastRfidFromNVS[] PROGMEM = "Restored last RFID from NVS";
static const char failedOpenFileForWrite[] PROGMEM = "Failed to open file for writing";
static const char fileWritten[] PROGMEM = "File written";
static const char writeFailed[] PROGMEM = "Write failed";
static const char writingFile[] PROGMEM = "Writing file";

31
src/main.cpp

@ -150,7 +150,6 @@ uint8_t nightLedBrightness = 2; // Brightness of Neopixe
// MQTT
bool enableMqtt = true;
#ifdef MQTT_ENABLE
uint8_t mqttFailCount = 3; // Number of times mqtt-reconnect is allowed to fail. If >= mqttFailCount to further reconnects take place
uint8_t const stillOnlineInterval = 60; // Interval 'I'm still alive' is sent via MQTT (in seconds)
#endif
@ -413,20 +412,23 @@ void IRAM_ATTR onTimer() {
* @param message
*/
void createFile(fs::FS &fs, const char * path, const char * message) {
snprintf(logBuf, serialLoglength, "Writing file: %s\n", path);
//snprintf(logBuf, serialLoglength, "Writing file: %s\n", path);
snprintf(logBuf, serialLoglength, "%s: %s", (char *) FPSTR(writingFile), path);
loggerNl(logBuf, LOGLEVEL_DEBUG);
File file = fs.open(path, FILE_WRITE);
if (!file) {
snprintf(logBuf, serialLoglength, "Failed to open file for writing");
snprintf(logBuf, serialLoglength, "%s", (char *) FPSTR(failedOpenFileForWrite));
//snprintf(logBuf, serialLoglength, "Failed to open file for writing");
loggerNl(logBuf, LOGLEVEL_ERROR);
return;
}
if (file.print(message)) {
snprintf(logBuf, serialLoglength, "File written");
//snprintf(logBuf, serialLoglength, "File written");
snprintf(logBuf, serialLoglength, "%s", (char *) FPSTR(fileWritten));
loggerNl(logBuf, LOGLEVEL_DEBUG);
} else {
Serial.println("Write failed");
snprintf(logBuf, serialLoglength, "Write failed");
//snprintf(logBuf, serialLoglength, "Write failed");
snprintf(logBuf, serialLoglength, "%s", (char *) FPSTR(writeFailed));
loggerNl(logBuf, LOGLEVEL_ERROR);
}
file.close();
@ -805,11 +807,20 @@ void postHeartbeatViaMqtt(void) {
/* Connects/reconnects to MQTT-Broker unless connection is not already available.
Manages MQTT-subscriptions.
*/
uint32_t mqttLastRetryTimestamp = 0;
bool reconnect() {
uint8_t maxRetries = 10;
uint8_t connect = false;
uint8_t i = 0;
while (!MQTTclient.connected() && mqttFailCount < maxRetries) {
if (!mqttLastRetryTimestamp || millis() - mqttLastRetryTimestamp >= mqttRetryInterval * 1000) {
mqttLastRetryTimestamp = millis();
} else {
return false;
}
while (!MQTTclient.connected() && i < mqttMaxRetriesPerInterval) {
i++;
snprintf(logBuf, serialLoglength, "%s %s", (char *) FPSTR(tryConnectMqttS), mqtt_server);
loggerNl(logBuf, LOGLEVEL_NOTICE);
@ -868,10 +879,8 @@ bool reconnect() {
return MQTTclient.connected();
} else {
snprintf(logBuf, serialLoglength, "%s: rc=%i (%d / %d)", (char *) FPSTR(mqttConnFailed), MQTTclient.state(), mqttFailCount+1, maxRetries);
snprintf(logBuf, serialLoglength, "%s: rc=%i (%d / %d)", (char *) FPSTR(mqttConnFailed), MQTTclient.state(), i, mqttMaxRetriesPerInterval);
loggerNl(logBuf, LOGLEVEL_ERROR);
mqttFailCount++;
delay(500);
}
}
return false;

2
src/settings.h

@ -130,6 +130,8 @@ float voltageIndicatorHigh = 4.2; // Upper range for Neopixel-
// (optional) Topics for MQTT
#ifdef MQTT_ENABLE
uint16_t mqttRetryInterval = 15; // Try to reconnect to MQTT-server every (n) seconds if connection is broken
uint8_t mqttMaxRetriesPerInterval = 1; // Number of retries per time-interval (mqttRetryInterval). mqttRetryInterval 15 / mqttMaxRetriesPerInterval 1 => once every 15s
#define DEVICE_HOSTNAME "ESP32-Tonuino-Leonie" // Name that that is used for MQTT
static const char topicSleepCmnd[] PROGMEM = "Cmnd/Tonuino-Leonie/Sleep";
static const char topicSleepState[] PROGMEM = "State/Tonuino-Leonie/Sleep";

Loading…
Cancel
Save