Browse Source

Reworked MQTT-Reconnect and slight mem-improvement

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

8
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 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 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 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 restoredHostnameFromNvs[] PROGMEM = "Hostname aus NVS geladen";
static const char currentVoltageMsg[] PROGMEM = "Aktuelle Batteriespannung"; static const char currentVoltageMsg[] PROGMEM = "Aktuelle Batteriespannung";
static const char voltageTooLow[] PROGMEM = "Batteriespannung niedrig"; static const char voltageTooLow[] PROGMEM = "Batteriespannung niedrig";
@ -150,4 +150,8 @@ static const char voltageIndicatorHighFromNVS[] PROGMEM = "Oberer Spannungslevel
static const char voltageCheckIntervalFromNVS[] PROGMEM = "Zyklus für Spannungsmessung (Batterie) fuer Neopixel-Anzeige aus NVS geladen"; static const char voltageCheckIntervalFromNVS[] PROGMEM = "Zyklus für Spannungsmessung (Batterie) fuer Neopixel-Anzeige aus NVS geladen";
static const char warningLowVoltageFromNVS[] PROGMEM = "Spannungslevel (Batterie) fuer Warnung via Neopixel aus NVS geladen"; 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 unableToRestoreLastRfidFromNVS[] PROGMEM = "Letzte RFID konnte nicht aus NVS geladen werden";
static const char restoredLastRfidFromNVS[] PROGMEM = "Letzte RFID wurde aus NVS geladen";
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";

6
src/logmessages_EN.h

@ -150,4 +150,8 @@ static const char voltageIndicatorHighFromNVS[] PROGMEM = "Restored upper voltag
static const char voltageCheckIntervalFromNVS[] PROGMEM = "Restored interval of battery-measurement or Neopixel-display from NVS"; static const char voltageCheckIntervalFromNVS[] PROGMEM = "Restored interval of battery-measurement or Neopixel-display from NVS";
static const char warningLowVoltageFromNVS[] PROGMEM = "Restored battery-voltage-level for warning via Neopixel from NVS"; 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 unableToRestoreLastRfidFromNVS[] PROGMEM = "Unable to restore last RFID from NVS";
static const char restoredLastRfidFromNVS[] PROGMEM = "Restored 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";

55
src/main.cpp

