Benutzer-Werkzeuge

Webseiten-Werkzeuge


sonstiges:tuerklingel

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
sonstiges:tuerklingel [2023-09-29 22:37] – [Funk-Türklingel] schuballaasonstiges:tuerklingel [2025-02-05 20:12] (aktuell) – [Sender in Haustelefon einbauen] schuballaa
Zeile 9: Zeile 9:
 **Optional:** **Optional:**
  
-Es könnte zu jeder zeit an sender und oder Empfängern eine Li-ion Akku (via JST) angeschlossen werden und somit auch mobil betrieben werden z.b bei Veranstaltungen die im Hinnenhof stattfinden um kein Klingeln zu verpassen.+Es könnte zu jeder zeit an den Empfängern eine Li-ion Akku (via JST) angeschlossen werden und somit auch mobil betrieben werden z.b bei Veranstaltungen die im Hinnenhof stattfinden um kein Klingeln zu verpassen.
  
 **Achtung bei Powerbanks!** **Achtung bei Powerbanks!**
Zeile 21: Zeile 21:
 Das Haustelefon ist vom Typ STR HT 3033. Das Haustelefon ist vom Typ STR HT 3033.
  
-Das Steuergerät ist ein SP 333 ({{:sonstiges:einstellungen_sp333_version_2.pdf|Doku}} ), welches im Hauskeller im Sicherungsschrank neben dem Netzteil NH 333 installiert ist.+Das Steuergerät ist ein SP 333 ({{:sonstiges:einstellungen_sp333_version_2.pdf|Doku}}  ), welches im Hauskeller im Sicherungsschrank neben dem Netzteil NH 333 installiert ist.
  
 ===== Pinout ===== ===== Pinout =====
