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_STATUS = "stat/mqtt_gateway/garagedoor/status";
const char* MQTT_DEBUG = "stat/mqtt_gateway/garagedoor/debug"; const char* MQTT_DEBUG = "stat/mqtt_gateway/garagedoor/debug";
const char* MQTT_HEARTBEAT = "stat/hs2/hk2/temperature";
#define MAXSIZE 100 #define MAXSIZE 100
#define CHANNEL 108 #define CHANNEL 108
char gotmsg[MAXSIZE]; char gotmsg[MAXSIZE];
char logString[MAXSIZE];
char TorOffen[] = "OPENED"; char TorOffen[] = "OPENED";
char TorGeschlossen[] = "CLOSED"; char TorGeschlossen[] = "CLOSED";
char TorInBewegung[] = "IN TRANSIT"; char TorInBewegung[] = "IN TRANSIT";
char TorAuf[] = "Öffne Garangentor"; char TorAuf[] = "Oeffne Garagentor";
char TorZu[] = "Schließe Garangentor"; char TorZu[] = "Schliesse Garagentor";
char TorStatus[] = "Schicke Status";
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
long lastMsg = 0;
char msg[MAXSIZE]; char msg[MAXSIZE];
int value = 0; int value = 0;
@@ -65,10 +68,10 @@ RF24 radio(D4,D8);
byte addresses[][6] = {"1Node","2Node"}; byte addresses[][6] = {"1Node","2Node"};
void ICACHE_RAM_ATTR resetModule() { void ICACHE_RAM_ATTR resetModule() {
Serial.print("Current time: "); /*Serial.print("Current time: ");
Serial.println(millis()); Serial.println(millis());
Serial.print("Last fed: "); Serial.print("Last fed: ");
Serial.println(LastWdgFeeding); Serial.println(LastWdgFeeding);*/
//restart if the program hangs for more than 10min... //restart if the program hangs for more than 10min...
if (millis() - LastWdgFeeding > 600000) { 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. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS); radio.setDataRate(RF24_250KBPS);
radio.setAutoAck(false);
radio.setRetries(15, 15);
//radio.setChannel(CHANNEL); //radio.setChannel(CHANNEL);
// Open a writing and reading pipe on each radio, with opposite addresses // 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"); client.subscribe("cmd/mqtt-client/garagedoor/target");
//Heartbeat meassge every 7 min... //Heartbeat meassge every 7 min...
client.subscribe("stat/hs2/hk2/temperature"); client.subscribe(MQTT_HEARTBEAT);
client.publish(MQTT_DEBUG, "Subscribed to mqtt messages", true); client.publish(MQTT_DEBUG, "Subscribed to mqtt messages", true);
LastWdgFeeding = millis(); LastWdgFeeding = millis();
@@ -196,7 +201,10 @@ void reconnect() {
void Callback(char* topic, byte* payload, unsigned int length) { void Callback(char* topic, byte* payload, unsigned int length) {
String msg; bool doTransmit;
bool doTransmitStatus;
doTransmit = false;
Serial.print("Message arrived ["); Serial.print("Message arrived [");
@@ -205,30 +213,56 @@ void Callback(char* topic, byte* payload, unsigned int length) {
for (int i=0;i<length;i++) { for (int i=0;i<length;i++) {
gotmsg[i]=(char)payload[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); client.publish(MQTT_DEBUG, gotmsg, true);
gotmsg[length]=0; gotmsg[length]=0;
Serial.println(gotmsg); Serial.println(gotmsg);
radio.stopListening(); // First, stop listening so we can talk.
Serial.print(F("Now sending "));
unsigned long start_time = micros(); 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); Serial.println(TorAuf);
if (!radio.write( &TorAuf, strlen(TorAuf) )){ if (!radio.write( &TorAuf, strlen(TorAuf) )){
Serial.println(F("failed")); Serial.println(F("failed"));
} }
doTransmit = true;
radio.startListening(); // Now, continue listening
} else if (strcmp(gotmsg,"CLOSE") == 0){ } 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); Serial.println(TorZu);
if (!radio.write( &TorZu, strlen(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
} }
delay(500);
radio.startListening(); // Now, continue listening
if (doTransmit){
// Set up a timeout period, get the current microseconds // Set up a timeout period, get the current microseconds
unsigned long started_waiting_at = micros(); unsigned long started_waiting_at = micros();
boolean timeout = false; // Set up a variable to indicate if a response was received or not boolean timeout = false; // Set up a variable to indicate if a response was received or not
@@ -242,6 +276,7 @@ void Callback(char* topic, byte* payload, unsigned int length) {
if ( timeout ){ // Describe the results if ( timeout ){ // Describe the results
Serial.println(F("Failed, response timed out.")); Serial.println(F("Failed, response timed out."));
client.publish(MQTT_DEBUG, "Failed, response timed out.", true);
} else { } else {
// Grab the response, compare, and send to debugging spew // Grab the response, compare, and send to debugging spew
len = radio.getDynamicPayloadSize(); len = radio.getDynamicPayloadSize();
@@ -249,13 +284,10 @@ void Callback(char* topic, byte* payload, unsigned int length) {
unsigned long end_time = micros(); unsigned long end_time = micros();
// Spew it // Spew it
Serial.print(F("Sent ")); sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time);
Serial.print(start_time); Serial.println(logString);
Serial.print(F(", Got response ")); client.publish(MQTT_DEBUG, logString, true);
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...
@@ -267,16 +299,14 @@ void Callback(char* topic, byte* payload, unsigned int length) {
void loop() { void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
if (!client.loop()) { if (!client.loop()) {
char msg[16]; char msg[16];
itoa(client.state(), msg, 10); itoa(client.state(), msg, 10);
client.publish(MQTT_DEBUG, msg, true); //client.publish(MQTT_DEBUG, msg, true);
reconnect(); reconnect();
} }
if ( true ) //always keep on listeing! if ( true ) //always keep on listening!
{ {
unsigned long got_time; unsigned long got_time;
@@ -289,14 +319,14 @@ void loop() {
radio.stopListening(); // First, stop listening so we can talk radio.stopListening(); // First, stop listening so we can talk
radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back. radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back.
delay(500); //delay(500);
radio.startListening(); radio.startListening();
// Now, resume listening so we catch the next packets. // Now, resume listening so we catch the next packets.
Serial.print(F("Sent response ")); Serial.print(F("Sent response ACK: "));
Serial.println(gotmsg); Serial.println(gotmsg);
Serial.print("Publish message: "); Serial.print("Publish status MQTT message: ");
Serial.println(gotmsg); Serial.println(gotmsg);
client.publish(MQTT_STATUS, gotmsg, true); client.publish(MQTT_STATUS, gotmsg, true);
} }

View File

@@ -29,8 +29,9 @@ char TorOffen[] = "OPENED";
char TorGeschlossen[] = "CLOSED"; char TorGeschlossen[] = "CLOSED";
char TorInBewegung[] = "IN TRANSIT"; char TorInBewegung[] = "IN TRANSIT";
char TorAuf[] = "Öffne Garangentor"; char TorAuf[] = "Oeffne Garagentor";
char TorZu[] = "Schließe Garangentor"; char TorZu[] = "Schliesse Garagentor";
char TorStatus[] = "Schicke Status";
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */ /* 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. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS); radio.setDataRate(RF24_250KBPS);
radio.setAutoAck(false);
radio.setRetries(15, 15);
//radio.setChannel(CHANNEL); //radio.setChannel(CHANNEL);
// Open a writing and reading pipe on each radio, with opposite addresses // Open a writing and reading pipe on each radio, with opposite addresses
@@ -84,7 +87,7 @@ void loop() {
if (TorStatusChanged == true) { if (TorStatusChanged == true) {
radio.stopListening(); // First, stop listening so we can talk. radio.stopListening(); // First, stop listening so we can talk.
Serial.print(F("Now sending ")); Serial.print(F("Now sending: "));
unsigned long start_time = micros(); unsigned long start_time = micros();
if ((TorStatus1 == true) && (TorStatus2 == false)) { if ((TorStatus1 == true) && (TorStatus2 == false)) {
@@ -157,11 +160,11 @@ if (TorStatusChanged == true) {
radio.stopListening(); // First, stop listening so we can talk radio.stopListening(); // First, stop listening so we can talk
radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back. radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets. radio.startListening(); // Now, resume listening so we catch the next packets.
Serial.print(F("Sent response ")); Serial.print(F("Sent response ACK: "));
Serial.println("ACK"); Serial.println(gotmsg);
if ((strcmp(gotmsg,TorAuf)==0) && (!TorStatus1) && (TorStatus2)) { if ((strcmp(gotmsg,TorAuf)==0) && (!TorStatus1) && (TorStatus2)) {
Serial.println("Öffne das Tor"); Serial.println("Oeffne das Tor");
digitalWrite(pinGaragenTorSchalter, LOW); digitalWrite(pinGaragenTorSchalter, LOW);
delay(1500); delay(1500);
digitalWrite(pinGaragenTorSchalter, HIGH); digitalWrite(pinGaragenTorSchalter, HIGH);
@@ -171,7 +174,7 @@ if (TorStatusChanged == true) {
} }
if ((strcmp(gotmsg,TorZu)==0) && (TorStatus1) && (!TorStatus2)) { if ((strcmp(gotmsg,TorZu)==0) && (TorStatus1) && (!TorStatus2)) {
Serial.println("Schließe das Tor"); Serial.println("Schliesse das Tor");
digitalWrite(pinGaragenTorSchalter, LOW); digitalWrite(pinGaragenTorSchalter, LOW);
delay(1500); delay(1500);
digitalWrite(pinGaragenTorSchalter, HIGH); digitalWrite(pinGaragenTorSchalter, HIGH);
@@ -179,6 +182,12 @@ if (TorStatusChanged == true) {
if ((strcmp(gotmsg,TorZu)==0) && (!TorStatus1)) { if ((strcmp(gotmsg,TorZu)==0) && (!TorStatus1)) {
Serial.println("Tor bereits geschlossen!"); 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... TorStatusChanged = true; //Tor status was changed!: Transmitting...
} }
Serial.print("Sensor 1: "); /*Serial.print("Sensor 1: ");
Serial.print(TorStatus1); Serial.print(TorStatus1);
Serial.print(" Sensor 2: "); Serial.print(" Sensor 2: ");
Serial.println(TorStatus2); Serial.println(TorStatus2);
delay(200); delay(200);*/
} // Loop } // Loop