I have a lamp that is controlled by a esp8266 using a webserver, I want to also control that lamp from HA.
The relevant code from the lamp is this
server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
server.begin(); // Actually start the server
Serial.println("HTTP server started");
}
void loop() {
// Check the state of the foot switch and toggle the relay accordingly
if (digitalRead(pinIntrerupator) != pozVeche)
{
pozVeche = digitalRead(pinIntrerupator);
digitalWrite(releu, !digitalRead(releu));
}
// Handle HTTP requests from clients if WiFi is connected
if (WiFi.status() == WL_CONNECTED) {
server.handleClient();
}
delay(100);
}
void handleLED() { // If a POST request is made to URI /LED
digitalWrite(releu,!digitalRead(releu)); // Change the state of the LED
server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}
void handleRoot(){ // When URI / is requested, send a web page with a button to toggle the LED
// server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\"value=\"Comuta\"></form>");
server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Comuta\" align=\"middle\" style=\"display:block; margin-left:auto; margin-right:auto; margin-top:150px; background-color:#f44336; font-weight:800; height:230px; color:white; font-size:110px; width:50%;\"></form>");
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
I tryed doing what this tutorial says but couldn’t get it to work.
Then I tried this:
switch:
- platform: rest
name: Lampa Birou
icon: mdi:cat
resource: http://192.168.100.6/LED
method: POST
headers:
Content-Type: application/x-www-form-urlencoded
body_on: ""
body_off: ""
but it also doesn’t work, it doesen’t even show me the switch in developer-tools/state.
I’ve only installed HA today so I don’t know anything, any help wold be appreciated.
Thanks!