restart MQTT after a HA core reboot

This commit is contained in:
Robert van den Breemen 2021-10-23 21:18:41 +02:00
parent 6a2b1b5d1b
commit 12555e5707
8 changed files with 51 additions and 32 deletions

View File

@ -70,6 +70,22 @@ void handleMQTTcallback(char* topic, byte* payload, unsigned int length) {
//incoming command to be forwarded to OTGW
addOTWGcmdtoqueue((char *)payload, length);
}
//detect home assistant going down...
char msgPayload[50];
strlcpy(msgPayload, (char *)payload, ((length+1<50)?(length+1):(50)));
if (stricmp(topic, "homeassistant/status") == 0) {
//incoming message on status, detect going down
if (stricmp(msgPayload, "offline") == 0){
//home assistant went down
DebugTln(F("Home Assistant went offline!"));
} else if (stricmp(msgPayload, "online") == 0){
DebugTln(F("Home Assistant went online!"));
//restart stuff, to make sure it works correctly again
startMQTT(); // fixing some issues with hanging HA AutoDiscovery in some scenario's?
} else {
DebugTf("Home Assistant Status=[%s]\r\n", msgPayload);
}
}
}
//===========================================================================================
@ -155,6 +171,7 @@ void handleMQTT()
MQTTDebugTf("MQTT: Subscribe TopicId [%s] FAILED! \r\n", topic);
PrintMQTTError();
}
MQTTclient.subscribe("homeassistant/status"); //start monitoring the status of homeassistant, if it goes down, then force a restart after it comes back online.
sendMQTTversioninfo();
}
else

View File

