enum für status durch char ersetzt, senden des status in eigene funktion
This commit is contained in:
parent
473c114053
commit
b2ed249097
1 changed files with 22 additions and 28 deletions
|
@ -10,21 +10,15 @@
|
|||
#include "credentials.h"
|
||||
|
||||
|
||||
enum door_state {
|
||||
/*
|
||||
*/
|
||||
CLOSED = '0',
|
||||
OPEN = '1'
|
||||
};
|
||||
door_state new_door_state = CLOSED;
|
||||
door_state current_door_state = CLOSED;
|
||||
char new_door_state = '0';
|
||||
char current_door_state = '0';
|
||||
WiFiClient basic_client;
|
||||
ESP_SSLClient ssl_client;
|
||||
|
||||
|
||||
void display_info() {
|
||||
/*
|
||||
* ausgabe einiger infos tum device. ich verstehe allerdings die
|
||||
* ausgabe einiger infos zum device. ich verstehe allerdings die
|
||||
* for-schleife nicht.
|
||||
*/
|
||||
uint32_t chipId = 0;
|
||||
|
@ -100,7 +94,7 @@ void set_ssl() {
|
|||
|
||||
}
|
||||
|
||||
door_state read_door_state() {
|
||||
char 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,
|
||||
|
@ -111,14 +105,14 @@ door_state read_door_state() {
|
|||
* geöffnete tür -> reed offen -> high
|
||||
*/
|
||||
if (digitalRead(REED_PIN) == HIGH) {
|
||||
return OPEN;
|
||||
return '1';
|
||||
}
|
||||
return CLOSED;
|
||||
return '0';
|
||||
}
|
||||
|
||||
bool send_status(door_state state) {
|
||||
bool send_status(int state) {
|
||||
/*
|
||||
* param 1: door_state
|
||||
* param 1: integer
|
||||
* returns: boolean
|
||||
*
|
||||
* needs error handling
|
||||
|
@ -127,33 +121,31 @@ bool send_status(door_state state) {
|
|||
int counter = 0;
|
||||
|
||||
// connect to server
|
||||
Serial.print("Connecting to server ");
|
||||
Serial.print(SERVER_URL);
|
||||
Serial.print(" ... ");
|
||||
Serial.printf("Connect to server %s:%i ... ", SERVER_URL, SERVER_PORT);
|
||||
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");
|
||||
ssl_client.stop();
|
||||
return false;
|
||||
}
|
||||
Serial.println(" ok");
|
||||
} else {
|
||||
Serial.println(" failed\n");
|
||||
ssl_client.stop();
|
||||
return false;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
@ -175,7 +167,9 @@ void loop() {
|
|||
*/
|
||||
new_door_state = read_door_state();
|
||||
if (new_door_state != current_door_state) {
|
||||
Serial.print("Status has changed to ");
|
||||
Serial.print("Status has changed from ");
|
||||
Serial.print(current_door_state);
|
||||
Serial.print(" to ");
|
||||
Serial.println(new_door_state);
|
||||
if (send_status(new_door_state)) {
|
||||
current_door_state = new_door_state;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue