reworked mqtt for new server + added watchdog

This commit is contained in:
2021-06-04 18:21:52 +02:00
parent 567ffdb490
commit 28662f0289
2 changed files with 107 additions and 70 deletions

View File

@@ -34,12 +34,12 @@ int len = 0;
const char* ssid = STASSID; const char* ssid = STASSID;
const char* password = STAPSK; const char* password = STAPSK;
const char* MQTT_BROKER = "montana2000"; const char* MQTT_BROKER = "montana2020";
const char* MQTT_STATUS = "stat/mqtt_gateway/garagedoor/status"; const char* MQTT_STATUS = "stat/Badezimmer/MQTTGateway/Garage/status";
const char* MQTT_DEBUG = "stat/mqtt_gateway/garagedoor/debug"; const char* MQTT_DEBUG = "tele/Badezimmer/MQTTGateway/Garage/debug";
const char* MQTT_HEARTBEAT = "stat/hs2/hk2/temperature"; const char* MQTT_HEARTBEAT = "stat/Flur/MA/lastRcv";
#define MAXSIZE 100 #define MAXSIZE 100
#define CHANNEL 108 #define CHANNEL 108
@@ -144,7 +144,7 @@ void setup() {
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
client.setServer(MQTT_BROKER, 1883); client.setServer(MQTT_BROKER, 1884);
client.setCallback(Callback); client.setCallback(Callback);
client.publish(MQTT_DEBUG, "Startup!", true); client.publish(MQTT_DEBUG, "Startup!", true);
@@ -186,7 +186,7 @@ void reconnect() {
delay(5000); delay(5000);
} else { } else {
Serial.println("connected"); Serial.println("connected");
client.subscribe("cmd/mqtt-client/garagedoor/target"); client.subscribe("cmnd/Badezimmer/MQTTGateway/Garage/target");
//Heartbeat meassge every 7 min... //Heartbeat meassge every 7 min...
client.subscribe(MQTT_HEARTBEAT); client.subscribe(MQTT_HEARTBEAT);
@@ -203,6 +203,7 @@ void reconnect() {
void Callback(char* topic, byte* payload, unsigned int length) { void Callback(char* topic, byte* payload, unsigned int length) {
bool doTransmit; bool doTransmit;
bool doTransmitStatus; bool doTransmitStatus;
int retries = 0;
doTransmit = false; doTransmit = false;
@@ -213,6 +214,7 @@ void Callback(char* topic, byte* payload, unsigned int length) {
for (int i=0;i<length;i++) { for (int i=0;i<length;i++) {
gotmsg[i]=(char)payload[i]; gotmsg[i]=(char)payload[i];
} }
//needs to be checked here as a client.publish overwrites the topic string... //needs to be checked here as a client.publish overwrites the topic string...
if (strncmp(topic,MQTT_HEARTBEAT,strlen(MQTT_HEARTBEAT)) == 0){ if (strncmp(topic,MQTT_HEARTBEAT,strlen(MQTT_HEARTBEAT)) == 0){
doTransmitStatus = true; doTransmitStatus = true;
@@ -220,12 +222,13 @@ void Callback(char* topic, byte* payload, unsigned int length) {
doTransmitStatus = false; doTransmitStatus = false;
} }
client.publish(MQTT_DEBUG, gotmsg, true);
gotmsg[length]=0; gotmsg[length]=0;
Serial.println(gotmsg); Serial.println(gotmsg);
while (retries<3)
{
retries++;
unsigned long start_time = micros(); unsigned long start_time = micros();
if (strcmp(gotmsg,"OPEN") == 0) { if (strcmp(gotmsg,"OPEN") == 0) {
@@ -278,11 +281,12 @@ void Callback(char* topic, byte* payload, unsigned int length) {
Serial.println(F("Failed, response timed out.")); Serial.println(F("Failed, response timed out."));
client.publish(MQTT_DEBUG, "Failed, response timed out.", true); client.publish(MQTT_DEBUG, "Failed, response timed out.", true);
} else { } else {
retries = 3; //exit retry loop...
// Grab the response, compare, and send to debugging spew // Grab the response, compare, and send to debugging spew
len = radio.getDynamicPayloadSize(); len = radio.getDynamicPayloadSize();
radio.read( &gotmsg, len ); radio.read( &gotmsg, len );
unsigned long end_time = micros(); unsigned long end_time = micros();
// Spew it // Spew it
sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time); sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time);
Serial.println(logString); Serial.println(logString);
@@ -290,6 +294,10 @@ void Callback(char* topic, byte* payload, unsigned int length) {
} }
} }
if (retries<3){
delay(2500); //wait for 2.5ms between tries...
}
} //End of 3 retries...
//feed the watchdog... //feed the watchdog...
LastWdgFeeding = millis(); LastWdgFeeding = millis();
} }

View File

@@ -7,6 +7,11 @@
#include <SPI.h> #include <SPI.h>
#include "RF24.h" #include "RF24.h"
#include <Ticker.h>
const uint32_t wdtTimeout = 8388607; //maximum for the ESP8266
uint32_t LastWdgFeeding;
Ticker timer;
/****************** User Config ***************************/ /****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/ /*** Set this radio as radio number 0 or 1 ***/
@@ -40,6 +45,20 @@ RF24 radio(D4,D8);
byte addresses[][6] = {"1Node","2Node"}; byte addresses[][6] = {"1Node","2Node"};
void ICACHE_RAM_ATTR resetModule() {
/*Serial.print("Current time: ");
Serial.println(millis());
Serial.print("Last fed: ");
Serial.println(LastWdgFeeding);*/
//restart if the program hangs for more than 10min...
if (millis() - LastWdgFeeding > 600000) {
Serial.print("Restarting due to watchdog!");
ESP.restart();
}
timer1_write(wdtTimeout);
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@@ -74,6 +93,10 @@ void setup() {
digitalWrite(pinGaragenTorSchalter, HIGH); digitalWrite(pinGaragenTorSchalter, HIGH);
TorStatusChanged = true; TorStatusChanged = true;
timer1_attachInterrupt(resetModule);
timer1_enable(TIM_DIV256, TIM_EDGE, TIM_SINGLE);
timer1_write(wdtTimeout);
} }
@@ -140,6 +163,9 @@ if (TorStatusChanged == true) {
//go back to listening //go back to listening
TorStatusChanged = false; TorStatusChanged = false;
//feed the watchdog...
LastWdgFeeding = millis();
} }
@@ -152,6 +178,9 @@ if (TorStatusChanged == true) {
if( radio.available()){ if( radio.available()){
// Variable for the received timestamp // Variable for the received timestamp
//feed the watchdog...
LastWdgFeeding = millis();
while (radio.available()) { // While there is data ready while (radio.available()) { // While there is data ready
len = radio.getDynamicPayloadSize(); len = radio.getDynamicPayloadSize();
radio.read( &gotmsg, len ); // Get the payload radio.read( &gotmsg, len ); // Get the payload
@@ -191,7 +220,7 @@ if (TorStatusChanged == true) {
} }
} }
/****************** Control Logic ***************************/ /****************** Control Logic ***************************/
if ((digitalRead(pinStatusGaragenTor1) != TorStatus1)||(digitalRead(pinStatusGaragenTor2) != TorStatus2)) { if ((digitalRead(pinStatusGaragenTor1) != TorStatus1)||(digitalRead(pinStatusGaragenTor2) != TorStatus2)) {
TorStatus1 = digitalRead(pinStatusGaragenTor1); TorStatus1 = digitalRead(pinStatusGaragenTor1);
TorStatus2 = digitalRead(pinStatusGaragenTor2); TorStatus2 = digitalRead(pinStatusGaragenTor2);