@ -169,7 +169,7 @@ String initWatchDog() {
// Code here is based on ESPEasy code, modified to work in the project.
// configure hardware pins according to eeprom settings.
OTGWDebugTln("Setup Watchdog");
OTGWDebugTln(F("Setup Watchdog"));
OTGWDebugTln(F("INIT : I2C"));
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL); //configure the I2C bus
//=============================================
@ -1039,7 +1039,7 @@ void addOTWGcmdtoqueue(const char* buf, int len){
if (cmdptr < CMDQUEUE_MAX) {
cmdptr++; //next free slot
OTGWDebugTf("CmdQueue: Next free queue slot: [%d]\r\n", cmdptr);
} else OTGWDebugTln("CmdQueue: Error: Reached max queue");
} else OTGWDebugTln(F("CmdQueue: Error: Reached max queue"));
} else OTGWDebugTf("CmdQueue: Found command at: [%d] - [%d]\r\n", insertptr, cmdptr);
}
@ -1446,7 +1446,7 @@ void handleOTGW()
//check for reset command
if (stricmp(sWrite, "GW=R")==0){
//detected [GW=R], then reset the gateway the gpio way
OTGWDebugTln("Detected: GW=R. Reset gateway command executed.");
OTGWDebugTln(F("Detected: GW=R. Reset gateway command executed."));
resetOTGW();
} else if (stricmp(sWrite, "PS=1")==0) {
//detected [PS=1], then PrintSummary mode = true --> From this point on you need to ask for summary.
@ -1610,7 +1610,7 @@ void startOTGWstream()
void upgradepicnow(const char *filename) {
if (OTGWSerial.busy()) return; // if already in programming mode, never call it twice
OTGWDebugTln("Start PIC upgrade now.");
OTGWDebugTln(F("Start PIC upgrade now."));
fwupgradestart(filename);
while (OTGWSerial.busy()){
feedWatchDog();

View File

@ -56,6 +56,7 @@ void setLed(int8_t, uint8_t);
#define CBINARY(x) (x?"1":"0")
#define EVALBOOLEAN(x) (stricmp(x,"true")==0||stricmp(x,"on")==0||stricmp(x,"1")==0)
//prototype
void sendMQTTData(const String, const String, const bool);
void sendMQTTData(const char*, const char*, const bool);

View File

@ -199,6 +199,7 @@ void do5minevent(){
DebugTf("Uptime seconds: %d\r\n", upTimeSeconds);
String sUptime = String(upTimeSeconds);
sendMQTTData("otgw-firmware/uptime", sUptime, false);
sendMQTTversioninfo();
}
//===[ check for new pic version ]===

View File

@ -16,44 +16,44 @@ void handleDebug(){
Debugln();
break;
case 'q':
DebugTln("Read settings");
DebugTln(F("Read settings"));
readSettings(true);
break;
case 'm':
DebugTln("Configure MQTT Discovery");
DebugTln(F("Configure MQTT Discovery"));
DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
doAutoConfigure();
break;
case 'r':
if (WiFi.status() != WL_CONNECTED)
{
DebugTln("Reconnecting to wifi");
DebugTln(F("Reconnecting to wifi"));
startWiFi(CSTR(settingHostname), 240);
//check OTGW and telnet
startTelnet();
startOTGWstream();
} else DebugTln("Wifi is connected");
} else DebugTln(F("Wifi is connected"));
if (!statusMQTTconnection) {
DebugTln("Reconnecting MQTT");
DebugTln(F("Reconnecting MQTT"));
startMQTT();
} else DebugTln("MQTT is connected");
} else DebugTln(F("MQTT is connected"));
break;
case '1': bDebugOTmsg = !bDebugOTmsg; DebugTf("\r\nDebug OTmsg: %s\r\n", CBOOLEAN(bDebugOTmsg)); break;
case '2': bDebugRestAPI = !bDebugRestAPI; DebugTf("\r\nDebug RestAPI: %s\r\n", CBOOLEAN(bDebugRestAPI)); break;
case '3': bDebugMQTT = !bDebugMQTT; DebugTf("\r\nDebug MQTT: %s\r\n", CBOOLEAN(bDebugMQTT)); break;
case 'b':
DebugTln("Blink led 1");
DebugTln(F("Blink led 1"));
// DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
blinkLED(LED1, 5, 500);
break;
case 'i':
DebugTln("relay init");
DebugTln(F("relay init"));
// DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
initOutputs();
break;
case 'u':
DebugTln("gpio output on ");
DebugTln(F("gpio output on "));
// DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
digitalWrite(settingGPIOOUTPUTSpin, ON);
break;
@ -61,7 +61,7 @@ void handleDebug(){
DebugTf("read gpio output state (0== led ON): %d \r\n", digitalRead(settingGPIOOUTPUTSpin));
break;
case 'k':
DebugTln("read settings");
DebugTln(F("read settings"));
// DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
readSettings(true);
break;
@ -78,9 +78,9 @@ void handleDebug(){
case 'f':
if(settingMyDEBUG)
{
DebugTln("MyDEBUG = true");
DebugTln(F("MyDEBUG = true"));
}else{
DebugTln("MyDEBUG = false");
DebugTln(F("MyDEBUG = false"));
}
// DebugTf("Enable MQTT: %s", CBOOLEAN(settingMQTTenable));
break;

View File

@ -196,7 +196,7 @@ if (!settingNTPenable) return;
case TIME_NOTSET:
case TIME_NEEDSYNC:
NtpLastSync = time(nullptr); //remember last sync
DebugTln("Start time syncing");
DebugTln(F("Start time syncing"));
startNTP();
NtpStatus = TIME_WAITFORSYNC;
break;
@ -211,18 +211,18 @@ if (!settingNTPenable) return;
DebugTf("Error: Timezone Invalid/Not Found: [%s]\r\n", CSTR(settingNTPtimezone));
settingNTPtimezone = NTP_DEFAULT_TIMEZONE;
myTz = manager.createForZoneName(CSTR(settingNTPtimezone)); //try with default Timezone instead
} else DebugTln("Timezone lookup: successful");
} else DebugTln(F("Timezone lookup: successful"));
auto myTime = ZonedDateTime::forUnixSeconds(NtpLastSync, myTz);
setTime(myTime.hour(), myTime.minute(), myTime.second(), myTime.day(), myTime.month(), myTime.year());
NtpStatus = TIME_SYNC;
DebugTln("Time synced!");
DebugTln(F("Time synced!"));
}
break;
case TIME_SYNC:
if ((time(nullptr)-NtpLastSync) > NTP_RESYNC_TIME){
//when xx seconds have passed, resync using NTP
DebugTln("Time resync needed");
DebugTln(F("Time resync needed"));
NtpStatus = TIME_NEEDSYNC;
}
break;

View File

@ -213,7 +213,7 @@ void sendOTGWlabel(const char *msglabel){
void sendTelegraf()
{
RESTDebugTln("sending OT monitor values to Telegraf...\r");
RESTDebugTln(F("sending OT monitor values to Telegraf...\r"));
sendStartJsonArray();
@ -262,7 +262,7 @@ void sendTelegraf()
void sendOTmonitor()
{
RESTDebugTln("sending OT monitor values ...\r");
RESTDebugTln(F("sending OT monitor values ...\r"));
sendStartJsonObj("otmonitor");
@ -402,7 +402,7 @@ void sendDeviceTime()
//=======================================================================
void sendDeviceSettings()
{
RESTDebugTln("sending device settings ...\r");
RESTDebugTln(F("sending device settings ...\r"));
sendStartJsonObj("settings");

View File

@ -2,15 +2,15 @@
#define _VERSION_MAJOR 0
#define _VERSION_MINOR 8
#define _VERSION_PATCH 7
#define _VERSION_BUILD 1132
#define _VERSION_GITHASH "8170a7b"
#define _VERSION_BUILD 1145
#define _VERSION_GITHASH "6a2b1b5"
#define _VERSION_PRERELEASE beta
#define _VERSION_DATE "22-10-2021"
#define _VERSION_TIME "00:16:14"
#define _VERSION_DATE "23-10-2021"
#define _VERSION_TIME "21:12:41"
#define _SEMVER_CORE "0.8.7"
#define _SEMVER_BUILD "0.8.7+1132"
#define _SEMVER_GITHASH "0.8.7+8170a7b"
#define _SEMVER_FULL "0.8.7-beta+8170a7b"
#define _SEMVER_NOBUILD "0.8.7-beta (22-10-2021)"
#define _VERSION "0.8.7-beta+8170a7b (22-10-2021)"
#define _SEMVER_BUILD "0.8.7+1145"
#define _SEMVER_GITHASH "0.8.7+6a2b1b5"
#define _SEMVER_FULL "0.8.7-beta+6a2b1b5"
#define _SEMVER_NOBUILD "0.8.7-beta (23-10-2021)"
#define _VERSION "0.8.7-beta+6a2b1b5 (23-10-2021)"
//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver