Skip to content

Commit d7650ff

Browse files
committed
Fix GPIO state change when LED_BUILTIN is not defined or -1
1 parent 2132e82 commit d7650ff

File tree

3 files changed

+41
-73
lines changed

3 files changed

+41
-73
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
WebConfigServer: Changelog
22
==========================
33

4+
HEAD
5+
----
6+
7+
* Fix GPIO state change when LED_BUILTIN is not defined or -1
8+
49
v2.3.1 (2023-12-19)
510
------
611

src/WebConfigServer.cpp

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -512,30 +512,18 @@ void WebConfigServer::updateGpio(AsyncWebServerRequest *request){
512512
#endif
513513

514514

515-
// Reverse current LED status:
515+
// Reverse current LED status:
516+
String response = "{\"message\":\"GPIO " + String(gpio);
517+
if(pin != -1 ){
516518
pinMode(pin, OUTPUT);
517-
// digitalWrite(pin, LOW);
518-
digitalWrite(pin, !digitalRead(pin));
519-
520-
// if ( val == "true" ) {
521-
// digitalWrite(pin, HIGH);
522-
// } else if ( val == "false" ) {
523-
// digitalWrite(pin, LOW);
524-
// } else {
525-
// success = "true";
526-
// Serial.println("Err parsing GPIO Value");
527-
// }
528-
529-
// String response = "{\"gpio\":\"" + String(gpio) + "\",";
530-
// response += "\"val\":\"" + String(val) + "\",";
531-
// response += "\"success\":\"" + String(success) + "\"}";
532-
533-
String response = "{\"message\":\"GPIO " + String(gpio);
534519
response += " changed to " + String(!digitalRead(pin)) + "\"}";
520+
} else {
521+
log_e("GPIO state can not be changed for the requested pin numer: %d ", pin);
522+
response += " can not be changed\"}";
523+
}
535524

536-
request->send(200, "text/json", response);
537-
Serial.println("JSON POST /gpio : " + response);
538-
525+
request->send(200, "text/json", response);
526+
Serial.println("JSON POST /gpio : " + response);
539527
}
540528

541529
void WebConfigServer::configureServer(){
@@ -809,32 +797,17 @@ void WebConfigServer::updateGpio(WebServer *server){
809797
}
810798

811799
// Reverse current LED status:
812-
pinMode(pin, OUTPUT);
813-
// digitalWrite(pin, LOW);
814-
Serial.println(pin);
815-
Serial.print("Current status:");
816-
Serial.println(digitalRead(pin));
817-
digitalWrite(pin, !digitalRead(pin));
818-
819-
820-
// if ( val == "true" ) {
821-
// digitalWrite(pin, HIGH);
822-
// } else if ( val == "false" ) {
823-
// digitalWrite(pin, LOW);
824-
// } else {
825-
// success = "true";
826-
// Serial.println("Err parsing GPIO Value");
827-
// }
828-
829-
// String json = "{\"gpio\":\"" + String(gpio) + "\",";
830-
// json += "\"val\":\"" + String(val) + "\",";
831-
// json += "\"success\":\"" + String(success) + "\"}";
832-
833-
String json = "{\"message\":\"GPIO " + String(gpio);
834-
json += " changed to " + String(!digitalRead(pin)) + "\"}";
835-
836-
server->send(200, "application/json", json);
837-
Serial.println("GPIO updated!");
800+
String response = "{\"message\":\"GPIO " + String(gpio);
801+
if(pin != -1 ){
802+
pinMode(pin, OUTPUT);
803+
response += " changed to " + String(!digitalRead(pin)) + "\"}";
804+
} else {
805+
log_e("GPIO state can not be changed for the requested pin numer: %d ", pin);
806+
response += " can not be changed\"}";
807+
}
808+
809+
server->send(200, "application/json", response);
810+
Serial.println("JSON POST /gpio : " + response);
838811

839812
}
840813

@@ -1067,33 +1040,19 @@ void WebConfigServer::updateGpio(ESP8266WebServer *server){
10671040

10681041
}
10691042

1070-
// Reverse current LED status:
1071-
pinMode(pin, OUTPUT);
1072-
// digitalWrite(pin, LOW);
1073-
Serial.println(pin);
1074-
Serial.print("Current status:");
1075-
Serial.println(digitalRead(pin));
1076-
digitalWrite(pin, !digitalRead(pin));
1077-
1078-
1079-
// if ( val == "true" ) {
1080-
// digitalWrite(pin, HIGH);
1081-
// } else if ( val == "false" ) {
1082-
// digitalWrite(pin, LOW);
1083-
// } else {
1084-
// success = "true";
1085-
// Serial.println("Err parsing GPIO Value");
1086-
// }
1087-
1088-
// String json = "{\"gpio\":\"" + String(gpio) + "\",";
1089-
// json += "\"val\":\"" + String(val) + "\",";
1090-
// json += "\"success\":\"" + String(success) + "\"}";
1091-
1092-
String json = "{\"message\":\"GPIO " + String(gpio);
1093-
json += " changed to " + String(!digitalRead(pin)) + "\"}";
10941043

1095-
server->send(200, "application/json", json);
1096-
Serial.println("GPIO updated!");
1044+
// Reverse current LED status:
1045+
String response = "{\"message\":\"GPIO " + String(gpio);
1046+
if(pin != -1 ){
1047+
pinMode(pin, OUTPUT);
1048+
response += " changed to " + String(!digitalRead(pin)) + "\"}";
1049+
} else {
1050+
log_e("GPIO state can not be changed for the requested pin numer: %d ", pin);
1051+
response += " can not be changed\"}";
1052+
}
1053+
1054+
server->send(200, "application/json", response);
1055+
Serial.println("JSON POST /gpio : " + response);
10971056

10981057
}
10991058

src/WebConfigServer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ typedef int8_t WebConfigStatus;
4141

4242
#define ARDUINOJSON_ENABLE_ALIGNMENT 1
4343

44+
#ifndef LED_BUILTIN
45+
#define LED_BUILTIN -1
46+
#endif
47+
4448
#include "IWebConfig.h"
4549

4650

0 commit comments

Comments
 (0)