garage_esp32 with server and buttons

This commit is contained in:
2023-05-17 17:41:52 +02:00
parent ee5ef25761
commit c488ba1cd4
+154 -95
View File
@@ -1,17 +1,68 @@
/*
* Sources:
* https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-button/
*
*/
#include <WiFi.h> #include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
#include <HTTPClient.h> #include <HTTPClient.h>
const char* ssid = "LeWe"; const char* ssid = "LeWe";
const char* password = "5275274365464843"; const char* password = "5275274365464843";
WiFiServer server(80); const int PIN_LED = 2;
String header; 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; WiFiClient webClient;
String webhook = "http://www.google.com/";
HTTPClient http; 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() { void setup() {
// start serial output // start serial output
Serial.begin(115200); Serial.begin(115200);
@@ -19,118 +70,126 @@ void setup() {
btStop(); // turn off bluetooth btStop(); // turn off bluetooth
Serial.print("Connecting to "); pinMode(PIN_LED, OUTPUT);
Serial.println(ssid); pinMode(PIN_GARAGE_BUTTON, INPUT);
WiFi.hostname("garage-esp32"); pinMode(PIN_GATE_BUTTON, INPUT);
WiFi.begin(ssid, password); 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) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(1000);
Serial.print("."); Serial.println("Connecting to WiFi...");
} }
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true); WiFi.setAutoReconnect(true);
WiFi.persistent(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(); server.begin();
Serial.println("Setup completed"); 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() { void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { int newVal = digitalRead(PIN_GARAGE_BUTTON);
String currentLine = ""; // make a String to hold incoming data from the client if (newVal != lastVal) {
while (client.connected()) { // loop while the client's connected lastDebounceTime = millis();
if (client.available()) { // if there's bytes to read from the client, while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
char c = client.read(); // read a byte, then delay(1);
//Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type: text/html; charset=utf-8");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /bell/on") >= 0) {
Serial.println("RING");
} }
else if (header.indexOf("GET /play/") >= 0) { // yes, I know this is not RESTful if (digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
String str1 = header; lastVal = newVal;
str1 = str1.substring(header.indexOf("GET /play/") + 10); if (newVal == HIGH) {
int fileNo = str1.substring(0, str1.indexOf(" ")).toInt(); trigger(PIN_GARAGE);
Serial.print("Play file ");
Serial.println(fileNo);
} }
else if (header.indexOf("GET /restart") >= 0) {
Serial.println("Restarting...");
ESP.restart();
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<title>Doorbell</title>");
client.println("<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; }");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 { background-color: #4CAF50; border: none; color: white; padding: 4px 10px; text-decoration: none; margin: 1px; cursor: pointer;}</style></head>");
client.println("<body><h1>Doorbell</h1>");
client.println("<p><a href=\"/bell/on\"><button class=\"button\">Ring</button></a></p>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// call webhook after ringing
if (header.indexOf("GET /bell/on") >= 0) {
if (http.begin(webClient, webhook)) {
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
//Serial.println(payload);
}
}
else {
Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str());
}
// close connection
http.end();
}
else {
Serial.println("Could not send HTTP request");
} }
} }
break; newVal = digitalRead(PIN_GATE_BUTTON);
if (newVal != lastVal) {
lastDebounceTime = millis();
while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GATE_BUTTON) != lastVal) {
delay(1);
} }
else { // if you got a newline, then clear currentLine if (digitalRead(PIN_GATE_BUTTON) != lastVal) {
currentLine = ""; lastVal = newVal;
if (newVal == HIGH) {
trigger(PIN_GATE);
} }
} }
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected");
Serial.println("");
} }
} }