Zeile 37: Zeile 37:
 Da uns keine Leitung zwischen den Räumen Bornstr. 14-15 sowie 16-17 zur Verfügung steht, mussten wir auf eine [[http://www.amazon.de/dp/B00ISMW1DG|Funk-Türklingel]] mit zwei Empfängern zurückgreifen. Der Sender sowie die 2 Empfänger werden mit einem Standard 5 Volt USB Netzteil versorgt. Da uns keine Leitung zwischen den Räumen Bornstr. 14-15 sowie 16-17 zur Verfügung steht, mussten wir auf eine [[http://www.amazon.de/dp/B00ISMW1DG|Funk-Türklingel]] mit zwei Empfängern zurückgreifen. Der Sender sowie die 2 Empfänger werden mit einem Standard 5 Volt USB Netzteil versorgt.
  
-Wenn geklingelt wird, zieht ein [[[https://datasheetspdf.com/pdf-file/525208/AnpecElectronicsCoropration/APM2030N/1|n channel mosfet]] den Pin 4 des [[https://www.amazon.de/AZDelivery-Lolin-ESP32-Parent/dp/B086V1P4BL?th=1|esp32]] auf ground. Wenn dieses > 1000 ms auf Ground bleibt, wurde geklingelt und ein ESPNOW signal zu den Empfängern geschickt.+Änderungen am 04.02.2025: 
 + 
 +Der Mosfet (siehe Bild) hat nicht mehr das Klingel Signal zum ESPNOW Sender weitergeleitetDaher dedektiere ich das Klingelsignal nun über einen H11LX (Optokoppler) der beim Klingeln den Pin 04 LOW setzt und ein ESPNOW signal zu den Empfängern ,,Klingeln" schickt. 
 + 
 +Zusätzlich habe ich den Empfängern mit einem Watchchdog und ein auto restart (jede Minute) ausgestattet. 
 + 
 +Nachtrag am 05.02.2025: 
 + 
 +Es kam gelegentlich zu Fehlauslösungen. Daher habe ich in der Software eingestellt, dass das Klingelsignal mindestens 1800 ms anliegen muss (In meinen Tests war das Signal nie kürzer als 2000 ms). 
  
 ===== Sender in Haustelefon einbauen ===== ===== Sender in Haustelefon einbauen =====
  
-Der Sender ist ein ESP32 Lolin Lite welcher auf ein Trigger auf Pin 4 wartet (als sensor wird ein n chanel mosfet benutzt(Bild: Patine mitte links)+Der Sender ist ein ESP32 Lolin Lite welcher auf ein Trigger auf Pin 4 wartet.
  
 {{https://wiki.hackerspace-bremen.de/_media/sonstiges/klingel_sender.jpg?nolink&600x800|klingel_sender.jpg}} {{https://wiki.hackerspace-bremen.de/_media/sonstiges/klingel_sender.jpg?nolink&600x800|klingel_sender.jpg}}
 +
  
 ===== Sourcecode ===== ===== Sourcecode =====
  
 Sender: Sender:
- 
 <code> <code>
-#include <WiFi.h>\\ + 
-#include <esp_wifi.h>\\+#include <WiFi.h> 
 +#include <esp_wifi.h>
 #include <esp_now.h> #include <esp_now.h>
  
-//Set your new MAC Address\\ +// Set your new MAC Address 
-uint8_t newMACAddress[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; //REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS\\ +uint8_t newMACAddress[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; 
-uint8_t broadcastAddress1[] = {0x30, 0xAF, 0xA1, 0x05, 0x1D, 0x63};\\+uint8_t broadcastAddress1[] = {0x30, 0xAF, 0xA1, 0x05, 0x1D, 0x63};
 uint8_t broadcastAddress2[] = {0x30, 0xAF, 0xA2, 0x05, 0x1D, 0x63}; uint8_t broadcastAddress2[] = {0x30, 0xAF, 0xA2, 0x05, 0x1D, 0x63};
  
-typedef struct bell_struct {\\ +unsigned long TestpreviousMillis = 0; 
-uint64_t trigger;\\+unsigned long DebugpreviousMillis = 0; 
 + 
 +const long Testinterval = 60000; 
 +const long Debuginterval = 1000; 
 + 
 +int testPin = 33; 
 +int triggerPin = 4; 
 + 
 +int testmode = 0; 
 +int testmodeState = 0; 
 +int triggerState = 0; 
 + 
 +typedef struct bell_struct { 
 +  uint64_t trigger;
 } bell_struct; } bell_struct;
  
Zeile 67: Zeile 90:
 esp_now_peer_info_t peerInfo; esp_now_peer_info_t peerInfo;
  
-//callback when data is sent\\ +// Variabledie von der ISR gesetzt wird 
-void OnDataSent(const uint8_t *mac_addresp_now_send_status_t status) {\\ +volatile bool bellTriggered = false;
-char macStr[18];\\ +
-Serial.print("Packet to: ");// \\ +
-Copies the sender mac address to a string\\ +
-snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", +
- +
-       mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);\\+
  
-Serial.print(macStr);\\ +// Callback when data is sent 
-Serial.print(" send status:\t");\\ +void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 
-Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");\\+  char macStr[18]; 
 +  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", 
 +           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 
 +  Serial.print("Packet to: "); 
 +  Serial.print(macStr); 
 +  Serial.print(" send status:\t"); 
 +  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
 } }
  
-void setup() {\\ +void setup() { 
-Serial.begin(115200);+  Serial.begin(115200);
  
-WiFi.mode(WIFI_STA);+  WiFi.mode(WIFI_STA);
  
-//Set the ESP32 Board MAC Address.\\ +  // Set the ESP32 Board MAC Address 
-esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]); //Print the new MAC Address.\\ +  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]); 
-Serial.print("[NEW] ESP32 Board MAC Address: ");\\ +  Serial.print("[NEW] ESP32 Board MAC Address: "); 
-Serial.println(WiFi.macAddress());+  Serial.println(WiFi.macAddress());
  
-if (esp_now_init() != ESP_OK) { +  if (esp_now_init() != ESP_OK) { 
-Serial.println("Error initializing ESP-NOW");\\ +    Serial.println("Error initializing ESP-NOW"); 
-ESP.restart();\\+    ESP.restart(); 
 +  }
  
-}+  esp_now_register_send_cb(OnDataSent);
  
-esp_now_register_send_cb(OnDataSent);+  // Register first peer 
 +  peerInfo.channel = 0; 
 +  peerInfo.encrypt = false; 
 +  memcpy(peerInfo.peer_addr, broadcastAddress1, 6); 
 +  if (esp_now_add_peer(&peerInfo) != ESP_OK) { 
 +    Serial.println("Failed to add peer 1"); 
 +    return; 
 +  }
  
-//register peer\\ +  // Register second peer 
-peerInfo.channel = 0;\\ +  memcpy(peerInfo.peer_addr, broadcastAddress2, 6); 
-peerInfo.encrypt = false;// \\ +  if (esp_now_add_peer(&peerInfo) != ESP_OK) { 
-register first peer\\ +    Serial.println("Failed to add peer 2"); 
-memcpy(peerInfo.peer_addr, broadcastAddress1, 6);\\ +    return; 
-if (esp_now_add_peer(&peerInfo) != ESP_OK) { +  }
-Serial.println("Failed to add peer");\\ +
-return;\\+
  
 +  // Set pin 4 as an interrupt
 +  pinMode(triggerPin, INPUT);
 +  pinMode(testPin, INPUT_PULLUP);
 +  attachInterrupt(digitalPinToInterrupt(triggerPin), triggerbell, FALLING);
 } }
  
-//register second peer\\ +// Minimal Interrupt-Service-Routine 
-memcpy(peerInfo.peer_addr, broadcastAddress2, 6);\\ +void triggerbell() { 
-if (esp_now_add_peer(&peerInfo) !ESP_OK) {\\ +  bellTriggered true; // Setzt die Flag-Variable
-Serial.println("Failed to add peer");\\ +
-return;\\ +
-//Set pin 4 as an interrupt\\ +
-pinMode(4, INPUT_PULLUP);\\ +
-attachInterrupt(digitalPinToInterrupt(4), triggerbell, FALLING);\\+
 } }
  
-void triggerbell() {\\ +void testbell() { 
-bell.trigger = 8562190430;+  bell.trigger = 4042190430;
  
-esp_err_t result = esp_now_send(0, (uint8_t *) &bell, sizeof(bell_struct));+  esp_err_t result = esp_now_send(0, (uint8_t *)&bell, sizeof(bell_struct));
  
-if (result == ESP_OK) {+  if (result == ESP_OK) { 
 +    Serial.println("Sent with success"); 
 +  } else { 
 +    Serial.println("Error sending the data"); 
 +  }
  
-Serial.println("Sent with success");\\ +  Serial.print("[NEW] ESP32 Board MAC Address: "); 
- +  Serial.println(WiFi.macAddress());
-} else { +
-Serial.println("Error sending the data");\\+
 } }
  
-Serial.print("[NEW] ESP32 Board MAC Address: ");\\ +void loop() { 
-Serial.println(WiFi.macAddress());+  unsigned long currentMillis = millis();
  
-}+  testmodeState = digitalRead(testPin); 
 +  triggerState = digitalRead(triggerPin);
  
-void loop() {+  // Verarbeitung, wenn Interrupt ausgelöst wurde 
 +  if (bellTriggered) { 
 +    bellTriggered = false; // Zurücksetzen der Flag-Variable
  
 +    // Senden des Klingelsignals
 +    bell.trigger = 8562190430;
 +
 +    esp_err_t result = esp_now_send(0, (uint8_t *)&bell, sizeof(bell_struct));
 +
 +    if (result == ESP_OK) {
 +      Serial.println("Sent with success");
 +    } else {
 +      Serial.println("Error sending the data");
 +    }
 +
 +    Serial.print("[NEW] ESP32 Board MAC Address: ");
 +    Serial.println(WiFi.macAddress());
 +  }
 +
 +  // Testmodus umschalten
 +  if (testmodeState == 0 && testmode == 0) {
 +    Serial.println("Klingelsendertestmodus ist aktiviert!");
 +    testmode = 1;
 +    TestpreviousMillis = Testinterval + currentMillis;
 +  }
 +
 +  if (testmodeState == 1 && testmode == 1) {
 +    Serial.println("Klingelsendertestmodus wird deaktiviert!");
 +    testmode = 0;
 +    TestpreviousMillis = Testinterval + currentMillis;
 +  }
 +
 +  // Debugging-Ausgabe
 +  if (currentMillis - DebugpreviousMillis>= Debuginterval) {
 +    DebugpreviousMillis = currentMillis;
 +    Serial.print("testmodeState: ");
 +    Serial.print(testmodeState);
 +    Serial.print(" - triggerState: ");
 +    Serial.println(triggerState);
 +  }
 +
 +  // Testklingelsignal im Testmodus senden
 +  if (currentMillis - TestpreviousMillis>= Testinterval) {
 +    TestpreviousMillis = currentMillis;
 +    if (testmode == 1) {
 +      Serial.println("sende Testklingel signal (payload: 4042190430)");
 +      testbell();
 +    } else {
 +      Serial.println("warte auf klingeln, testmodus deaktiviert!");
 +    }
 +  }
 } }
 +
 </code> </code>
  
Zeile 146: Zeile 227:
  
 <code> <code>
-#include <WiFi.h>\\ +#include <WiFi.h> 
-#include <esp_wifi.h>\\+#include <esp_wifi.h>
 #include <esp_now.h> #include <esp_now.h>
 +#include <Ticker.h>
  
-//Set MAC Address this device (bell receiver 1)\\ +// Set MAC Address this device (bell receiver 1) 
-uint8_t newMACAddress[] = {0x30, 0xAF, 0xA1, 0x05, 0x1D, 0x63}; uint8_t addr[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; unsigned long lastPrintoutTime = 0;\\ +uint8_t newMACAddress[] = {0x30, 0xAF, 0xA1, 0x05, 0x1D, 0x63}; 
-bool isTriggered false;\\ +uint8_t addr[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; 
-unsigned long lastTriggeredTime = 0; typedef struct bell_struct {\\ +unsigned long lastPrintoutTime = 0; 
-uint64_t trigger;\\ +unsigned long lastRebootTime 0
-} bell_struct; //Create a struct_message called myData\\ +unsigned long lastDebugTime = 0; 
-bell_struct myData;+const unsigned long DebugIntervall = 1000
 +const unsigned long REBOOT_INTERVAL = 60000; // Periodischer Neustart nach 60 Sekunden
  
-void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {\\ +bool isTriggered false
-memcpy(&myData, incomingData, sizeof(myData));\\ +unsigned long lastTriggeredTime = 0;
-Serial.print("Bytes received: ");\\ +
-Serial.println(len);\\ +
-Serial.print("received command:");\\ +
-Serial.println(myData.trigger);\\ +
-if (memcmp(mac, addr, 6) == 0) { // Check if expected Mac Addr\\ +
-Serial.println("klingel device received!");\\ +
-if (myData.trigger == 8562190430) {\\ +
-Serial.println("valid Trigger code received!");\\ +
-triggerbell();\\ +
-}\\ +
-}\\ +
-} void setup() { Serial.begin(115200); Serial.println(); WiFi.mode(WIFI_STA); Serial.print("[OLD] ESP32 Board MAC Address: ");\\ +
-Serial.println(WiFi.macAddress()); //ESP32 Board add-on before version < 1.0.5\\ +
-//esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]); // ESP32 Board add-on after version > 1.0.5\\ +
-esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);+
  
-Serial.print("[NEW] ESP32 Board MAC Address: ");\\ +int selftest = 0;
-Serial.println(WiFi.macAddress());+
  
-pinMode(LED_BUILTIN, OUTPUT);\\ +typedef struct bell_struct { 
-pinMode(4, OUTPUT);+  uint64_t trigger
 +} bell_struct;
  
-digitalWrite(LED_BUILTIN, LOW);\\ +// Create struct_message called myData 
-digitalWrite(4, HIGH); // trigger bell\\ +bell_struct myData;
-delay(100);\\ +
-digitalWrite(4, LOW);\\ +
-digitalWrite(LED_BUILTIN, HIGH);\\ +
-delay(100); //Set device as Wi-Fi Station\\ +
-WiFi.mode(WIFI_STA);+
  
-//Init ESP-NOW\\ +//INITIALIZE WATCHDOG////////// 
-if (esp_now_init() != ESP_OK) {\\ +//watchdogCount==30 means it will wait 30 seconds before it will automatically reset on a hang 
-Serial.println("Error initializing ESP-NOW");\\ +//got this watchdog code from Andreas Spiess "The Guy with the Swiss Accent"
-ESP.restart();\\ +
-//Once ESPNow is successfully Init, we will register for recv CB to\\ +
-//get recv packer info\\ +
-esp_now_register_recv_cb(OnDataRecv);\\ +
-}\\ +
-void triggerbell() {\\ +
-if (!isTriggered || (millis() - lastTriggeredTime > 10000)) {\\ +
-isTriggered true;\\ +
-lastTriggeredTime millis(); digitalWrite(LED_BUILTIN, LOW);\\ +
-digitalWrite(4, HIGH); //trigger bell+
  
-delay(100);\\ +Ticker secondTick
-digitalWrite(4, LOW);\\ +volatile int watchdogCount = 0; 
-digitalWrite(LED_BUILTIN, HIGH);\\ + 
-Serial.println("bell triggered");\\+void ISRwatchdog() 
 +  watchdogCount ++
 +  if (watchdogCount>= 180{ 
 +    Serial.println("crashed"); 
 +    ESP.restart(); 
 +  }
 } }
 +//END WATCHDOG
 +
 +void printRestartReason() {
 +  esp_reset_reason_t reason = esp_reset_reason();
 +  Serial.print("Restart reason: ");
 +  switch (reason) {
 +    case ESP_RST_POWERON: Serial.println("Power on"); selftest = 1; break;
 +    case ESP_RST_EXT: Serial.println("External reset"); break;
 +    case ESP_RST_SW: Serial.println("Software reset"); break;
 +    case ESP_RST_PANIC: Serial.println("Exception/panic"); break;
 +    case ESP_RST_INT_WDT: Serial.println("Interrupt watchdog"); break;
 +    case ESP_RST_TASK_WDT: Serial.println("Task watchdog"); break;
 +    case ESP_RST_WDT: Serial.println("Other watchdog"); break;
 +    case ESP_RST_DEEPSLEEP: Serial.println("Deep sleep"); break;
 +    case ESP_RST_BROWNOUT: Serial.println("Brownout"); break;
 +    case ESP_RST_SDIO: Serial.println("SDIO"); break;
 +    default: Serial.println("Unknown"); break;
 +  }
 } }
  
-void loop() {\\ +void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) { 
-//Print out the `isTriggered` variable every second\\ +  memcpy(&myData, incomingData, sizeof(myData)); 
-if (millis() - lastPrintoutTime > 1000{\\ +  Serial.print("Bytes received: "); 
-Serial.print("isTriggered: ");\\ +  Serial.println(len); 
-Serial.println(isTriggered);\\ +  Serial.print("received command: "); 
-lastPrintoutTime = millis();\\ +  Serial.println(myData.trigger);
-}// \\ +
-\\ +
-Reset the `isTriggered` variable after 5 seconds\\ +
-if (millis() - lastTriggeredTime > 5000) {+
  
-isTriggered false;\\+  // Prüfen, ob die MAC-Adresse übereinstimmt 
 +  if (memcmp(info->src_addr, addr, 6) == 0) { 
 +    Serial.println("klingel device received!"); 
 +    if (myData.trigger == 8562190430) { 
 +      Serial.println("valid Trigger code received!"); 
 +      triggerbell(); 
 +    } 
 +  } 
 +}
  
 +void setup() {
 +  Serial.begin(115200);
 +  Serial.println();
 +  secondTick.attach(1, ISRwatchdog);
 +  printRestartReason();
 +
 +  WiFi.mode(WIFI_STA);
 +  Serial.print("[OLD] ESP32 Board MAC Address: ");
 +  Serial.println(WiFi.macAddress());
 +
 +  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
 +  Serial.print("[NEW] ESP32 Board MAC Address: ");
 +  Serial.println(WiFi.macAddress());
 +
 +  pinMode(LED_BUILTIN, OUTPUT);
 +  pinMode(4, OUTPUT);
 +
 +  if (selftest == 1) {
 +    Serial.println("selbsttest!");
 +    digitalWrite(LED_BUILTIN, LOW);
 +    digitalWrite(4, HIGH); // Trigger bell
 +    delay(100);
 +    digitalWrite(4, LOW);
 +    digitalWrite(LED_BUILTIN, HIGH);
 +    delay(100);
 +  } else {
 +    Serial.println("kein selbsttest!");
 +  }
 +
 +  WiFi.mode(WIFI_STA);
 +
 +  // ESP-NOW initialisieren
 +  if (esp_now_init() != ESP_OK) {
 +    Serial.println("Error initializing ESP-NOW");
 +    ESP.restart();
 +  }
 +
 +  esp_now_register_recv_cb(OnDataRecv);
 +
 +  lastRebootTime = millis(); // Zeit des letzten Neustarts speichern
 } }
  
 +void triggerbell() {
 +  if (!isTriggered || (millis() - lastTriggeredTime> 10000)) {
 +    isTriggered = true;
 +    lastTriggeredTime = millis();
 +    digitalWrite(LED_BUILTIN, LOW);
 +    digitalWrite(4, HIGH); // Trigger bell
 +
 +    delay(100);
 +    digitalWrite(4, LOW);
 +    digitalWrite(LED_BUILTIN, HIGH);
 +    Serial.println("bell triggered");
 +  }
 } }
 +
 +void loop() {
 +
 +  // Reset der `isTriggered`-Variable nach 5 Sekunden
 +  if (millis() - lastTriggeredTime> 5000) {
 +    isTriggered = false;
 +  }
 +
 +  // Periodischer Neustart alle 60 Sekunden
 +  if (millis() - lastRebootTime> REBOOT_INTERVAL) {
 +    Serial.println("Performing periodic reboot...");
 +    ESP.restart();
 +  }
 +
 +  if (millis() - lastDebugTime> DebugIntervall) {
 +    lastDebugTime = millis();
 +    Serial.print("isTriggered: ");
 +    Serial.println(isTriggered);
 +  }
 +
 +  watchdogCount = 0;
 +}
 +
 </code> </code>
  
 Empfänger2 Empfänger2
- 
 <code> <code>
-#include <WiFi.h>\\ + 
-#include <esp_wifi.h>\\+#include <WiFi.h> 
 +#include <esp_wifi.h>
 #include <esp_now.h> #include <esp_now.h>
 +#include <Ticker.h>
  
-//Set MAC Address this device (bell receiver 2)\\ +// Set MAC Address this device (bell receiver 2) 
-uint8_t newMACAddress[] = {0x30, 0xAF, 0xA2, 0x05, 0x1D, 0x63}; uint8_t addr[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; unsigned long lastPrintoutTime = 0;\\ +uint8_t newMACAddress[] = {0x30, 0xAF, 0xA2, 0x05, 0x1D, 0x63}; 
-bool isTriggered false;\\ +uint8_t addr[] = {0x30, 0xAF, 0xA0, 0x05, 0x1D, 0x63}; 
-unsigned long lastTriggeredTime = 0; typedef struct bell_struct {\\ +unsigned long lastPrintoutTime = 0; 
-uint64_t trigger;\\ +unsigned long lastRebootTime 0
-} bell_struct; //Create a struct_message called myData\\ +unsigned long lastDebugTime = 0; 
-bell_struct myData;+const unsigned long DebugIntervall = 1000
 +const unsigned long REBOOT_INTERVAL = 60000; // Periodischer Neustart nach 60 Sekunden
  
-void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {\\ +bool isTriggered false
-memcpy(&myData, incomingData, sizeof(myData));\\ +unsigned long lastTriggeredTime = 0;
-Serial.print("Bytes received: ");\\ +
-Serial.println(len);\\ +
-Serial.print("received command:");\\ +
-Serial.println(myData.trigger);\\ +
-if (memcmp(mac, addr, 6) == 0) { // Check if expected Mac Addr\\ +
-Serial.println("klingel device received!");\\ +
-if (myData.trigger == 8562190430) {\\ +
-Serial.println("valid Trigger code received!");\\ +
-triggerbell();\\ +
-}\\ +
-}\\ +
-} void setup() { Serial.begin(115200); Serial.println(); WiFi.mode(WIFI_STA); Serial.print("[OLD] ESP32 Board MAC Address: ");\\ +
-Serial.println(WiFi.macAddress()); //ESP32 Board add-on before version < 1.0.5\\ +
-//esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]); // ESP32 Board add-on after version > 1.0.5\\ +
-esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);+
  
-Serial.print("[NEW] ESP32 Board MAC Address: ");\\ +int selftest = 0;
-Serial.println(WiFi.macAddress());+
  
-pinMode(LED_BUILTIN, OUTPUT);\\ +typedef struct bell_struct { 
-pinMode(4, OUTPUT);+  uint64_t trigger
 +} bell_struct;
  
-digitalWrite(LED_BUILTIN, LOW);\\ +// Create struct_message called myData 
-digitalWrite(4, HIGH); // trigger bell\\ +bell_struct myData;
-delay(100);\\ +
-digitalWrite(4, LOW);\\ +
-digitalWrite(LED_BUILTIN, HIGH);\\ +
-delay(100); //Set device as Wi-Fi Station\\ +
-WiFi.mode(WIFI_STA);+
  
-//Init ESP-NOW\\ +//INITIALIZE WATCHDOG////////// 
-if (esp_now_init() != ESP_OK) {\\ +//watchdogCount==30 means it will wait 30 seconds before it will automatically reset on a hang 
-Serial.println("Error initializing ESP-NOW");\\ +//got this watchdog code from Andreas Spiess "The Guy with the Swiss Accent"
-ESP.restart();\\ +
-//Once ESPNow is successfully Init, we will register for recv CB to\\ +
-//get recv packer info\\ +
-esp_now_register_recv_cb(OnDataRecv);\\ +
-}\\ +
-void triggerbell() {\\ +
-if (!isTriggered || (millis() - lastTriggeredTime > 10000)) {\\ +
-isTriggered true;\\ +
-lastTriggeredTime millis(); digitalWrite(LED_BUILTIN, LOW);\\ +
-digitalWrite(4, HIGH); //trigger bell+
  
-delay(100);\\ +Ticker secondTick
-digitalWrite(4, LOW);\\ +volatile int watchdogCount = 0;
-digitalWrite(LED_BUILTIN, HIGH);\\ +
-Serial.println("bell triggered");\\+
  
 +void ISRwatchdog() {
 +  watchdogCount ++;
 +  if (watchdogCount>= 180) {
 +    Serial.println("crashed");
 +    ESP.restart();
 +  }
 } }
 +//END WATCHDOG
 +
 +void printRestartReason() {
 +  esp_reset_reason_t reason = esp_reset_reason();
 +  Serial.print("Restart reason: ");
 +  switch (reason) {
 +    case ESP_RST_POWERON: Serial.println("Power on"); selftest = 1; break;
 +    case ESP_RST_EXT: Serial.println("External reset"); break;
 +    case ESP_RST_SW: Serial.println("Software reset"); break;
 +    case ESP_RST_PANIC: Serial.println("Exception/panic"); break;
 +    case ESP_RST_INT_WDT: Serial.println("Interrupt watchdog"); break;
 +    case ESP_RST_TASK_WDT: Serial.println("Task watchdog"); break;
 +    case ESP_RST_WDT: Serial.println("Other watchdog"); break;
 +    case ESP_RST_DEEPSLEEP: Serial.println("Deep sleep"); break;
 +    case ESP_RST_BROWNOUT: Serial.println("Brownout"); break;
 +    case ESP_RST_SDIO: Serial.println("SDIO"); break;
 +    default: Serial.println("Unknown"); break;
 +  }
 } }
  
-void loop() {\\ +void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) { 
-//Print out the `isTriggered` variable every second\\ +  memcpy(&myData, incomingData, sizeof(myData)); 
-if (millis() - lastPrintoutTime > 1000{\\ +  Serial.print("Bytes received: "); 
-Serial.print("isTriggered: ");\\ +  Serial.println(len); 
-Serial.println(isTriggered);\\ +  Serial.print("received command: "); 
-lastPrintoutTime = millis();\\ +  Serial.println(myData.trigger);
-}// \\ +
-\\ +
-Reset the `isTriggered` variable after 5 seconds\\ +
-if (millis() - lastTriggeredTime > 5000) {+
  
-isTriggered false;\\+  // Prüfen, ob die MAC-Adresse übereinstimmt 
 +  if (memcmp(info->src_addr, addr, 6) == 0) { 
 +    Serial.println("klingel device received!"); 
 +    if (myData.trigger == 8562190430) { 
 +      Serial.println("valid Trigger code received!"); 
 +      triggerbell(); 
 +    } 
 +  } 
 +
 + 
 +void setup() { 
 +  Serial.begin(115200); 
 +  Serial.println(); 
 +  secondTick.attach(1, ISRwatchdog); 
 +  printRestartReason(); 
 + 
 +  WiFi.mode(WIFI_STA); 
 +  Serial.print("[OLD] ESP32 Board MAC Address: "); 
 +  Serial.println(WiFi.macAddress()); 
 + 
 +  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]); 
 +  Serial.print("[NEW] ESP32 Board MAC Address: "); 
 +  Serial.println(WiFi.macAddress()); 
 + 
 +  pinMode(LED_BUILTIN, OUTPUT); 
 +  pinMode(4, OUTPUT); 
 + 
 +  if (selftest == 1) { 
 +    Serial.println("selbsttest!"); 
 +    digitalWrite(LED_BUILTIN, LOW); 
 +    digitalWrite(4, HIGH); // Trigger bell 
 +    delay(100); 
 +    digitalWrite(4, LOW); 
 +    digitalWrite(LED_BUILTIN, HIGH); 
 +    delay(100); 
 +  } else { 
 +    Serial.println("kein selbsttest!"); 
 +  } 
 + 
 +  WiFi.mode(WIFI_STA); 
 + 
 +  // ESP-NOW initialisieren 
 +  if (esp_now_init() != ESP_OK) { 
 +    Serial.println("Error initializing ESP-NOW"); 
 +    ESP.restart(); 
 +  } 
 + 
 +  esp_now_register_recv_cb(OnDataRecv);
  
 +  lastRebootTime = millis(); // Zeit des letzten Neustarts speichern
 } }
  
 +void triggerbell() {
 +  if (!isTriggered || (millis() - lastTriggeredTime> 10000)) {
 +    isTriggered = true;
 +    lastTriggeredTime = millis();
 +    digitalWrite(LED_BUILTIN, LOW);
 +    digitalWrite(4, HIGH); // Trigger bell
 +
 +    delay(100);
 +    digitalWrite(4, LOW);
 +    digitalWrite(LED_BUILTIN, HIGH);
 +    Serial.println("bell triggered");
 +  }
 } }
 +
 +void loop() {
 +
 +  // Reset der `isTriggered`-Variable nach 5 Sekunden
 +  if (millis() - lastTriggeredTime> 5000) {
 +    isTriggered = false;
 +  }
 +
 +  // Periodischer Neustart alle 60 Sekunden
 +  if (millis() - lastRebootTime> REBOOT_INTERVAL) {
 +    Serial.println("Performing periodic reboot...");
 +    ESP.restart();
 +  }
 +
 +  if (millis() - lastDebugTime> DebugIntervall) {
 +    lastDebugTime = millis();
 +    Serial.print("isTriggered: ");
 +    Serial.println(isTriggered);
 +  }
 +
 +  watchdogCount = 0;
 +}
 +
 </code> </code>
  
  
sonstiges/tuerklingel.1696019875.txt.gz · Zuletzt geändert: 2023-09-29 22:37 von schuballaa