From 567ffdb490b8c36d85375d4882ab3f6952030caf Mon Sep 17 00:00:00 2001 From: NilsGrunwald Date: Fri, 14 May 2021 15:40:51 +0200 Subject: [PATCH] Reworked GarageDoor Control units --- MQTTGateway/MQTTGateway.ino | 140 +++++++++++++++++----------- SensorActorUnit/SensorActorUnit.ino | 39 +++++--- 2 files changed, 109 insertions(+), 70 deletions(-) diff --git a/MQTTGateway/MQTTGateway.ino b/MQTTGateway/MQTTGateway.ino index 35f8f80..ecd8ed9 100644 --- a/MQTTGateway/MQTTGateway.ino +++ b/MQTTGateway/MQTTGateway.ino @@ -39,22 +39,25 @@ const char* MQTT_BROKER = "montana2000"; const char* MQTT_STATUS = "stat/mqtt_gateway/garagedoor/status"; const char* MQTT_DEBUG = "stat/mqtt_gateway/garagedoor/debug"; +const char* MQTT_HEARTBEAT = "stat/hs2/hk2/temperature"; + #define MAXSIZE 100 #define CHANNEL 108 char gotmsg[MAXSIZE]; +char logString[MAXSIZE]; char TorOffen[] = "OPENED"; char TorGeschlossen[] = "CLOSED"; char TorInBewegung[] = "IN TRANSIT"; -char TorAuf[] = "Öffne Garangentor"; -char TorZu[] = "Schließe Garangentor"; +char TorAuf[] = "Oeffne Garagentor"; +char TorZu[] = "Schliesse Garagentor"; +char TorStatus[] = "Schicke Status"; WiFiClient espClient; PubSubClient client(espClient); -long lastMsg = 0; char msg[MAXSIZE]; int value = 0; @@ -65,10 +68,10 @@ RF24 radio(D4,D8); byte addresses[][6] = {"1Node","2Node"}; void ICACHE_RAM_ATTR resetModule() { - Serial.print("Current time: "); + /*Serial.print("Current time: "); Serial.println(millis()); Serial.print("Last fed: "); - Serial.println(LastWdgFeeding); + Serial.println(LastWdgFeeding);*/ //restart if the program hangs for more than 10min... if (millis() - LastWdgFeeding > 600000) { @@ -151,6 +154,8 @@ void setup() { // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. radio.setPALevel(RF24_PA_MAX); radio.setDataRate(RF24_250KBPS); + radio.setAutoAck(false); + radio.setRetries(15, 15); //radio.setChannel(CHANNEL); // Open a writing and reading pipe on each radio, with opposite addresses @@ -184,7 +189,7 @@ void reconnect() { client.subscribe("cmd/mqtt-client/garagedoor/target"); //Heartbeat meassge every 7 min... - client.subscribe("stat/hs2/hk2/temperature"); + client.subscribe(MQTT_HEARTBEAT); client.publish(MQTT_DEBUG, "Subscribed to mqtt messages", true); LastWdgFeeding = millis(); @@ -196,7 +201,10 @@ void reconnect() { void Callback(char* topic, byte* payload, unsigned int length) { - String msg; + bool doTransmit; + bool doTransmitStatus; + + doTransmit = false; Serial.print("Message arrived ["); @@ -205,60 +213,84 @@ 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); + } } - delay(500); - radio.startListening(); // Now, continue listening - - // 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.")); - } else { - // Grab the response, compare, and send to debugging spew - len = radio.getDynamicPayloadSize(); - radio.read( &gotmsg, len ); - unsigned long end_time = micros(); - - // Spew it - Serial.print(F("Sent ")); - Serial.print(start_time); - Serial.print(F(", Got response ")); - Serial.print(gotmsg); - Serial.print(F(", Round-trip delay ")); - Serial.print(end_time-start_time); - Serial.println(F(" microseconds")); - } - - //feed the watchdog... + //feed the watchdog... LastWdgFeeding = millis(); } @@ -267,16 +299,14 @@ void Callback(char* topic, byte* payload, unsigned int length) { void loop() { ArduinoOTA.handle(); - - if (!client.loop()) { char msg[16]; itoa(client.state(), msg, 10); - client.publish(MQTT_DEBUG, msg, true); + //client.publish(MQTT_DEBUG, msg, true); reconnect(); } - if ( true ) //always keep on listeing! + if ( true ) //always keep on listening! { unsigned long got_time; @@ -289,16 +319,16 @@ void loop() { radio.stopListening(); // First, stop listening so we can talk radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back. - delay(500); + //delay(500); radio.startListening(); // Now, resume listening so we catch the next packets. - Serial.print(F("Sent response ")); + Serial.print(F("Sent response ACK: ")); Serial.println(gotmsg); - Serial.print("Publish message: "); + Serial.print("Publish status MQTT message: "); Serial.println(gotmsg); - client.publish(MQTT_STATUS, gotmsg, true); - } - } + client.publish(MQTT_STATUS, gotmsg, true); + } + } } // Loop diff --git a/SensorActorUnit/SensorActorUnit.ino b/SensorActorUnit/SensorActorUnit.ino index f34c1c2..5101b4c 100644 --- a/SensorActorUnit/SensorActorUnit.ino +++ b/SensorActorUnit/SensorActorUnit.ino @@ -29,8 +29,9 @@ char TorOffen[] = "OPENED"; char TorGeschlossen[] = "CLOSED"; char TorInBewegung[] = "IN TRANSIT"; -char TorAuf[] = "Öffne Garangentor"; -char TorZu[] = "Schließe Garangentor"; +char TorAuf[] = "Oeffne Garagentor"; +char TorZu[] = "Schliesse Garagentor"; +char TorStatus[] = "Schicke Status"; /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */ @@ -48,6 +49,8 @@ void setup() { // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. radio.setPALevel(RF24_PA_MAX); radio.setDataRate(RF24_250KBPS); + radio.setAutoAck(false); + radio.setRetries(15, 15); //radio.setChannel(CHANNEL); // Open a writing and reading pipe on each radio, with opposite addresses @@ -58,7 +61,7 @@ void setup() { radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); } - + // Start the radio listening for data radio.startListening(); @@ -84,24 +87,24 @@ void loop() { if (TorStatusChanged == true) { radio.stopListening(); // First, stop listening so we can talk. - Serial.print(F("Now sending ")); + Serial.print(F("Now sending: ")); unsigned long start_time = micros(); if ((TorStatus1 == true) && (TorStatus2 == false)) { Serial.println(TorOffen); if (!radio.write( &TorOffen, strlen(TorOffen) )){ - Serial.println(F("failed")); + Serial.println(F("failed")); } } else if ((TorStatus1 == false) && (TorStatus2 == false)) { Serial.println(TorInBewegung); if (!radio.write( &TorInBewegung, strlen(TorInBewegung) )){ - Serial.println(F("failed")); + Serial.println(F("failed")); } } else if ((TorStatus1 == false) && (TorStatus2 == true)) { - Serial.println(TorGeschlossen); + Serial.println(TorGeschlossen); if (!radio.write( &TorGeschlossen, strlen(TorGeschlossen) )){ - Serial.println(F("failed")); - } + Serial.println(F("failed")); + } } radio.startListening(); // Now, continue listening @@ -157,11 +160,11 @@ if (TorStatusChanged == true) { radio.stopListening(); // First, stop listening so we can talk radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back. radio.startListening(); // Now, resume listening so we catch the next packets. - Serial.print(F("Sent response ")); - Serial.println("ACK"); + Serial.print(F("Sent response ACK: ")); + Serial.println(gotmsg); if ((strcmp(gotmsg,TorAuf)==0) && (!TorStatus1) && (TorStatus2)) { - Serial.println("Öffne das Tor"); + Serial.println("Oeffne das Tor"); digitalWrite(pinGaragenTorSchalter, LOW); delay(1500); digitalWrite(pinGaragenTorSchalter, HIGH); @@ -171,7 +174,7 @@ if (TorStatusChanged == true) { } if ((strcmp(gotmsg,TorZu)==0) && (TorStatus1) && (!TorStatus2)) { - Serial.println("Schließe das Tor"); + Serial.println("Schliesse das Tor"); digitalWrite(pinGaragenTorSchalter, LOW); delay(1500); digitalWrite(pinGaragenTorSchalter, HIGH); @@ -179,6 +182,12 @@ if (TorStatusChanged == true) { if ((strcmp(gotmsg,TorZu)==0) && (!TorStatus1)) { Serial.println("Tor bereits geschlossen!"); } + + if (strcmp(gotmsg,TorStatus)==0) { + Serial.println("Tor status requested!"); + TorStatusChanged = true; //forciere das Sendes des Status + delay(1500); + } } } @@ -189,10 +198,10 @@ if (TorStatusChanged == true) { TorStatusChanged = true; //Tor status was changed!: Transmitting... } - Serial.print("Sensor 1: "); + /*Serial.print("Sensor 1: "); Serial.print(TorStatus1); Serial.print(" Sensor 2: "); Serial.println(TorStatus2); - delay(200); + delay(200);*/ } // Loop