diff --git a/source/client/esp32-s3/statusclient/certs.template b/source/client/esp32-s3/statusclient/certs.template new file mode 100644 index 0000000..df8b415 --- /dev/null +++ b/source/client/esp32-s3/statusclient/certs.template @@ -0,0 +1,27 @@ +/* + * file: certs.template + * desc: This file is part of the Krautspace Doorstatus project. It contains + * certificates for the statusclient.ino programm, that runs on an esp32-s3 + * board (esp32-1732s019). + * + * Replace the comments in certificate sections with our own certificates. + */ + +constexpr static inline char const CA_CERTS[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +place here your servers public cert +-----END CERTIFICATE----- +)EOF"; + +constexpr static inline char const CLIENT_CERT[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +place here your clients public dert +-----END CERTIFICATE----- +)EOF"; + +constexpr static inline char const CLIENT_KEY[] PROGMEM = R"EOF( +-----BEGIN RSA PRIVATE KEY----- +place here your clients private key +-----END RSA PRIVATE KEY----- +)EOF"; + diff --git a/source/client/esp32-s3/statusclient/config.template b/source/client/esp32-s3/statusclient/config.template new file mode 100644 index 0000000..e2a3bc3 --- /dev/null +++ b/source/client/esp32-s3/statusclient/config.template @@ -0,0 +1,33 @@ +/* + * file: config.h + */ + +/* endpoint */ +const char* SERVER_URL = "your status server url"; +const uint16_t SERVER_PORT = port; + +/* ssl debud level + * esp_ssl_debug_none = 0 + * esp_ssl_debug_error = 1 + * esp_ssl_debug_warn = 2 + * esp_ssl_debug_info = 3 + * esp_ssl_debug_dump = 4 + */ +const uint8_t SSL_DEBUG_LEVEL = 3; + +/* ssl socket */ +const int32_t TIMEOUT = 20; +const int32_t SESSION_TIMEOUT = 6000; + +/* serial interface settings */ +const unsigned long BAUD_RATE = 115200; + +/* pin and frequence to read the pin */ +int REED_PIN = 47; +const unsigned long FREQUENCY = 3000; + +/* time server settings */ +const char* NTP_URL = "pool.ntp.org"; +const int GTM_OFFSET_SEC = 3600; +const unsigned long DAYLIGHT_OFFSET_SEC = 3600; + diff --git a/source/client/esp32-s3/statusclient/credentials.template b/source/client/esp32-s3/statusclient/credentials.template new file mode 100644 index 0000000..e419d7c --- /dev/null +++ b/source/client/esp32-s3/statusclient/credentials.template @@ -0,0 +1,3 @@ +const char *WIFI_SSID = "ssid meines wifi"; +const char *WIFI_PASSWORD = "passwort meines wifi"; + diff --git a/source/client/esp32-s3/statusclient/statusclient.ino b/source/client/esp32-s3/statusclient/statusclient.ino new file mode 100644 index 0000000..a6b8bb0 --- /dev/null +++ b/source/client/esp32-s3/statusclient/statusclient.ino @@ -0,0 +1,188 @@ +/** + * + */ +#include +#include +#include +#include +#include "config.h" +#include "certs.h" +#include "credentials.h" + + +enum door_state { + /* + */ + CLOSED = '0', + OPEN = '1' +}; +door_state new_door_state = CLOSED; +door_state current_door_state = CLOSED; +WiFiClient basic_client; +ESP_SSLClient ssl_client; + + +void display_info() { + /* + * ausgabe einiger infos tum device. ich verstehe allerdings die + * for-schleife nicht. + */ + uint32_t chipId = 0; + + for (int i = 0; i < 17; i = i + 8) { + chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; + } + Serial.printf("\nESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), + ESP.getChipRevision()); + Serial.printf("This chip has %d cores\n", ESP.getChipCores()); + Serial.print("Chip ID: "); + Serial.println(chipId); + Serial.print(" "); +} + +void init_wifi() { + /* + */ + int counter = 0; + Serial.print("Connecting to wifi "); + Serial.print(WIFI_SSID); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + counter++; + if (counter >= 180) { + Serial.println(" Failed"); + ESP.restart(); + } + } + Serial.println(" Connected"); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); + Serial.print("Strength: "); + Serial.println(WiFi.RSSI()); +} + +void set_clock() { + /* + * We need time for certificate authorization + */ + Serial.print("Fetch time from "); + Serial.println(NTP_URL); + configTime(GTM_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_URL); + printLocalTime(); +} + +void printLocalTime() { + /* + */ + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) { + Serial.println("Failed to obtain time"); + return; + } + Serial.printf("%s\n", asctime(&timeinfo)); +} + +void set_ssl() { + /* + */ + // ssl_client.setTimeout(120); + // ssl_client.setSessionTimeout(0); + ssl_client.setDebugLevel(SSL_DEBUG_LEVEL); + ssl_client.setClient(&basic_client, false); + ssl_client.setSSLVersion(BR_TLS12, BR_TLS12); + ssl_client.setCACert(CA_CERTS); + ssl_client.setCertificate(CLIENT_CERT); + ssl_client.setPrivateKey(CLIENT_KEY); + +} + +door_state read_door_state() { + /* + * 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, + * dass die spannung "abfließen" kann. dadurch hat der pin dann den + * status 'low'. die option geht leider nur mit der bibliothek für den + * esp8862. + * geschlossene tür -> reed geschlossen -> low + * geöffnete tür -> reed offen -> high + */ + if (digitalRead(REED_PIN) == HIGH) { + return OPEN; + } + return CLOSED; +} + +bool send_status(door_state state) { + /* + * param 1: door_state + * returns: boolean + * + * needs error handling + */ + int32_t response = 0; + int counter = 0; + + // connect to server + Serial.print("Connecting to server "); + Serial.print(SERVER_URL); + Serial.print(" ... "); + if (ssl_client.connect(SERVER_URL, SERVER_PORT)) { + Serial.println(" ok"); + Serial.print("Upgrade to HTTPS..."); + if (!ssl_client.connectSSL()) + { + Serial.println(" failed\r\n"); + return false; + } + Serial.println(" ok"); + // send new status + Serial.print("Send new status ... "); + response = ssl_client.write(current_door_state); + ssl_client.flush(); + Serial.print("Bytes written: "); + Serial.println(response); + + Serial.print("Read response..."); + Serial.print((char)ssl_client.read()); + Serial.println(); + } else { + Serial.println(" failed\n"); + ssl_client.stop(); + return false; + } + ssl_client.stop(); + return true; +} + +void setup() { + /* + */ + Serial.begin(115200); + Serial.println("------------- setup -------------"); + display_info(); + init_wifi(); + set_clock(); + set_ssl(); + Serial.println("-------- setup finished ---------"); +} + +void loop() { + /* + */ + new_door_state = read_door_state(); + if (new_door_state != current_door_state) { + Serial.print("Status has changed to "); + Serial.println(new_door_state); + if (send_status(new_door_state)) { + current_door_state = new_door_state; + Serial.println("Door status send successful"); + } else { + Serial.println("Failed to send new door status"); + } + } + delay(FREQUENCY); +}