Esp8266 lamp controlled also from HA

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!

If the lamp is controlled by a esp why is it not ESP-Home on it?

Besides, as Hellis81 said, that using ESPHome would make your life easier, did you confirm the HTTP call is working outside of HA, e.g. via curl, and with what command?

I’m not using ESPHome because I made that lamp years ago before I knew about HA or ESPHome. It’ s been reliable and I don’t intend to change it.

You have one input pin and one output pin to drive a relay.
That’s about six lines in ESP-Home.
I bet it will be faster to make it ESP-Home than trying to get this working.
You could actually buy a new ESP and preprogram it, then just make a quick swap.

Yes it works with:

curl -X POST http://192.168.100.6/LED

The physical installation isn’t exactly easily accesibile and right now I’m just playing with HA, I don’t know if I’ll keep using it so I preffer to not change what I have and is working.
But I’m more than happy to learn so if you could tell me those 6 lines of code I’d appreciate it.
Also in the original post I said that the switch doesen’t appear anywhere and I don’t know what I’m doing wrong so that that would be useful to know.

Have a good day and thank You!