forked from Krautspace/doorstatus
Refactored nodemcu/statusclient
- Fix init_wifi: removed `ESP8266WiFiMulti`; replaced with simple wifi ap connection - Fix send_status: refactored return type into `bool` - Fix send_status: optimized if-branch for calculation of `status` into array access - Fix send_status: removed `client.connected()` => `client.connect(...)` is enough - Fix: renamed `reed` into `read` - Cleanup: changed int variables into uint8_t - Cleanup: moved static data (`ca_certs`, `client_cert/key` into global data - Style: changed typedef enum into scoped enum (enum class decl) - Style: changed int const definitions into anonymous enum constants - Style: reorderd headers alphabetically
This commit is contained in:
parent
c2fd6ec7b0
commit
78e31e52a1
1 changed files with 72 additions and 82 deletions
|
@ -1,31 +1,46 @@
|
||||||
/*
|
/*
|
||||||
* file: statusclient.ino
|
* file: statusclient.ino
|
||||||
* desc: This file is part of the Krautspace Doorstatus project. It's the
|
* desc: This file is part of the Krautspace Doorstatus project. It's the
|
||||||
* main file for a client, who deals with the input from a reed sensor and
|
* main file for a client, who deals with the input from a read sensor and
|
||||||
* push these values to a server. The code is make to run on a NodeMCU with
|
* push these values to a server. The code is make to run on a NodeMCU with
|
||||||
* ESP8266 chip.
|
* ESP8266 chip.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ESP8266WiFiMulti.h>
|
|
||||||
#include <WiFiClientSecure.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiClientSecure.h>
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
#include "certs.h"
|
#include "certs.h"
|
||||||
|
#include "config.h"
|
||||||
#include "credentials.h"
|
#include "credentials.h"
|
||||||
|
|
||||||
const int LED_PIN = 16; // D0
|
// cpp23 utility
|
||||||
const int REED_PIN = 5; // D1
|
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);}
|
||||||
|
}
|
||||||
|
|
||||||
typedef enum {
|
// defining some constants
|
||||||
DOOR_CLOSED = 0,
|
enum : int {
|
||||||
DOOR_OPEN = 1
|
LED_PIN = 16, // D0
|
||||||
} door_state;
|
READ_PIN = 5 // D1
|
||||||
door_state current_door_state = DOOR_CLOSED;
|
};
|
||||||
|
|
||||||
BearSSL::WiFiClientSecure client;
|
enum class door_state {
|
||||||
|
closed = 0,
|
||||||
|
open = 1
|
||||||
|
};
|
||||||
|
|
||||||
void blink_led(int blink_count, int delay_time) {
|
// 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"}};
|
||||||
|
|
||||||
|
// 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) {
|
||||||
/*
|
/*
|
||||||
* zur ausgabe von meldungen blinkt die interne led.
|
* zur ausgabe von meldungen blinkt die interne led.
|
||||||
* erfolgreichesmeldungen werden durch kurze blinkzeichen angezeigt,
|
* erfolgreichesmeldungen werden durch kurze blinkzeichen angezeigt,
|
||||||
|
@ -39,7 +54,7 @@ void blink_led(int blink_count, int delay_time) {
|
||||||
* param 1: integer
|
* param 1: integer
|
||||||
* param 2: integer
|
* param 2: integer
|
||||||
*/
|
*/
|
||||||
for(int i=0; i!= blink_count; ++i) {
|
for (; blink_count; --blink_count) {
|
||||||
digitalWrite(LED_PIN, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
delay(delay_time);
|
delay(delay_time);
|
||||||
digitalWrite(LED_PIN, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
@ -53,58 +68,45 @@ void init_serial() {
|
||||||
*/
|
*/
|
||||||
Serial.begin(BAUD_RATE);
|
Serial.begin(BAUD_RATE);
|
||||||
Serial.setDebugOutput(DEBUG);
|
Serial.setDebugOutput(DEBUG);
|
||||||
Serial.println();
|
Serial.println("\n[Srl] Serial interface initialized");
|
||||||
Serial.println("[Srl] Serial interface initialized");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_pins() {
|
void init_pins() {
|
||||||
/*
|
/*
|
||||||
* set gpio for reed sensor and led
|
* set gpio for read sensor and led
|
||||||
*/
|
*/
|
||||||
pinMode(REED_PIN, INPUT_PULLUP);
|
pinMode(READ_PIN, INPUT_PULLUP);
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
digitalWrite(LED_PIN, HIGH);
|
digitalWrite(LED_PIN, HIGH);
|
||||||
Serial.println("[Pin] LED and REED initialized");
|
Serial.println("[Pin] LED and READ initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_wifi() {
|
void init_wifi() {
|
||||||
/*
|
WiFi.begin(SSID, PSK);
|
||||||
* Creates the ssl context. Turns wifi off and than into
|
Serial.println("[WiFi] Wifi initialized");
|
||||||
* access point mode.
|
|
||||||
* TODO: is 'turn of' needed!
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
*/
|
Serial.println("[WiFi] Error: Failed to connect");
|
||||||
ESP8266WiFiMulti wifi;
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
wifi.addAP(SSID_1, PSK_1);
|
|
||||||
wifi.addAP(SSID_2, NULL);
|
|
||||||
Serial.println("[Wifi] Wifi initialized");
|
|
||||||
wifi.run();
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
|
||||||
Serial.print("[Wif] Connected to ");
|
|
||||||
Serial.println(WiFi.SSID());
|
|
||||||
Serial.print("[Wifi] IP: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
set_clock();
|
|
||||||
} else {
|
|
||||||
Serial.println("[Wifi] Error: Failed to connect");
|
|
||||||
blink_led(3, 500);
|
blink_led(3, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serial.printf("[WiFi] Connected to %s\n", WiFi.SSID());
|
||||||
|
Serial.print("[WiFi] IP: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
set_clock();
|
||||||
}
|
}
|
||||||
|
|
||||||
door_state read_door_state() {
|
door_state read_door_state() {
|
||||||
/*
|
/*
|
||||||
* die initialisierung des reed-pin mit pullup bewirkt, daß am pin
|
* die initialisierung des read-pin mit pullup bewirkt, daß am pin
|
||||||
* 3,3 volt anliegen. die verbindung des pins mit GND sorgt dafür,
|
* 3,3 volt anliegen. die verbindung des pins mit GND sorgt dafür,
|
||||||
* daß die spannung "abfließen" kann. dadurch hat der pin dann den
|
* daß die spannung "abfließen" kann. dadurch hat der pin dann den
|
||||||
* status 'low'.
|
* status 'low'.
|
||||||
* geschlossene tür -> reed geschlossen -> low
|
* geschlossene tür -> read geschlossen -> low
|
||||||
* geöffnete tür -> reed offen -> high
|
* geöffnete tür -> read offen -> high
|
||||||
*/
|
*/
|
||||||
if (digitalRead(REED_PIN) == HIGH) {
|
return (digitalRead(READ_PIN) == HIGH) ? door_state::open : door_state::closed;
|
||||||
return DOOR_OPEN;
|
|
||||||
}
|
|
||||||
return DOOR_CLOSED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_clock() {
|
void set_clock() {
|
||||||
|
@ -115,58 +117,45 @@ void set_clock() {
|
||||||
|
|
||||||
Serial.print("[Clock] Waiting for NTP time sync");
|
Serial.print("[Clock] Waiting for NTP time sync");
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
while (now < 8 * 3600 * 2) {
|
for (; now < 16 * 3600; now = time(nullptr)) { // 16 hours
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
now = time(nullptr);
|
|
||||||
}
|
}
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
gmtime_r(&now, &timeinfo);
|
gmtime_r(&now, &timeinfo);
|
||||||
Serial.print("[Clock] Current time: ");
|
Serial.printf("[Clock] Current time: %s\n", asctime(&timeinfo));
|
||||||
Serial.println(asctime(&timeinfo));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_status(door_state state) {
|
bool send_status(door_state state) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Inits wifi (if needed) and send the status
|
* Inits wifi (if needed) and send the status
|
||||||
*/
|
*/
|
||||||
char status[2] = "";
|
char const* const status = state_str[cpp23::to_underlying(state)];
|
||||||
|
|
||||||
if (state == DOOR_CLOSED) {
|
BearSSL::WiFiClientSecure client;
|
||||||
strncpy(status, "0", 1);
|
client.setTrustAnchors(&ca_certs);
|
||||||
} else if (state == DOOR_OPEN) {
|
|
||||||
strncpy(status, "1", 1);
|
|
||||||
} else {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
client.setClientRSACert(&client_cert, &client_key);
|
||||||
|
|
||||||
Serial.println("[Ctx] SSL Context initialized");
|
Serial.println("[Ctx] SSL Context initialized");
|
||||||
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);
|
|
||||||
if (!client.connected()) {
|
if (!client.connect(SERVER_URL, SERVER_PORT)) {
|
||||||
Serial.println("[Send] Can't connect to server");
|
Serial.println("[Send] Can't connect to server");
|
||||||
Serial.print("[Send] SSL Error: ");
|
Serial.printf("[Send] SSL Error: %d\n", client.getLastSSLError());
|
||||||
Serial.println(client.getLastSSLError());
|
|
||||||
client.stop();
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
ESP.resetFreeContStack();
|
|
||||||
uint32_t freeStackStart = ESP.getFreeContStack();
|
|
||||||
Serial.println("[Send] Connection successful established");
|
|
||||||
Serial.printf("[Send] Send status: %s\n", status);
|
|
||||||
client.write(status);
|
|
||||||
client.stop();
|
client.stop();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ESP.resetFreeContStack();
|
||||||
|
// uint32_t freeStackStart = ESP.getFreeContStack();
|
||||||
|
Serial.println("[Send] Connection successful established");
|
||||||
|
Serial.printf("[Send] Send status: %s\n", status);
|
||||||
|
client.write(status);
|
||||||
|
client.stop();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
|
@ -187,12 +176,13 @@ void loop() {
|
||||||
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);
|
||||||
blink_led(2, 100);
|
blink_led(2, 100);
|
||||||
if (send_status(new_door_state) == 0) {
|
if (send_status(new_door_state)) {
|
||||||
current_door_state = new_door_state;
|
current_door_state = new_door_state;
|
||||||
blink_led(5, 100);
|
blink_led(5, 100);
|
||||||
} else {
|
} else {
|
||||||
blink_led(5, 500);
|
blink_led(5, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(FREQUENCY);
|
delay(FREQUENCY);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue