diff --git a/MQTTGateway/MQTTGateway.ino b/MQTTGateway/MQTTGateway.ino index ecd8ed9..7ad4dcb 100644 --- a/MQTTGateway/MQTTGateway.ino +++ b/MQTTGateway/MQTTGateway.ino @@ -34,12 +34,12 @@ int len = 0; const char* ssid = STASSID; 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_DEBUG = "stat/mqtt_gateway/garagedoor/debug"; +const char* MQTT_STATUS = "stat/Badezimmer/MQTTGateway/Garage/status"; +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 CHANNEL 108 @@ -144,7 +144,7 @@ void setup() { Serial.print("IP address: "); Serial.println(WiFi.localIP()); - client.setServer(MQTT_BROKER, 1883); + client.setServer(MQTT_BROKER, 1884); client.setCallback(Callback); client.publish(MQTT_DEBUG, "Startup!", true); @@ -186,7 +186,7 @@ void reconnect() { delay(5000); } else { Serial.println("connected"); - client.subscribe("cmd/mqtt-client/garagedoor/target"); + client.subscribe("cmnd/Badezimmer/MQTTGateway/Garage/target"); //Heartbeat meassge every 7 min... client.subscribe(MQTT_HEARTBEAT); @@ -203,6 +203,7 @@ void reconnect() { void Callback(char* topic, byte* payload, unsigned int length) { bool doTransmit; bool doTransmitStatus; + int retries = 0; doTransmit = false; @@ -213,6 +214,7 @@ void Callback(char* topic, byte* payload, unsigned int length) { for (int i=0;i 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop - timeout = true; - break; - } - } - - if ( timeout ){ // Describe the results - Serial.println(F("Failed, response timed out.")); - client.publish(MQTT_DEBUG, "Failed, response timed out.", true); - } else { - // Grab the response, compare, and send to debugging spew - len = radio.getDynamicPayloadSize(); - radio.read( &gotmsg, len ); - unsigned long end_time = micros(); - - // Spew it - sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time); - Serial.println(logString); - client.publish(MQTT_DEBUG, logString, true); - } - } + if (doTransmit){ + // Set up a timeout period, get the current microseconds + unsigned long started_waiting_at = micros(); + boolean timeout = false; // Set up a variable to indicate if a response was received or not + + while ( ! radio.available() ){ // While nothing is received + if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop + timeout = true; + break; + } + } + + if ( timeout ){ // Describe the results + Serial.println(F("Failed, response timed out.")); + client.publish(MQTT_DEBUG, "Failed, response timed out.", true); + } else { + retries = 3; //exit retry loop... + + // Grab the response, compare, and send to debugging spew + len = radio.getDynamicPayloadSize(); + radio.read( &gotmsg, len ); + unsigned long end_time = micros(); + // Spew it + sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time); + Serial.println(logString); + client.publish(MQTT_DEBUG, logString, true); + } + } + + if (retries<3){ + delay(2500); //wait for 2.5ms between tries... + } + } //End of 3 retries... //feed the watchdog... LastWdgFeeding = millis(); } diff --git a/SensorActorUnit/SensorActorUnit.ino b/SensorActorUnit/SensorActorUnit.ino index 5101b4c..a242233 100644 --- a/SensorActorUnit/SensorActorUnit.ino +++ b/SensorActorUnit/SensorActorUnit.ino @@ -7,6 +7,11 @@ #include #include "RF24.h" +#include + +const uint32_t wdtTimeout = 8388607; //maximum for the ESP8266 +uint32_t LastWdgFeeding; +Ticker timer; /****************** User Config ***************************/ /*** Set this radio as radio number 0 or 1 ***/ @@ -40,6 +45,20 @@ RF24 radio(D4,D8); 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() { Serial.begin(115200); @@ -74,6 +93,10 @@ void setup() { digitalWrite(pinGaragenTorSchalter, HIGH); 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 TorStatusChanged = false; + + //feed the watchdog... + LastWdgFeeding = millis(); } @@ -152,6 +178,9 @@ if (TorStatusChanged == true) { if( radio.available()){ // Variable for the received timestamp + //feed the watchdog... + LastWdgFeeding = millis(); + while (radio.available()) { // While there is data ready len = radio.getDynamicPayloadSize(); 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)) { TorStatus1 = digitalRead(pinStatusGaragenTor1); TorStatus2 = digitalRead(pinStatusGaragenTor2);