Files
smart-home/garage/garage_esp32/garage_esp32.ino
T

196 lines
5.0 KiB
Arduino

/*
* Sources:
* https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-button/
*
*/
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
#include <HTTPClient.h>
const char* ssid = "LeWe";
const char* password = "5275274365464843";
const int PIN_LED = 2;
const int PIN_GARAGE_BUTTON = 4;
const int PIN_GATE_BUTTON = 5;
const int PIN_GARAGE = 18;
const int PIN_GATE = 19;
const int PIN_DOOR_OPEN = 21;
const int PIN_DOOR_CLOSED = 22;
const int PIN_SENSOR_GATE= 23;
AsyncWebServer server(80);
const char* PARAM_MESSAGE = "message";
WiFiClient webClient;
HTTPClient http;
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void pinStatus(AsyncWebServerRequest *request) {
String status = "PIN_DOOR_OPEN: " + String(digitalRead(PIN_DOOR_OPEN)) + "\n" + "PIN_DOOR_CLOSED: " + String(digitalRead(PIN_DOOR_CLOSED));
request->send(200, "text/plain", status);
}
void mowerSafety() {
if (http.begin(webClient, "http://192.168.178.54/json?cmd=status&user=lewe&pass=nFdcX3sMD115WAap")) {
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// TODO abhängig vom Status Befehl an den Mäher schicken
Serial.println(payload);
}
}
else {
Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str());
}
http.end();
}
else {
Serial.println("Could not send HTTP request to mower");
}
}
void setup() {
// start serial output
Serial.begin(115200);
Serial.println();
btStop(); // turn off bluetooth
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_GARAGE_BUTTON, INPUT);
pinMode(PIN_GATE_BUTTON, INPUT);
pinMode(PIN_GARAGE, OUTPUT);
pinMode(PIN_GATE, OUTPUT);
pinMode(PIN_DOOR_OPEN, INPUT);
pinMode(PIN_DOOR_CLOSED, INPUT);
pinMode(PIN_SENSOR_GATE, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", "Hello World!");
});
server.on("/garage", HTTP_GET, [] (AsyncWebServerRequest *request) {
String action;
if (request->hasParam("action")) {
action = request->getParam("action")->value();
if (action.equals("open") and digitalRead(PIN_DOOR_CLOSED) and not digitalRead(PIN_DOOR_OPEN)) {
request->send(200, "application/json", "{ \"action\": \"open\" }");
}
else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) {
request->send(200, "application/json", "{ \"action\": \"close\" }");
}
else {
pinStatus(request);
}
}
else {
if (digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) {
request->send(200, "application/json", "{ \"status\": \"true\" }");
}
else {
request->send(200, "application/json", "{ \"status\": \"false\" }");
}
}
});
server.on("/gate", HTTP_GET, [] (AsyncWebServerRequest *request) {
String action;
if (request->hasParam("action")) {
action = request->getParam("action")->value();
if (action.equals("open") and not digitalRead(PIN_SENSOR_GATE)) {
request->send(200, "application/json", "{ \"action\": \"open\" }");
}
else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) {
request->send(200, "application/json", "{ \"action\": \"close\" }");
}
else {
pinStatus(request);
}
}
else {
if (digitalRead(PIN_SENSOR_GATE)) {
request->send(200, "application/json", "{ \"status\": \"false\" }");
}
else {
request->send(200, "application/json", "{ \"status\": \"true\" }");
}
}
});
server.onNotFound(notFound);
server.begin();
Serial.println("Setup completed");
}
void trigger(int pinNo) {
Serial.print("Trigger ");
Serial.println(pinNo);
digitalWrite(pinNo, HIGH);
digitalWrite(PIN_LED, HIGH);
delay(400);
digitalWrite(pinNo, LOW);
digitalWrite(PIN_LED, LOW);
}
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 80;
int lastVal = LOW;
void loop() {
int newVal = digitalRead(PIN_GARAGE_BUTTON);
if (newVal != lastVal) {
lastDebounceTime = millis();
while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
delay(1);
}
if (digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
lastVal = newVal;
if (newVal == HIGH) {
trigger(PIN_GARAGE);
}
}
}
newVal = digitalRead(PIN_GATE_BUTTON);
if (newVal != lastVal) {
lastDebounceTime = millis();
while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GATE_BUTTON) != lastVal) {
delay(1);
}
if (digitalRead(PIN_GATE_BUTTON) != lastVal) {
lastVal = newVal;
if (newVal == HIGH) {
trigger(PIN_GATE);
}
}
}
}