blink-funktionen in einer funktion zusammengefasst

This commit is contained in:
example 2023-10-21 15:57:59 +02:00
parent 4f723d5751
commit f5f2efccae

View file

@ -25,6 +25,28 @@ door_state current_door_state = DOOR_CLOSED;
BearSSL::WiFiClientSecure client; BearSSL::WiFiClientSecure client;
void blink_led(int blink_count, int delay_time) {
/*
* zur ausgabe von meldungen blinkt die interne led.
* erfolgreichesmeldungen werden durch kurze blinkzeichen angezeigt,
* fehlermeldungen sind durch eine lange sequenz gekennzeichnet.
* Signale:
* status der tür hat sich geändert: 2x kurz
* änderung erfolgreich gesendet: 5x kurz
* es konnte keine wifi aufgebaut werden: 3x lang
* senden des status fehlgeschlagen: 5x lang
*
* param 1: integer
* param 2: integer
*/
for(int i=0; i!= blink_count; ++i) {
digitalWrite(LED_PIN, LOW);
delay(delay_time);
digitalWrite(LED_PIN, HIGH);
delay(delay_time);
}
}
void init_serial() { void init_serial() {
/* /*
* set baudrate and debug modus * set baudrate and debug modus
@ -66,7 +88,7 @@ void init_wifi() {
set_clock(); set_clock();
} else { } else {
Serial.println("[Wifi] Error: Failed to connect"); Serial.println("[Wifi] Error: Failed to connect");
signal_wifi_failed(); blink_led(3, 500);
} }
} }
@ -85,88 +107,6 @@ door_state read_door_state() {
return DOOR_CLOSED; return DOOR_CLOSED;
} }
void signal_door_changed() {
/*
* LED signal, if door is opened
*/
uint8_t count = 2;
for(uint8_t i=0; i!= count; ++i) {
digitalWrite(LED_PIN, LOW);
delay(100);
digitalWrite(LED_PIN, HIGH);
delay(100);
}
}
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() { void set_clock() {
/* /*
* We need time for certificate authorization * We need time for certificate authorization
@ -214,7 +154,6 @@ int send_status(door_state state) {
Serial.println("[Send] Can't connect to server"); Serial.println("[Send] Can't connect to server");
Serial.print("[Send] SSL Error: "); Serial.print("[Send] SSL Error: ");
Serial.println(client.getLastSSLError()); Serial.println(client.getLastSSLError());
signal_send_failed();
client.stop(); client.stop();
return 1; return 1;
} else { } else {
@ -223,7 +162,6 @@ int send_status(door_state state) {
Serial.println("[Send] Connection successful established"); Serial.println("[Send] Connection successful established");
Serial.printf("[Send] Send status: %s\n", status); Serial.printf("[Send] Send status: %s\n", status);
client.write(status); client.write(status);
signal_send_successful();
client.stop(); client.stop();
} }
return 0; return 0;
@ -245,15 +183,15 @@ void loop() {
/* /*
* things are running in a endless loop * things are running in a endless loop
*/ */
if (WiFi.status() != WL_CONNECTED) {
init_wifi();
}
door_state new_door_state = read_door_state(); door_state new_door_state = read_door_state();
if (new_door_state != current_door_state) { if (new_door_state != current_door_state) {
Serial.printf("[Loop] Status has changed to %i\n", new_door_state); Serial.printf("[Loop] Status has changed to %i\n", new_door_state);
signal_door_changed(); blink_led(2, 100);
if (send_status(new_door_state) == 0) { if (send_status(new_door_state) == 0) {
current_door_state = new_door_state; current_door_state = new_door_state;
blink_led(5, 100);
} else {
blink_led(5, 500);
} }
} }
delay(FREQUENCY); delay(FREQUENCY);