From b2ed249097006c892c0aa0b01bebf384bbff32d9 Mon Sep 17 00:00:00 2001 From: bernd Date: Fri, 13 Jun 2025 17:47:48 +0200 Subject: [PATCH] =?UTF-8?q?enum=20f=C3=BCr=20status=20durch=20char=20erset?= =?UTF-8?q?zt,=20senden=20des=20status=20in=20eigene=20funktion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../esp32-s3/statusclient/statusclient.ino | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/source/client/esp32-s3/statusclient/statusclient.ino b/source/client/esp32-s3/statusclient/statusclient.ino index a6b8bb0..af25347 100644 --- a/source/client/esp32-s3/statusclient/statusclient.ino +++ b/source/client/esp32-s3/statusclient/statusclient.ino @@ -10,21 +10,15 @@ #include "credentials.h" -enum door_state { - /* - */ - CLOSED = '0', - OPEN = '1' -}; -door_state new_door_state = CLOSED; -door_state current_door_state = CLOSED; +char new_door_state = '0'; +char current_door_state = '0'; WiFiClient basic_client; ESP_SSLClient ssl_client; void display_info() { /* - * ausgabe einiger infos tum device. ich verstehe allerdings die + * ausgabe einiger infos zum device. ich verstehe allerdings die * for-schleife nicht. */ uint32_t chipId = 0; @@ -100,7 +94,7 @@ void set_ssl() { } -door_state read_door_state() { +char read_door_state() { /* * die initialisierung des reed-switch-pins mit pullup bewirkt, dass am pin * 3,3 volt anliegen. die verbindung des pins mit GND sorgt dafür, @@ -111,14 +105,14 @@ door_state read_door_state() { * geöffnete tür -> reed offen -> high */ if (digitalRead(REED_PIN) == HIGH) { - return OPEN; + return '1'; } - return CLOSED; + return '0'; } -bool send_status(door_state state) { +bool send_status(int state) { /* - * param 1: door_state + * param 1: integer * returns: boolean * * needs error handling @@ -127,33 +121,31 @@ bool send_status(door_state state) { int counter = 0; // connect to server - Serial.print("Connecting to server "); - Serial.print(SERVER_URL); - Serial.print(" ... "); + Serial.printf("Connect to server %s:%i ... ", SERVER_URL, SERVER_PORT); if (ssl_client.connect(SERVER_URL, SERVER_PORT)) { Serial.println(" ok"); Serial.print("Upgrade to HTTPS..."); if (!ssl_client.connectSSL()) { Serial.println(" failed\r\n"); + ssl_client.stop(); return false; } Serial.println(" ok"); - // send new status - Serial.print("Send new status ... "); - response = ssl_client.write(current_door_state); - ssl_client.flush(); - Serial.print("Bytes written: "); - Serial.println(response); - - Serial.print("Read response..."); - Serial.print((char)ssl_client.read()); - Serial.println(); } else { Serial.println(" failed\n"); ssl_client.stop(); return false; } + // send new status + Serial.print("Send new status ... "); + response = ssl_client.write(current_door_state); + ssl_client.flush(); + Serial.print("Bytes written: "); + Serial.println(response); + Serial.print("Read response..."); + Serial.print((char)ssl_client.read()); + Serial.println(); ssl_client.stop(); return true; } @@ -175,7 +167,9 @@ void loop() { */ new_door_state = read_door_state(); if (new_door_state != current_door_state) { - Serial.print("Status has changed to "); + Serial.print("Status has changed from "); + Serial.print(current_door_state); + Serial.print(" to "); Serial.println(new_door_state); if (send_status(new_door_state)) { current_door_state = new_door_state;