Reworked GarageDoor Control units

This commit is contained in:
2021-05-14 15:40:51 +02:00
parent 67f048075b
commit 567ffdb490
2 changed files with 109 additions and 70 deletions

View File

@@ -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<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;
} else {
doTransmitStatus = false;
}
client.publish(MQTT_DEBUG, gotmsg, true);
gotmsg[length]=0;
Serial.println(gotmsg);
radio.stopListening(); // First, stop listening so we can talk.
Serial.print(F("Now sending "));
unsigned long start_time = micros();
if (strcmp(gotmsg,"OPEN") == 0) {
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"));
}
} else if (strcmp(gotmsg,"CLOSE") == 0){
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"));
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
}
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 {
// 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