reworked mqtt for new server + added watchdog
This commit is contained in:
@@ -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<length;i++) {
|
||||
gotmsg[i]=(char)payload[i];
|
||||
}
|
||||
|
||||
//needs to be checked here as a client.publish overwrites the topic string...
|
||||
if (strncmp(topic,MQTT_HEARTBEAT,strlen(MQTT_HEARTBEAT)) == 0){
|
||||
doTransmitStatus = true;
|
||||
@@ -220,76 +222,82 @@ void Callback(char* topic, byte* payload, unsigned int length) {
|
||||
doTransmitStatus = false;
|
||||
}
|
||||
|
||||
client.publish(MQTT_DEBUG, gotmsg, true);
|
||||
|
||||
gotmsg[length]=0;
|
||||
Serial.println(gotmsg);
|
||||
|
||||
|
||||
|
||||
unsigned long start_time = micros();
|
||||
if (strcmp(gotmsg,"OPEN") == 0) {
|
||||
client.publish(MQTT_DEBUG, "Request garage door opened.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorAuf);
|
||||
if (!radio.write( &TorAuf, strlen(TorAuf) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
} else if (strcmp(gotmsg,"CLOSE") == 0){
|
||||
client.publish(MQTT_DEBUG, "Request garage door closed.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorZu);
|
||||
if (!radio.write( &TorZu, strlen(TorZu) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
} else if (doTransmitStatus){
|
||||
client.publish(MQTT_DEBUG, "Request garage door status.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorStatus);
|
||||
if (!radio.write( &TorStatus, strlen(TorStatus) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
}
|
||||
|
||||
|
||||
while (retries<3)
|
||||
{
|
||||
retries++;
|
||||
|
||||
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
|
||||
unsigned long start_time = micros();
|
||||
if (strcmp(gotmsg,"OPEN") == 0) {
|
||||
client.publish(MQTT_DEBUG, "Request garage door opened.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorAuf);
|
||||
if (!radio.write( &TorAuf, strlen(TorAuf) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
} else if (strcmp(gotmsg,"CLOSE") == 0){
|
||||
client.publish(MQTT_DEBUG, "Request garage door closed.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorZu);
|
||||
if (!radio.write( &TorZu, strlen(TorZu) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
} else if (doTransmitStatus){
|
||||
client.publish(MQTT_DEBUG, "Request garage door status.", true);
|
||||
radio.stopListening(); // First, stop listening so we can talk.
|
||||
Serial.print(F("Now sending: "));
|
||||
Serial.println(TorStatus);
|
||||
if (!radio.write( &TorStatus, strlen(TorStatus) )){
|
||||
Serial.println(F("failed"));
|
||||
}
|
||||
doTransmit = true;
|
||||
radio.startListening(); // Now, continue listening
|
||||
}
|
||||
|
||||
|
||||
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 {
|
||||
// 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();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
|
||||
#include <SPI.h>
|
||||
#include "RF24.h"
|
||||
#include <Ticker.h>
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user