esp32 garage with sprinklers
This commit is contained in:
@@ -14,15 +14,19 @@
|
|||||||
const char* ssid = "LeWe";
|
const char* ssid = "LeWe";
|
||||||
const char* password = "5275274365464843";
|
const char* password = "5275274365464843";
|
||||||
|
|
||||||
const int PIN_LED = 2;
|
const int PIN_LED = 2; // esp32 dev board integrated LED
|
||||||
const int PIN_GARAGE_BUTTON = 4;
|
const int PIN_GARAGE_BUTTON = 4; // button to open/close the garage door
|
||||||
const int PIN_GATE_BUTTON = 5;
|
const int PIN_GATE_BUTTON = 5; // button to open/close the gate
|
||||||
const int PIN_GARAGE = 18;
|
const int PIN_MOWER_ACTIVE = 15; // only contact mower if pin is HIGH
|
||||||
const int PIN_GATE = 19;
|
const int PIN_GARAGE = 18; // relais pin for garage door
|
||||||
const int PIN_DOOR_OPEN = 21;
|
const int PIN_GATE = 19; // relais pin for gate
|
||||||
const int PIN_DOOR_CLOSED = 22;
|
const int PIN_DOOR_OPEN = 21; // sensor pin for garage door in position 'open'
|
||||||
const int PIN_SENSOR_GATE= 23;
|
const int PIN_DOOR_CLOSED = 22; // sensor pin for garage door in position 'closed'
|
||||||
|
const int PIN_SENSOR_GATE= 23; // sensor pin for gate (HIGH is open)
|
||||||
|
const int PIN_SPRINKLER1 = 25; // relais pin for sprinkler 1
|
||||||
|
const int PIN_SPRINKLER2 = 26; // relais pin for sprinkler 2
|
||||||
|
const int PIN_SPRINKLER3 = 27; // relais pin for sprinkler 3
|
||||||
|
const int PIN_SPRINKLER4 = 14; // relais pin for sprinkler 4
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
const char* PARAM_MESSAGE = "message";
|
const char* PARAM_MESSAGE = "message";
|
||||||
@@ -30,6 +34,8 @@ const char* PARAM_MESSAGE = "message";
|
|||||||
WiFiClient webClient;
|
WiFiClient webClient;
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
|
int sprinklerDuration = 0;
|
||||||
|
unsigned long sprinklerStartMillis;
|
||||||
|
|
||||||
|
|
||||||
void notFound(AsyncWebServerRequest *request) {
|
void notFound(AsyncWebServerRequest *request) {
|
||||||
@@ -37,20 +43,58 @@ void notFound(AsyncWebServerRequest *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pinStatus(AsyncWebServerRequest *request) {
|
void pinStatus(AsyncWebServerRequest *request) {
|
||||||
String status = "PIN_DOOR_OPEN: " + String(digitalRead(PIN_DOOR_OPEN)) + "\n" + "PIN_DOOR_CLOSED: " + String(digitalRead(PIN_DOOR_CLOSED));
|
String status = "esp32garage\n\nPIN_LED (" + String(PIN_LED) + ") = " + String(digitalRead(PIN_LED)) + "\n" +
|
||||||
|
"PIN_GARAGE_BUTTON (" + String(PIN_GARAGE_BUTTON) + ") = " + String(digitalRead(PIN_GARAGE_BUTTON)) + "\n" +
|
||||||
|
"PIN_GATE_BUTTON (" + String(PIN_GATE_BUTTON) + ") = " + String(digitalRead(PIN_GATE_BUTTON)) + "\n" +
|
||||||
|
"PIN_MOWER_ACTIVE (" + String(PIN_MOWER_ACTIVE) + ") = " + String(digitalRead(PIN_MOWER_ACTIVE)) + "\n" +
|
||||||
|
"PIN_GARAGE (" + String(PIN_GARAGE) + ") = " + String(digitalRead(PIN_GARAGE)) + "\n" +
|
||||||
|
"PIN_GATE (" + String(PIN_GATE) + ") = " + String(digitalRead(PIN_GATE)) + "\n" +
|
||||||
|
"PIN_DOOR_OPEN (" + String(PIN_DOOR_OPEN) + ") = " + String(digitalRead(PIN_DOOR_OPEN)) + "\n" +
|
||||||
|
"PIN_DOOR_CLOSED (" + String(PIN_DOOR_CLOSED) + ") = " + String(digitalRead(PIN_DOOR_CLOSED)) + "\n" +
|
||||||
|
"PIN_SENSOR_GATE (" + String(PIN_SENSOR_GATE) + ") = " + String(digitalRead(PIN_SENSOR_GATE)) + "\n" +
|
||||||
|
"PIN_SPRINKLER1 (" + String(PIN_SPRINKLER1) + ") = " + String(digitalRead(PIN_SPRINKLER1)) + "\n" +
|
||||||
|
"PIN_SPRINKLER2 (" + String(PIN_SPRINKLER2) + ") = " + String(digitalRead(PIN_SPRINKLER2)) + "\n" +
|
||||||
|
"PIN_SPRINKLER3 (" + String(PIN_SPRINKLER3) + ") = " + String(digitalRead(PIN_SPRINKLER3)) + "\n" +
|
||||||
|
"PIN_SPRINKLER4 (" + String(PIN_SPRINKLER4) + ") = " + String(digitalRead(PIN_SPRINKLER4)) + "\n\n" +
|
||||||
|
"/ \n" +
|
||||||
|
"/garage[?action=(open|close)] \n" +
|
||||||
|
"/gate[?action=(open|close)] \n" +
|
||||||
|
"/sprinkler[?action=(stop|(start&device={int}&duration={int}))]";
|
||||||
|
|
||||||
request->send(200, "text/plain", status);
|
request->send(200, "text/plain", status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mowerSafety() {
|
void mowerSafety() {
|
||||||
|
Serial.println("mowerSafety()");
|
||||||
|
|
||||||
if (http.begin(webClient, "http://192.168.178.54/json?cmd=status&user=lewe&pass=nFdcX3sMD115WAap")) {
|
if (http.begin(webClient, "http://192.168.178.54/json?cmd=status&user=lewe&pass=nFdcX3sMD115WAap")) {
|
||||||
int httpCode = http.GET();
|
int httpCode = http.GET();
|
||||||
|
|
||||||
if (httpCode > 0) {
|
if (httpCode > 0) {
|
||||||
if (httpCode == HTTP_CODE_OK) {
|
if (httpCode == HTTP_CODE_OK) {
|
||||||
String payload = http.getString();
|
String payload = http.getString();
|
||||||
|
String statusStr = payload.substring(payload.indexOf("\"status\": {\"status\": ") + 21);
|
||||||
|
statusStr = statusStr.substring(0, statusStr.indexOf(","));
|
||||||
|
|
||||||
// TODO abhängig vom Status Befehl an den Mäher schicken
|
Serial.println(statusStr);
|
||||||
Serial.println(payload);
|
|
||||||
|
if (statusStr.equals("2") or statusStr.equals("3") or statusStr.equals("5") or statusStr.equals("17")) {
|
||||||
|
Serial.println("stop mower");
|
||||||
|
if (http.begin(webClient, "http://192.168.178.54/json?cmd=stop&user=lewe&pass=nFdcX3sMD115WAap")) {
|
||||||
|
int httpCode = http.GET();
|
||||||
|
|
||||||
|
if (httpCode < 0) {
|
||||||
|
Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str());
|
||||||
|
}
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Could not send HTTP request (stop) to mower");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("no status to be stopped");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -63,6 +107,67 @@ void mowerSafety() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mowerAuto() {
|
||||||
|
Serial.println("mowerAuto()");
|
||||||
|
|
||||||
|
if (http.begin(webClient, "http://192.168.178.54/json?cmd=mode&mode=auto&user=lewe&pass=nFdcX3sMD115WAap")) {
|
||||||
|
int httpCode = http.GET();
|
||||||
|
|
||||||
|
if (httpCode < 0) {
|
||||||
|
Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str());
|
||||||
|
}
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Could not send HTTP request (auto mode) to mower");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerGarage() {
|
||||||
|
Serial.println("Trigger garage door");
|
||||||
|
|
||||||
|
digitalWrite(PIN_GARAGE, HIGH);
|
||||||
|
digitalWrite(PIN_LED, HIGH);
|
||||||
|
delay(400);
|
||||||
|
digitalWrite(PIN_GARAGE, LOW);
|
||||||
|
digitalWrite(PIN_LED, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerGate() {
|
||||||
|
Serial.println("Trigger gate");
|
||||||
|
|
||||||
|
bool toSafety = false;
|
||||||
|
if (digitalRead(PIN_SENSOR_GATE)) {
|
||||||
|
toSafety = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
digitalWrite(PIN_GATE, HIGH);
|
||||||
|
digitalWrite(PIN_LED, HIGH);
|
||||||
|
delay(400);
|
||||||
|
digitalWrite(PIN_GATE, LOW);
|
||||||
|
digitalWrite(PIN_LED, LOW);
|
||||||
|
|
||||||
|
// DEAKTIVIEREN FÜR WINTERPAUSE:
|
||||||
|
if (digitalRead(PIN_MOWER_ACTIVE)) {
|
||||||
|
if (toSafety) {
|
||||||
|
mowerSafety();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mowerAuto();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopSprinklers() {
|
||||||
|
Serial.println("Stopping sprinklers");
|
||||||
|
|
||||||
|
digitalWrite(PIN_SPRINKLER1, LOW);
|
||||||
|
digitalWrite(PIN_SPRINKLER2, LOW);
|
||||||
|
digitalWrite(PIN_SPRINKLER3, LOW);
|
||||||
|
digitalWrite(PIN_SPRINKLER4, LOW);
|
||||||
|
sprinklerDuration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial output
|
// start serial output
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -73,11 +178,16 @@ void setup() {
|
|||||||
pinMode(PIN_LED, OUTPUT);
|
pinMode(PIN_LED, OUTPUT);
|
||||||
pinMode(PIN_GARAGE_BUTTON, INPUT);
|
pinMode(PIN_GARAGE_BUTTON, INPUT);
|
||||||
pinMode(PIN_GATE_BUTTON, INPUT);
|
pinMode(PIN_GATE_BUTTON, INPUT);
|
||||||
|
pinMode(PIN_MOWER_ACTIVE, INPUT);
|
||||||
pinMode(PIN_GARAGE, OUTPUT);
|
pinMode(PIN_GARAGE, OUTPUT);
|
||||||
pinMode(PIN_GATE, OUTPUT);
|
pinMode(PIN_GATE, OUTPUT);
|
||||||
pinMode(PIN_DOOR_OPEN, INPUT);
|
pinMode(PIN_DOOR_OPEN, INPUT);
|
||||||
pinMode(PIN_DOOR_CLOSED, INPUT);
|
pinMode(PIN_DOOR_CLOSED, INPUT);
|
||||||
pinMode(PIN_SENSOR_GATE, INPUT);
|
pinMode(PIN_SENSOR_GATE, INPUT);
|
||||||
|
pinMode(PIN_SPRINKLER1, OUTPUT);
|
||||||
|
pinMode(PIN_SPRINKLER2, OUTPUT);
|
||||||
|
pinMode(PIN_SPRINKLER3, OUTPUT);
|
||||||
|
pinMode(PIN_SPRINKLER4, OUTPUT);
|
||||||
|
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
@@ -89,7 +199,7 @@ void setup() {
|
|||||||
WiFi.persistent(true);
|
WiFi.persistent(true);
|
||||||
|
|
||||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
request->send_P(200, "text/html", "Hello World!");
|
pinStatus(request);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/garage", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
server.on("/garage", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||||
@@ -98,10 +208,12 @@ void setup() {
|
|||||||
action = request->getParam("action")->value();
|
action = request->getParam("action")->value();
|
||||||
|
|
||||||
if (action.equals("open") and digitalRead(PIN_DOOR_CLOSED) and not digitalRead(PIN_DOOR_OPEN)) {
|
if (action.equals("open") and digitalRead(PIN_DOOR_CLOSED) and not digitalRead(PIN_DOOR_OPEN)) {
|
||||||
request->send(200, "application/json", "{ \"action\": \"open\" }");
|
request->send(200, "application/json", "{ \"action\": \"open\" }");
|
||||||
|
triggerGarage();
|
||||||
}
|
}
|
||||||
else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) {
|
else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) {
|
||||||
request->send(200, "application/json", "{ \"action\": \"close\" }");
|
request->send(200, "application/json", "{ \"action\": \"close\" }");
|
||||||
|
triggerGarage();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pinStatus(request);
|
pinStatus(request);
|
||||||
@@ -123,7 +235,7 @@ void setup() {
|
|||||||
action = request->getParam("action")->value();
|
action = request->getParam("action")->value();
|
||||||
|
|
||||||
if (action.equals("open") and not digitalRead(PIN_SENSOR_GATE)) {
|
if (action.equals("open") and not digitalRead(PIN_SENSOR_GATE)) {
|
||||||
request->send(200, "application/json", "{ \"action\": \"open\" }");
|
request->send(200, "application/json", "{ \"action\": \"open\" }");
|
||||||
}
|
}
|
||||||
else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) {
|
else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) {
|
||||||
request->send(200, "application/json", "{ \"action\": \"close\" }");
|
request->send(200, "application/json", "{ \"action\": \"close\" }");
|
||||||
@@ -142,6 +254,55 @@ void setup() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
server.on("/sprinkler", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
||||||
|
String action;
|
||||||
|
if (request->hasParam("action")) {
|
||||||
|
action = request->getParam("action")->value();
|
||||||
|
|
||||||
|
if (action.equals("stop")) {
|
||||||
|
stopSprinklers();
|
||||||
|
request->send(200, "application/json", "{ \"action\": \"stop\" }");
|
||||||
|
}
|
||||||
|
else if (action.equals("start") and (request->hasParam("device")) and (request->hasParam("duration"))) {
|
||||||
|
String device = request->getParam("device")->value();
|
||||||
|
String duration = request->getParam("duration")->value();
|
||||||
|
sprinklerDuration = duration.toInt();
|
||||||
|
sprinklerStartMillis = millis();
|
||||||
|
if (device == 1) {
|
||||||
|
digitalWrite(PIN_SPRINKLER1, HIGH);
|
||||||
|
}
|
||||||
|
else if (device == 2) {
|
||||||
|
digitalWrite(PIN_SPRINKLER2, HIGH);
|
||||||
|
}
|
||||||
|
else if (device == 3) {
|
||||||
|
digitalWrite(PIN_SPRINKLER3, HIGH);
|
||||||
|
}
|
||||||
|
else if (device == 4) {
|
||||||
|
digitalWrite(PIN_SPRINKLER4, HIGH);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sprinklerDuration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// HIER GEHT'S WEITER
|
||||||
|
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\" }");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// timer
|
||||||
|
|
||||||
server.onNotFound(notFound);
|
server.onNotFound(notFound);
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
@@ -149,22 +310,16 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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 lastDebounceTime = 0;
|
||||||
unsigned long debounceDelay = 80;
|
unsigned long debounceDelay = 80;
|
||||||
int lastVal = LOW;
|
int lastVal = LOW;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
if ((sprinklerDuration > 0) and ((millis() - sprinklerStartMillis) > sprinklerDuration)) {
|
||||||
|
stopSprinklers();
|
||||||
|
}
|
||||||
|
|
||||||
int newVal = digitalRead(PIN_GARAGE_BUTTON);
|
int newVal = digitalRead(PIN_GARAGE_BUTTON);
|
||||||
if (newVal != lastVal) {
|
if (newVal != lastVal) {
|
||||||
lastDebounceTime = millis();
|
lastDebounceTime = millis();
|
||||||
@@ -174,7 +329,7 @@ void loop() {
|
|||||||
if (digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
|
if (digitalRead(PIN_GARAGE_BUTTON) != lastVal) {
|
||||||
lastVal = newVal;
|
lastVal = newVal;
|
||||||
if (newVal == HIGH) {
|
if (newVal == HIGH) {
|
||||||
trigger(PIN_GARAGE);
|
triggerGarage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,7 +343,7 @@ void loop() {
|
|||||||
if (digitalRead(PIN_GATE_BUTTON) != lastVal) {
|
if (digitalRead(PIN_GATE_BUTTON) != lastVal) {
|
||||||
lastVal = newVal;
|
lastVal = newVal;
|
||||||
if (newVal == HIGH) {
|
if (newVal == HIGH) {
|
||||||
trigger(PIN_GATE);
|
triggerGate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user