From a8b7891fe3adb66f67502f71af5e4aab41ce5ade Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Tue, 13 Jun 2023 16:15:42 +0200 Subject: [PATCH] better GPIO endpoint --- src/main.cpp | 58 +++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9cb865e..3606c12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,10 +111,7 @@ String processor(const String& var){ String buttons = ""; #ifdef RELAY1 - buttons += "

Output - Relay 1

"; - #endif - #ifdef BUZZER - buttons += "

Output - Buzzer

"; + buttons += "

Output - Relay 1

"; #endif return buttons; @@ -340,39 +337,44 @@ void setup() { request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}"); }); - // Send a GET request to /gpio?output=&state= + // Send a GET request to /gpio?output=&state= server.on("/gpio", HTTP_GET, [] (AsyncWebServerRequest *request) { - String inputMessage1; - String inputMessage2; + String paramOutput; + String paramState; if(!request->authenticate(http_username, http_password)) return request->requestAuthentication(); - // GET input1 value on /gpio?output=&state= + // GET input1 value on /gpio?output=&state= if (request->hasParam("output") && request->hasParam("state")) { - inputMessage1 = request->getParam("output")->value(); - inputMessage2 = request->getParam("state")->value(); - digitalWrite(inputMessage1.toInt(), inputMessage2.toInt()); - } - else { - inputMessage1 = "No message sent"; - inputMessage2 = "No message sent"; - } + paramOutput = request->getParam("output")->value(); + paramState = request->getParam("state")->value(); - #ifdef SERIAL_DEBUG - Serial.print("GPIO: "); - Serial.print(inputMessage1); - Serial.print(" - Set to: "); - Serial.println(inputMessage2); - request->send(200, "text/plain", "OK"); - #endif + #ifdef RELAY1 + if (paramOutput == "relay1") { + if (paramState == String(0)){ + unlockDoor(false); + AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Door Unlocked.\"}"); + request->send(response); + } + else if (paramState == String(1)) { + lockDoor(); + AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Door Locked.\"}"); + request->send(response); + } + else { + AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"msg\":\"Incorect state provide 0 to unlock and 1 to lock.\"}"); + request->send(response); + } + } - #ifdef WEB_SERIAL_DEBUG - WebSerial.print("GPIO: "); - WebSerial.print(inputMessage1); - WebSerial.print(" - Set to: "); - WebSerial.println(inputMessage2); + #endif + + AsyncWebServerResponse *response = request->beginResponse(400, "application/json", "{\"msg\":\"Error with request, incorrect GPIO pin number.\"}"); + request->send(response); + + } });