From d7650ff1b35c0ce6761b99c0a3da427a71b7455e Mon Sep 17 00:00:00 2001 From: Pablo Clemente Date: Fri, 22 Dec 2023 14:34:33 +0100 Subject: [PATCH] Fix GPIO state change when LED_BUILTIN is not defined or -1 --- CHANGELOG.md | 5 ++ src/WebConfigServer.cpp | 105 ++++++++++++---------------------------- src/WebConfigServer.h | 4 ++ 3 files changed, 41 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 890fad7..b78a59a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ WebConfigServer: Changelog ========================== +HEAD +---- + +* Fix GPIO state change when LED_BUILTIN is not defined or -1 + v2.3.1 (2023-12-19) ------ diff --git a/src/WebConfigServer.cpp b/src/WebConfigServer.cpp index 4ed55f2..41e5b2c 100644 --- a/src/WebConfigServer.cpp +++ b/src/WebConfigServer.cpp @@ -512,30 +512,18 @@ void WebConfigServer::updateGpio(AsyncWebServerRequest *request){ #endif - // Reverse current LED status: + // Reverse current LED status: + String response = "{\"message\":\"GPIO " + String(gpio); + if(pin != -1 ){ pinMode(pin, OUTPUT); - // digitalWrite(pin, LOW); - digitalWrite(pin, !digitalRead(pin)); - - // if ( val == "true" ) { - // digitalWrite(pin, HIGH); - // } else if ( val == "false" ) { - // digitalWrite(pin, LOW); - // } else { - // success = "true"; - // Serial.println("Err parsing GPIO Value"); - // } - - // String response = "{\"gpio\":\"" + String(gpio) + "\","; - // response += "\"val\":\"" + String(val) + "\","; - // response += "\"success\":\"" + String(success) + "\"}"; - - String response = "{\"message\":\"GPIO " + String(gpio); response += " changed to " + String(!digitalRead(pin)) + "\"}"; + } else { + log_e("GPIO state can not be changed for the requested pin numer: %d ", pin); + response += " can not be changed\"}"; + } - request->send(200, "text/json", response); - Serial.println("JSON POST /gpio : " + response); - + request->send(200, "text/json", response); + Serial.println("JSON POST /gpio : " + response); } void WebConfigServer::configureServer(){ @@ -809,32 +797,17 @@ void WebConfigServer::updateGpio(WebServer *server){ } // Reverse current LED status: - pinMode(pin, OUTPUT); - // digitalWrite(pin, LOW); - Serial.println(pin); - Serial.print("Current status:"); - Serial.println(digitalRead(pin)); - digitalWrite(pin, !digitalRead(pin)); - - - // if ( val == "true" ) { - // digitalWrite(pin, HIGH); - // } else if ( val == "false" ) { - // digitalWrite(pin, LOW); - // } else { - // success = "true"; - // Serial.println("Err parsing GPIO Value"); - // } - - // String json = "{\"gpio\":\"" + String(gpio) + "\","; - // json += "\"val\":\"" + String(val) + "\","; - // json += "\"success\":\"" + String(success) + "\"}"; - - String json = "{\"message\":\"GPIO " + String(gpio); - json += " changed to " + String(!digitalRead(pin)) + "\"}"; - - server->send(200, "application/json", json); - Serial.println("GPIO updated!"); + String response = "{\"message\":\"GPIO " + String(gpio); + if(pin != -1 ){ + pinMode(pin, OUTPUT); + response += " changed to " + String(!digitalRead(pin)) + "\"}"; + } else { + log_e("GPIO state can not be changed for the requested pin numer: %d ", pin); + response += " can not be changed\"}"; + } + + server->send(200, "application/json", response); + Serial.println("JSON POST /gpio : " + response); } @@ -1067,33 +1040,19 @@ void WebConfigServer::updateGpio(ESP8266WebServer *server){ } - // Reverse current LED status: - pinMode(pin, OUTPUT); - // digitalWrite(pin, LOW); - Serial.println(pin); - Serial.print("Current status:"); - Serial.println(digitalRead(pin)); - digitalWrite(pin, !digitalRead(pin)); - - - // if ( val == "true" ) { - // digitalWrite(pin, HIGH); - // } else if ( val == "false" ) { - // digitalWrite(pin, LOW); - // } else { - // success = "true"; - // Serial.println("Err parsing GPIO Value"); - // } - - // String json = "{\"gpio\":\"" + String(gpio) + "\","; - // json += "\"val\":\"" + String(val) + "\","; - // json += "\"success\":\"" + String(success) + "\"}"; - - String json = "{\"message\":\"GPIO " + String(gpio); - json += " changed to " + String(!digitalRead(pin)) + "\"}"; - server->send(200, "application/json", json); - Serial.println("GPIO updated!"); + // Reverse current LED status: + String response = "{\"message\":\"GPIO " + String(gpio); + if(pin != -1 ){ + pinMode(pin, OUTPUT); + response += " changed to " + String(!digitalRead(pin)) + "\"}"; + } else { + log_e("GPIO state can not be changed for the requested pin numer: %d ", pin); + response += " can not be changed\"}"; + } + + server->send(200, "application/json", response); + Serial.println("JSON POST /gpio : " + response); } diff --git a/src/WebConfigServer.h b/src/WebConfigServer.h index b07c33c..3336c38 100644 --- a/src/WebConfigServer.h +++ b/src/WebConfigServer.h @@ -41,6 +41,10 @@ typedef int8_t WebConfigStatus; #define ARDUINOJSON_ENABLE_ALIGNMENT 1 +#ifndef LED_BUILTIN + #define LED_BUILTIN -1 +#endif + #include "IWebConfig.h"