Skip to content

Commit

Permalink
Fix GPIO state change when LED_BUILTIN is not defined or -1
Browse files Browse the repository at this point in the history
  • Loading branch information
paclema committed Dec 22, 2023
1 parent 2132e82 commit d7650ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 73 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
------

Expand Down
105 changes: 32 additions & 73 deletions src/WebConfigServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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);

}

Expand Down
4 changes: 4 additions & 0 deletions src/WebConfigServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ typedef int8_t WebConfigStatus;

#define ARDUINOJSON_ENABLE_ALIGNMENT 1

#ifndef LED_BUILTIN
#define LED_BUILTIN -1
#endif

#include "IWebConfig.h"


Expand Down

0 comments on commit d7650ff

Please sign in to comment.