client portierung auf esp32-s3 hinzu
This commit is contained in:
parent
6e5dfe4e9c
commit
473c114053
4 changed files with 251 additions and 0 deletions
188
source/client/esp32-s3/statusclient/statusclient.ino
Normal file
188
source/client/esp32-s3/statusclient/statusclient.ino
Normal file
|
@ -0,0 +1,188 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
#include <time.h>
|
||||
#include <WiFi.h>
|
||||
#include <Arduino.h>
|
||||
#include <ESP_SSLClient.h>
|
||||
#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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue