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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user