forked from Krautspace/doorstatus
kommentare eingefügt, led blinkt fehlercodes
This commit is contained in:
parent
a1d14235ff
commit
8f4f6d82d4
1 changed files with 122 additions and 62 deletions
|
@ -18,9 +18,9 @@ const int LED_PIN = 16; // D0
|
||||||
const int REED_PIN = 5; // D1
|
const int REED_PIN = 5; // D1
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
DOOR_CLOSED = 0,
|
DOOR_CLOSED = 0,
|
||||||
DOOR_OPEN = 1
|
DOOR_OPEN = 1
|
||||||
} door_state;
|
} door_state;
|
||||||
door_state current_door_state = DOOR_CLOSED;
|
door_state current_door_state = DOOR_CLOSED;
|
||||||
door_state new_door_state = DOOR_CLOSED;
|
door_state new_door_state = DOOR_CLOSED;
|
||||||
|
|
||||||
|
@ -41,30 +41,33 @@ void init_pins() {
|
||||||
*/
|
*/
|
||||||
pinMode(REED_PIN, INPUT_PULLUP);
|
pinMode(REED_PIN, INPUT_PULLUP);
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
digitalWrite(LED_PIN, LOW);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
Serial.println("[Pin] LED and REED initialized");
|
Serial.println("[Pin] LED and REED initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void init_wifi() {
|
void init_wifi() {
|
||||||
/*
|
/*
|
||||||
* first turn wifi off and than in access point mode
|
* Creates the ssl context. Turns wifi off and than into
|
||||||
* maybe turn of is not needed!
|
* access point mode.
|
||||||
*/
|
* TODO: is 'turn of' needed!
|
||||||
|
*/
|
||||||
ESP8266WiFiMulti wifi;
|
ESP8266WiFiMulti wifi;
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
wifi.addAP(SSID_1, PSK_1);
|
wifi.addAP(SSID_1, PSK_1);
|
||||||
wifi.addAP(SSID_2, PSK_2);
|
wifi.addAP(SSID_2, NULL);
|
||||||
Serial.println("[Wifi] Wifi initialized");
|
Serial.println("[Wifi] Wifi initialized");
|
||||||
wifi.run();
|
wifi.run();
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Serial.print("[Wif] Connected to ");
|
Serial.print("[Wif] Connected to ");
|
||||||
Serial.println(WiFi.SSID());
|
Serial.println(WiFi.SSID());
|
||||||
Serial.print("[Wifi] IP: ");
|
Serial.print("[Wifi] IP: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
set_clock();
|
||||||
} else {
|
} 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;
|
return DOOR_CLOSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggle_led(door_state state) {
|
void signal_door_changed() {
|
||||||
/*
|
/*
|
||||||
* turns onboard led on or depends on the door state
|
* 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);
|
digitalWrite(LED_PIN, LOW);
|
||||||
} else {
|
delay(100);
|
||||||
digitalWrite(LED_PIN, HIGH);
|
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() {
|
void set_clock() {
|
||||||
|
/*
|
||||||
|
* We need time for certificate authorization
|
||||||
|
*/
|
||||||
|
configTime(TZ_STRING, NTP_URL);
|
||||||
|
|
||||||
configTime(TZ_STRING, NTP_URL);
|
Serial.print("[Clock] Waiting for NTP time sync");
|
||||||
|
time_t now = time(nullptr);
|
||||||
Serial.print("Waiting for NTP time sync: ");
|
while (now < 8 * 3600 * 2) {
|
||||||
time_t now = time(nullptr);
|
delay(500);
|
||||||
while (now < 8 * 3600 * 2) {
|
Serial.print(".");
|
||||||
delay(500);
|
now = time(nullptr);
|
||||||
Serial.print(".");
|
}
|
||||||
now = time(nullptr);
|
Serial.println("");
|
||||||
}
|
struct tm timeinfo;
|
||||||
Serial.println("");
|
gmtime_r(&now, &timeinfo);
|
||||||
struct tm timeinfo;
|
Serial.print("[Clock] Current time: ");
|
||||||
gmtime_r(&now, &timeinfo);
|
Serial.println(asctime(&timeinfo));
|
||||||
Serial.print("Current time: ");
|
|
||||||
Serial.print(asctime(&timeinfo));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_status(door_state state) {
|
int send_status(door_state state) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* geht die initialisierung mit einem byte länge?
|
* Inits wifi (if needed) and send the status
|
||||||
* terminiert strcpy den status mit \0?
|
|
||||||
*/
|
*/
|
||||||
char status[2] = "";
|
char status[2] = "";
|
||||||
|
|
||||||
|
@ -135,17 +209,19 @@ int send_status(door_state state) {
|
||||||
BearSSL::PrivateKey client_key(CLIENT_KEY);
|
BearSSL::PrivateKey client_key(CLIENT_KEY);
|
||||||
client.setTrustAnchors(&server_cert);
|
client.setTrustAnchors(&server_cert);
|
||||||
client.setClientRSACert(&client_cert, &client_key);
|
client.setClientRSACert(&client_cert, &client_key);
|
||||||
delay(500);
|
delay(200);
|
||||||
Serial.println("[Ctx] SSL Context initialized");
|
Serial.println("[Ctx] SSL Context initialized");
|
||||||
Serial.print("[Ctx] Free Heap: ");
|
delay(200);
|
||||||
Serial.println(ESP.getFreeHeap());
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
init_wifi();
|
||||||
|
}
|
||||||
Serial.printf("[Send] Connect to %s:%i\n", SERVER_URL, SERVER_PORT);
|
Serial.printf("[Send] Connect to %s:%i\n", SERVER_URL, SERVER_PORT);
|
||||||
client.connect(SERVER_URL, SERVER_PORT);
|
client.connect(SERVER_URL, SERVER_PORT);
|
||||||
if (!client.connected()) {
|
if (!client.connected()) {
|
||||||
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 {
|
||||||
|
@ -154,34 +230,19 @@ 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();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* things to do once at boot time
|
* things to do once at boot time
|
||||||
*/
|
*/
|
||||||
init_serial();
|
init_serial();
|
||||||
Serial.print("[Init] Free Heap ( after serial init): ");
|
|
||||||
Serial.println(ESP.getFreeHeap());
|
|
||||||
init_pins();
|
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() {
|
void loop() {
|
||||||
|
@ -192,11 +253,10 @@ 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.printf("[Loop] Status has changed to %i\n", new_door_state);
|
Serial.printf("[Loop] Status has changed to %i\n", new_door_state);
|
||||||
toggle_led(new_door_state);
|
signal_door_changed();;
|
||||||
send_status(new_door_state);
|
if (send_status(new_door_state) == 0) {
|
||||||
current_door_state = new_door_state;
|
current_door_state = new_door_state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Serial.print("[Loop] Free Heap: ");
|
|
||||||
Serial.println(ESP.getFreeHeap());
|
|
||||||
delay(FREQUENCY);
|
delay(FREQUENCY);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue