From 8f4f6d82d4966ab39594afab7d8a84ea6c5b12e0 Mon Sep 17 00:00:00 2001 From: example Date: Thu, 7 Apr 2022 01:09:05 +0200 Subject: [PATCH] =?UTF-8?q?kommentare=20eingef=C3=BCgt,=20led=20blinkt=20f?= =?UTF-8?q?ehlercodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/nodemcu/statusclient/statusclient.ino | 184 ++++++++++++------- 1 file changed, 122 insertions(+), 62 deletions(-) diff --git a/source/nodemcu/statusclient/statusclient.ino b/source/nodemcu/statusclient/statusclient.ino index 6ef9ded..ed21eb0 100644 --- a/source/nodemcu/statusclient/statusclient.ino +++ b/source/nodemcu/statusclient/statusclient.ino @@ -18,9 +18,9 @@ const int LED_PIN = 16; // D0 const int REED_PIN = 5; // D1 typedef enum { - DOOR_CLOSED = 0, - DOOR_OPEN = 1 -} door_state; + DOOR_CLOSED = 0, + DOOR_OPEN = 1 +} door_state; door_state current_door_state = DOOR_CLOSED; door_state new_door_state = DOOR_CLOSED; @@ -41,30 +41,33 @@ void init_pins() { */ pinMode(REED_PIN, INPUT_PULLUP); pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, LOW); + digitalWrite(LED_PIN, HIGH); Serial.println("[Pin] LED and REED initialized"); } void init_wifi() { - /* - * first turn wifi off and than in access point mode - * maybe turn of is not needed! - */ + /* + * Creates the ssl context. Turns wifi off and than into + * access point mode. + * TODO: is 'turn of' needed! + */ ESP8266WiFiMulti wifi; WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_STA); wifi.addAP(SSID_1, PSK_1); - wifi.addAP(SSID_2, PSK_2); + wifi.addAP(SSID_2, NULL); Serial.println("[Wifi] Wifi initialized"); wifi.run(); - if (WiFi.status() == WL_CONNECTED) { - Serial.print("[Wif] Connected to "); - Serial.println(WiFi.SSID()); - Serial.print("[Wifi] IP: "); - Serial.println(WiFi.localIP()); + if (WiFi.status() == WL_CONNECTED) { + Serial.print("[Wif] Connected to "); + Serial.println(WiFi.SSID()); + Serial.print("[Wifi] IP: "); + Serial.println(WiFi.localIP()); + set_clock(); } else { - Serial.println("[Wifi] Error: Failed to connect"); + Serial.println("[Wifi] Error: Failed to connect"); + signal_wifi_failed(); } } @@ -83,41 +86,112 @@ door_state read_door_state() { return DOOR_CLOSED; } -void toggle_led(door_state state) { - /* - * turns onboard led on or depends on the door state +void signal_door_changed() { + /* + * LED signal, if door is opened */ - if (state == DOOR_OPEN) { + uint8_t count = 2; + for(uint8_t i=0; i!= count; ++i) { digitalWrite(LED_PIN, LOW); - } else { + delay(100); digitalWrite(LED_PIN, HIGH); + delay(100); } - delay(500); +} + +void signal_send_successful() { + /* + * LED signal, if new status was send successful + */ + uint8_t count = 5; + for(uint8_t i=0; i!= count; ++i) { + digitalWrite(LED_PIN, LOW); + delay(100); + digitalWrite(LED_PIN, HIGH); + delay(100); + } +} + +void signal_clock_failed() { + /* + * LED signal, if time setting failed + */ + uint8_t count = 2; + for(uint8_t i=0; i!= count; ++i) { + digitalWrite(LED_PIN, LOW); + delay(500); + digitalWrite(LED_PIN, HIGH); + delay(500); + } + delay(2000); +} + +void signal_wifi_failed() { + /* + * LED signal, if wifi initialication was failed + */ + uint8_t count = 3; + for(uint8_t i=0; i!= count; ++i) { + digitalWrite(LED_PIN, LOW); + delay(500); + digitalWrite(LED_PIN, HIGH); + delay(500); + } + delay(2000); +} + +void signal_connect_failed() { + /* + * LED signal, if door is opened + */ + uint8_t count = 4; + for(uint8_t i=0; i!= count; ++i) { + digitalWrite(LED_PIN, LOW); + delay(500); + digitalWrite(LED_PIN, HIGH); + delay(500); + } + delay(2000); +} + +void signal_send_failed() { + /* + * LED signal, if door is opened + */ + uint8_t count = 5; + for(uint8_t i=0; i!= count; ++i) { + digitalWrite(LED_PIN, LOW); + delay(500); + digitalWrite(LED_PIN, HIGH); + delay(500); + } + delay(2000); } void set_clock() { + /* + * We need time for certificate authorization + */ + configTime(TZ_STRING, NTP_URL); - configTime(TZ_STRING, NTP_URL); - - Serial.print("Waiting for NTP time sync: "); - time_t now = time(nullptr); - while (now < 8 * 3600 * 2) { - delay(500); - Serial.print("."); - now = time(nullptr); - } - Serial.println(""); - struct tm timeinfo; - gmtime_r(&now, &timeinfo); - Serial.print("Current time: "); - Serial.print(asctime(&timeinfo)); + Serial.print("[Clock] Waiting for NTP time sync"); + time_t now = time(nullptr); + while (now < 8 * 3600 * 2) { + delay(500); + Serial.print("."); + now = time(nullptr); + } + Serial.println(""); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + Serial.print("[Clock] Current time: "); + Serial.println(asctime(&timeinfo)); } int send_status(door_state state) { /* - * geht die initialisierung mit einem byte länge? - * terminiert strcpy den status mit \0? + * Inits wifi (if needed) and send the status */ char status[2] = ""; @@ -135,17 +209,19 @@ int send_status(door_state state) { BearSSL::PrivateKey client_key(CLIENT_KEY); client.setTrustAnchors(&server_cert); client.setClientRSACert(&client_cert, &client_key); - delay(500); + delay(200); Serial.println("[Ctx] SSL Context initialized"); - Serial.print("[Ctx] Free Heap: "); - Serial.println(ESP.getFreeHeap()); - delay(500); + delay(200); + if (WiFi.status() != WL_CONNECTED) { + init_wifi(); + } Serial.printf("[Send] Connect to %s:%i\n", SERVER_URL, SERVER_PORT); client.connect(SERVER_URL, SERVER_PORT); if (!client.connected()) { Serial.println("[Send] Can't connect to server"); Serial.print("[Send] SSL Error: "); Serial.println(client.getLastSSLError()); + signal_send_failed(); client.stop(); return 1; } else { @@ -154,34 +230,19 @@ int send_status(door_state state) { Serial.println("[Send] Connection successful established"); Serial.printf("[Send] Send status: %s\n", status); client.write(status); - + signal_send_successful(); } return 0; } - - - void setup() { /* * things to do once at boot time */ init_serial(); - Serial.print("[Init] Free Heap ( after serial init): "); - Serial.println(ESP.getFreeHeap()); init_pins(); - Serial.print("[Init] Free Heap (after pins init): "); - Serial.println(ESP.getFreeHeap()); - init_wifi(); - Serial.print("[Init] Free Heap (after wifi init): "); - Serial.println(ESP.getFreeHeap()); - delay(500); - set_clock(); - Serial.print("[Init] Free Heap (after setting clock): "); - Serial.println(ESP.getFreeHeap()); - delay(500); } void loop() { @@ -192,11 +253,10 @@ void loop() { new_door_state = read_door_state(); if (new_door_state != current_door_state) { Serial.printf("[Loop] Status has changed to %i\n", new_door_state); - toggle_led(new_door_state); - send_status(new_door_state); - current_door_state = new_door_state; + signal_door_changed();; + if (send_status(new_door_state) == 0) { + current_door_state = new_door_state; + } } - Serial.print("[Loop] Free Heap: "); - Serial.println(ESP.getFreeHeap()); delay(FREQUENCY); }