2022-04-06 11:37:51 +02:00
|
|
|
/*
|
|
|
|
* file: statusclient.ino
|
|
|
|
* desc: This file is part of the Krautspace Doorstatus project. It's the
|
2023-10-24 11:30:48 +02:00
|
|
|
* main file for a client, who deals with the input from a reed sensor and
|
2022-04-06 11:37:51 +02:00
|
|
|
* push these values to a server. The code is make to run on a NodeMCU with
|
|
|
|
* ESP8266 chip.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
2023-10-24 01:08:04 +02:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WiFiClientSecure.h>
|
2022-04-06 11:37:51 +02:00
|
|
|
|
|
|
|
#include "certs.h"
|
2023-10-24 01:08:04 +02:00
|
|
|
#include "config.h"
|
2022-04-06 11:37:51 +02:00
|
|
|
#include "credentials.h"
|
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
// cpp23 utility
|
|
|
|
namespace cpp23 {
|
|
|
|
template <typename Enum>
|
|
|
|
constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept { return static_cast<std::underlying_type_t<Enum>>(e);}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defining some constants
|
2023-10-24 01:33:39 +02:00
|
|
|
enum : uint8_t {
|
2023-10-24 01:08:04 +02:00
|
|
|
LED_PIN = 16, // D0
|
2024-01-20 10:03:49 +01:00
|
|
|
//REED_PIN = 5 // D1
|
|
|
|
REED_PIN = 2 // D4
|
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
};
|
2022-04-06 11:37:51 +02:00
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
enum class door_state {
|
|
|
|
closed = 0,
|
|
|
|
open = 1
|
|
|
|
};
|
2022-04-06 11:37:51 +02:00
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
// defining some globals
|
|
|
|
static inline door_state current_door_state = door_state::closed;
|
|
|
|
constexpr static inline std::array<char const[2], 2> state_str {{"0", "1"}};
|
2022-04-06 11:37:51 +02:00
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
// initializing
|
|
|
|
static inline BearSSL::X509List const ca_certs { CA_CERTS };
|
|
|
|
static inline BearSSL::X509List const client_cert { CLIENT_CERT };
|
|
|
|
static inline BearSSL::PrivateKey const client_key { CLIENT_KEY };
|
|
|
|
|
|
|
|
void blink_led(uint8_t blink_count, uint8_t delay_time) {
|
2023-10-21 15:57:59 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2023-10-24 01:08:04 +02:00
|
|
|
for (; blink_count; --blink_count) {
|
2023-10-21 15:57:59 +02:00
|
|
|
digitalWrite(LED_PIN, LOW);
|
|
|
|
delay(delay_time);
|
|
|
|
digitalWrite(LED_PIN, HIGH);
|
|
|
|
delay(delay_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 11:37:51 +02:00
|
|
|
void init_serial() {
|
|
|
|
/*
|
|
|
|
* set baudrate and debug modus
|
|
|
|
*/
|
|
|
|
Serial.begin(BAUD_RATE);
|
|
|
|
Serial.setDebugOutput(DEBUG);
|
2023-10-24 01:08:04 +02:00
|
|
|
Serial.println("\n[Srl] Serial interface initialized");
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_pins() {
|
2023-10-24 01:08:04 +02:00
|
|
|
/*
|
2023-10-24 11:30:48 +02:00
|
|
|
* set gpio for reed sensor and led
|
2022-04-06 11:37:51 +02:00
|
|
|
*/
|
2023-10-24 11:30:48 +02:00
|
|
|
pinMode(REED_PIN, INPUT_PULLUP);
|
2022-04-06 11:37:51 +02:00
|
|
|
pinMode(LED_PIN, OUTPUT);
|
2022-04-07 01:09:05 +02:00
|
|
|
digitalWrite(LED_PIN, HIGH);
|
2023-10-24 11:30:48 +02:00
|
|
|
Serial.println("[Pin] LED and REED sensor initialized");
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_wifi() {
|
2023-10-24 01:08:04 +02:00
|
|
|
WiFi.begin(SSID, PSK);
|
|
|
|
Serial.println("[WiFi] Wifi initialized");
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
Serial.println("[WiFi] Error: Failed to connect");
|
2023-10-21 15:57:59 +02:00
|
|
|
blink_led(3, 500);
|
2023-10-24 01:08:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Serial.printf("[WiFi] Connected to %s\n", WiFi.SSID());
|
|
|
|
Serial.print("[WiFi] IP: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
|
|
|
set_clock();
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
door_state read_door_state() {
|
|
|
|
/*
|
2023-10-24 11:30:48 +02:00
|
|
|
* die initialisierung des reed-switch-pins mit pullup bewirkt, dass am pin
|
2022-04-06 11:37:51 +02:00
|
|
|
* 3,3 volt anliegen. die verbindung des pins mit GND sorgt dafür,
|
2023-10-24 11:30:48 +02:00
|
|
|
* dass die spannung "abfließen" kann. dadurch hat der pin dann den
|
2022-04-06 11:37:51 +02:00
|
|
|
* status 'low'.
|
2023-10-24 11:30:48 +02:00
|
|
|
* geschlossene tür -> reed geschlossen -> low
|
|
|
|
* geöffnete tür -> reed offen -> high
|
2022-04-06 11:37:51 +02:00
|
|
|
*/
|
2023-10-24 11:30:48 +02:00
|
|
|
return (digitalRead(REED_PIN) == HIGH) ? door_state::open : door_state::closed;
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 01:09:05 +02:00
|
|
|
void set_clock() {
|
|
|
|
/*
|
|
|
|
* We need time for certificate authorization
|
|
|
|
*/
|
|
|
|
configTime(TZ_STRING, NTP_URL);
|
|
|
|
|
|
|
|
Serial.print("[Clock] Waiting for NTP time sync");
|
|
|
|
time_t now = time(nullptr);
|
2023-10-24 01:08:04 +02:00
|
|
|
for (; now < 16 * 3600; now = time(nullptr)) { // 16 hours
|
2022-04-07 01:09:05 +02:00
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&now, &timeinfo);
|
2023-10-24 01:08:04 +02:00
|
|
|
Serial.printf("[Clock] Current time: %s\n", asctime(&timeinfo));
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
bool send_status(door_state state) {
|
2022-04-06 11:37:51 +02:00
|
|
|
/*
|
2022-04-07 01:09:05 +02:00
|
|
|
* Inits wifi (if needed) and send the status
|
2022-04-06 11:37:51 +02:00
|
|
|
*/
|
2023-10-24 01:08:04 +02:00
|
|
|
char const* const status = state_str[cpp23::to_underlying(state)];
|
2022-04-06 11:37:51 +02:00
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
BearSSL::WiFiClientSecure client;
|
|
|
|
client.setTrustAnchors(&ca_certs);
|
2022-04-06 11:37:51 +02:00
|
|
|
client.setClientRSACert(&client_cert, &client_key);
|
2023-10-24 01:08:04 +02:00
|
|
|
|
2022-04-06 11:37:51 +02:00
|
|
|
Serial.println("[Ctx] SSL Context initialized");
|
|
|
|
Serial.printf("[Send] Connect to %s:%i\n", SERVER_URL, SERVER_PORT);
|
2023-10-24 01:08:04 +02:00
|
|
|
|
|
|
|
if (!client.connect(SERVER_URL, SERVER_PORT)) {
|
2022-04-06 11:37:51 +02:00
|
|
|
Serial.println("[Send] Can't connect to server");
|
2023-10-24 01:08:04 +02:00
|
|
|
Serial.printf("[Send] SSL Error: %d\n", client.getLastSSLError());
|
2022-04-07 23:05:46 +02:00
|
|
|
client.stop();
|
2023-10-24 01:08:04 +02:00
|
|
|
return false;
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 01:08:04 +02:00
|
|
|
Serial.println("[Send] Connection successful established");
|
|
|
|
Serial.printf("[Send] Send status: %s\n", status);
|
|
|
|
client.write(status);
|
|
|
|
client.stop();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-06 11:37:51 +02:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* things to do once at boot time
|
|
|
|
*/
|
|
|
|
init_serial();
|
|
|
|
init_pins();
|
2022-04-07 23:05:46 +02:00
|
|
|
init_wifi();
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* things are running in a endless loop
|
|
|
|
*/
|
2022-04-07 23:05:46 +02:00
|
|
|
door_state new_door_state = read_door_state();
|
2022-04-06 11:37:51 +02:00
|
|
|
if (new_door_state != current_door_state) {
|
|
|
|
Serial.printf("[Loop] Status has changed to %i\n", new_door_state);
|
2023-10-21 15:57:59 +02:00
|
|
|
blink_led(2, 100);
|
2023-10-24 01:08:04 +02:00
|
|
|
if (send_status(new_door_state)) {
|
2022-04-07 01:09:05 +02:00
|
|
|
current_door_state = new_door_state;
|
2023-10-21 15:57:59 +02:00
|
|
|
blink_led(5, 100);
|
|
|
|
} else {
|
|
|
|
blink_led(5, 500);
|
2022-04-07 01:09:05 +02:00
|
|
|
}
|
2022-04-06 11:37:51 +02:00
|
|
|
}
|
2023-10-24 01:08:04 +02:00
|
|
|
|
2022-04-06 11:37:51 +02:00
|
|
|
delay(FREQUENCY);
|
|
|
|
}
|