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