@ -150,7 +150,6 @@ uint8_t nightLedBrightness = 2; // Brightness of Neopixe
// MQTT // MQTT
bool enableMqtt = true; bool enableMqtt = true;
#ifdef MQTT_ENABLE #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) uint8_t const stillOnlineInterval = 60; // Interval 'I'm still alive' is sent via MQTT (in seconds)
#endif #endif
@ -413,20 +412,23 @@ void IRAM_ATTR onTimer() {
* @param message * @param message
*/ */
void createFile(fs::FS &fs, const char * path, const char * 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); loggerNl(logBuf, LOGLEVEL_DEBUG);
File file = fs.open(path, FILE_WRITE); File file = fs.open(path, FILE_WRITE);
if(!file){
snprintf(logBuf, serialLoglength, "Failed to open file for writing");
if (!file) {
snprintf(logBuf, serialLoglength, "%s", (char *) FPSTR(failedOpenFileForWrite));
//snprintf(logBuf, serialLoglength, "Failed to open file for writing");
loggerNl(logBuf, LOGLEVEL_ERROR); loggerNl(logBuf, LOGLEVEL_ERROR);
return; return;
} }
if(file.print(message)){
snprintf(logBuf, serialLoglength, "File written");
if (file.print(message)) {
//snprintf(logBuf, serialLoglength, "File written");
snprintf(logBuf, serialLoglength, "%s", (char *) FPSTR(fileWritten));
loggerNl(logBuf, LOGLEVEL_DEBUG); loggerNl(logBuf, LOGLEVEL_DEBUG);
} else { } 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); loggerNl(logBuf, LOGLEVEL_ERROR);
} }
file.close(); file.close();
@ -533,39 +535,39 @@ void parseSDFileList(fs::FS &fs, const char * dirname, const char * parent, uint
yield(); yield();
File root = fs.open(dirname); File root = fs.open(dirname);
if(!root){
if (!root) {
snprintf(logBuf, serialLoglength, "Failed to open directory"); snprintf(logBuf, serialLoglength, "Failed to open directory");
loggerNl(logBuf, LOGLEVEL_DEBUG); loggerNl(logBuf, LOGLEVEL_DEBUG);
return; return;
} }
if(!root.isDirectory()){
if (!root.isDirectory()) {
snprintf(logBuf, serialLoglength, "Not a directory"); snprintf(logBuf, serialLoglength, "Not a directory");
loggerNl(logBuf, LOGLEVEL_DEBUG); loggerNl(logBuf, LOGLEVEL_DEBUG);
return; return;
} }
File file = root.openNextFile(); File file = root.openNextFile();
while(file){
while(file) {
esp_task_wdt_reset(); esp_task_wdt_reset();
const char *parent; const char *parent;
if (strcmp(root.name(), "/") == 0 || root.name() == 0){
if (strcmp(root.name(), "/") == 0 || root.name() == 0) {
parent = "#\0"; parent = "#\0";
} else { } else {
parent = root.name(); parent = root.name();
} }
if (file.name() == 0 ){
if (file.name() == 0 ) {
continue; continue;
} }
strncpy(fileNameBuf, (char *) file.name(), sizeof(fileNameBuf) / sizeof(fileNameBuf[0])); strncpy(fileNameBuf, (char *) file.name(), sizeof(fileNameBuf) / sizeof(fileNameBuf[0]));
// we have a folder // we have a folder
if(file.isDirectory()){
if(file.isDirectory()) {
esp_task_wdt_reset(); esp_task_wdt_reset();
if (pathValid(fileNameBuf)){
if (pathValid(fileNameBuf)) {
sendWebsocketData(0, 31); sendWebsocketData(0, 31);
appendNodeToJSONFile(SD, DIRECTORY_INDEX_FILE, fileNameBuf, parent, "folder" ); appendNodeToJSONFile(SD, DIRECTORY_INDEX_FILE, fileNameBuf, parent, "folder" );
@ -577,7 +579,7 @@ void parseSDFileList(fs::FS &fs, const char * dirname, const char * parent, uint
// we have a file // we have a file
} else { } else {
if (fileValid(fileNameBuf)){
if (fileValid(fileNameBuf)) {
appendNodeToJSONFile(SD, DIRECTORY_INDEX_FILE, fileNameBuf, parent, "file" ); appendNodeToJSONFile(SD, DIRECTORY_INDEX_FILE, fileNameBuf, parent, "file" );
} }
} }
@ -805,11 +807,20 @@ void postHeartbeatViaMqtt(void) {
/* Connects/reconnects to MQTT-Broker unless connection is not already available. /* Connects/reconnects to MQTT-Broker unless connection is not already available.
Manages MQTT-subscriptions. Manages MQTT-subscriptions.
*/ */
uint32_t mqttLastRetryTimestamp = 0;
bool reconnect() { bool reconnect() {
uint8_t maxRetries = 10;
uint8_t connect = false; uint8_t connect = false;
uint8_t i = 0;
if (!mqttLastRetryTimestamp || millis() - mqttLastRetryTimestamp >= mqttRetryInterval * 1000) {
mqttLastRetryTimestamp = millis();
} else {
return false;
}
while (!MQTTclient.connected() && mqttFailCount < maxRetries) {
while (!MQTTclient.connected() && i < mqttMaxRetriesPerInterval) {
i++;
snprintf(logBuf, serialLoglength, "%s %s", (char *) FPSTR(tryConnectMqttS), mqtt_server); snprintf(logBuf, serialLoglength, "%s %s", (char *) FPSTR(tryConnectMqttS), mqtt_server);
loggerNl(logBuf, LOGLEVEL_NOTICE); loggerNl(logBuf, LOGLEVEL_NOTICE);
@ -868,10 +879,8 @@ bool reconnect() {
return MQTTclient.connected(); return MQTTclient.connected();
} else { } 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); loggerNl(logBuf, LOGLEVEL_ERROR);
mqttFailCount++;
delay(500);
} }
} }
return false; return false;
@ -3214,9 +3223,9 @@ void sendWebsocketData(uint32_t client, uint8_t code) {
object["rfidId"] = currentRfidTagId; object["rfidId"] = currentRfidTagId;
} else if (code == 20) { } else if (code == 20) {
object["pong"] = "pong"; object["pong"] = "pong";
} else if (code == 30){
} else if (code == 30) {
object["refreshFileList"] = "ready"; object["refreshFileList"] = "ready";
}else if (code == 31){
} else if (code == 31) {
object["indexingState"] = fileNameBuf; object["indexingState"] = fileNameBuf;
} }

2
src/settings.h

@ -130,6 +130,8 @@ float voltageIndicatorHigh = 4.2; // Upper range for Neopixel-
// (optional) Topics for MQTT // (optional) Topics for MQTT
#ifdef MQTT_ENABLE #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 #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 topicSleepCmnd[] PROGMEM = "Cmnd/Tonuino-Leonie/Sleep";
static const char topicSleepState[] PROGMEM = "State/Tonuino-Leonie/Sleep"; static const char topicSleepState[] PROGMEM = "State/Tonuino-Leonie/Sleep";

Loading…
Cancel
Save