struktureller umbau

ClientSecure instanz jetzt global,
init_wifi() ins setup verlagert,
prüfung auf wifi in jedem loop,
client.stop() auch bei erfolg
This commit is contained in:
+++ 2022-04-07 23:05:46 +02:00
parent 8f4f6d82d4
commit a93bb9ea0a

View file

@ -22,8 +22,8 @@ typedef enum {
DOOR_OPEN = 1
} door_state;
door_state current_door_state = DOOR_CLOSED;
door_state new_door_state = DOOR_CLOSED;
BearSSL::WiFiClientSecure client;
void init_serial() {
/*
@ -45,7 +45,6 @@ void init_pins() {
Serial.println("[Pin] LED and REED initialized");
}
void init_wifi() {
/*
* Creates the ssl context. Turns wifi off and than into
@ -203,18 +202,12 @@ int send_status(door_state state) {
return 1;
}
BearSSL::WiFiClientSecure client;
BearSSL::X509List server_cert(SERVER_CERT);
BearSSL::X509List client_cert(CLIENT_CERT);
BearSSL::PrivateKey client_key(CLIENT_KEY);
client.setTrustAnchors(&server_cert);
client.setClientRSACert(&client_cert, &client_key);
delay(200);
Serial.println("[Ctx] SSL Context initialized");
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()) {
@ -231,6 +224,7 @@ int send_status(door_state state) {
Serial.printf("[Send] Send status: %s\n", status);
client.write(status);
signal_send_successful();
client.stop();
}
return 0;
}
@ -243,6 +237,7 @@ void setup() {
*/
init_serial();
init_pins();
init_wifi();
}
void loop() {
@ -250,10 +245,13 @@ void loop() {
/*
* things are running in a endless loop
*/
new_door_state = read_door_state();
if (WiFi.status() != WL_CONNECTED) {
init_wifi();
}
door_state 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);
signal_door_changed();;
signal_door_changed();
if (send_status(new_door_state) == 0) {
current_door_state = new_door_state;
}