enum für status durch char ersetzt, senden des status in eigene funktion

This commit is contained in:
bernd 2025-06-13 17:47:48 +02:00
parent 473c114053
commit b2ed249097

View file

@ -10,21 +10,15 @@
#include "credentials.h" #include "credentials.h"
enum door_state { char new_door_state = '0';
/* char current_door_state = '0';
*/
CLOSED = '0',
OPEN = '1'
};
door_state new_door_state = CLOSED;
door_state current_door_state = CLOSED;
WiFiClient basic_client; WiFiClient basic_client;
ESP_SSLClient ssl_client; ESP_SSLClient ssl_client;
void display_info() { void display_info() {
/* /*
* ausgabe einiger infos tum device. ich verstehe allerdings die * ausgabe einiger infos zum device. ich verstehe allerdings die
* for-schleife nicht. * for-schleife nicht.
*/ */
uint32_t chipId = 0; 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 * 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, * 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 * geöffnete tür -> reed offen -> high
*/ */
if (digitalRead(REED_PIN) == 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 * returns: boolean
* *
* needs error handling * needs error handling
@ -127,33 +121,31 @@ bool send_status(door_state state) {
int counter = 0; int counter = 0;
// connect to server // connect to server
Serial.print("Connecting to server "); Serial.printf("Connect to server %s:%i ... ", SERVER_URL, SERVER_PORT);
Serial.print(SERVER_URL);
Serial.print(" ... ");
if (ssl_client.connect(SERVER_URL, SERVER_PORT)) { if (ssl_client.connect(SERVER_URL, SERVER_PORT)) {
Serial.println(" ok"); Serial.println(" ok");
Serial.print("Upgrade to HTTPS..."); Serial.print("Upgrade to HTTPS...");
if (!ssl_client.connectSSL()) if (!ssl_client.connectSSL())
{ {
Serial.println(" failed\r\n"); Serial.println(" failed\r\n");
ssl_client.stop();
return false; return false;
} }
Serial.println(" ok"); Serial.println(" ok");
} else {
Serial.println(" failed\n");
ssl_client.stop();
return false;
}
// send new status // send new status
Serial.print("Send new status ... "); Serial.print("Send new status ... ");
response = ssl_client.write(current_door_state); response = ssl_client.write(current_door_state);
ssl_client.flush(); ssl_client.flush();
Serial.print("Bytes written: "); Serial.print("Bytes written: ");
Serial.println(response); Serial.println(response);
Serial.print("Read response..."); Serial.print("Read response...");
Serial.print((char)ssl_client.read()); Serial.print((char)ssl_client.read());
Serial.println(); Serial.println();
} else {
Serial.println(" failed\n");
ssl_client.stop();
return false;
}
ssl_client.stop(); ssl_client.stop();
return true; return true;
} }
@ -175,7 +167,9 @@ void loop() {
*/ */
new_door_state = read_door_state(); new_door_state = read_door_state();
if (new_door_state != current_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); Serial.println(new_door_state);
if (send_status(new_door_state)) { if (send_status(new_door_state)) {
current_door_state = new_door_state; current_door_state = new_door_state;