better GPIO endpoint

This commit is contained in:
Matthew Frost 2023-06-13 16:15:42 +02:00
parent 518f8cb45c
commit a8b7891fe3

View file

@ -111,10 +111,7 @@ String processor(const String& var){
String buttons = "";
#ifdef RELAY1
buttons += "<h4>Output - Relay 1 </h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\""+String(RELAY1_PIN)+"\" " + outputState(RELAY1_PIN) + "><span class=\"slider\"></span></label>";
#endif
#ifdef BUZZER
buttons += "<h4>Output - Buzzer </h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\""+String(ALARM_PIN)+"\" " + outputState(ALARM_PIN) + "><span class=\"slider\"></span></label>";
buttons += "<h4>Output - Relay 1 </h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"relay1\" " + outputState(RELAY1_PIN) + "><span class=\"slider\"></span></label>";
#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 <ESP_IP>/gpio?output=<inputMessage1>&state=<inputMessage2>
// Send a GET request to <ESP_IP>/gpio?output=<paramOutput>&state=<paramState>
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 <ESP_IP>/gpio?output=<inputMessage1>&state=<inputMessage2>
// GET input1 value on <ESP_IP>/gpio?output=<paramOutput>&state=<paramState>
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);
}
});