ESP8266 - ESP01S with relay. Toggling the relay with http (GPIO0) and GPIO2 at the same time I have an ESP8266 module with a relay like this: https://aliexpress.com/item/4000348370586.html.
I have been able to control the module, I can deactivate and activate the relay by means of an http request, with the following code.
#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESP8266WebServer.h&gt;

#ifndef STASSID
#define STASSID &quot;wifi_name&quot;
#define STAPSK  &quot;wifi_password&quot;
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

IPAddress ip(192, 168, 0, 101); //ESP static ip
IPAddress gateway(192, 168, 0, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS
const char* deviceName = &quot;ligth101&quot;;

const int relayPin = 0;

void handleOn() {
    digitalWrite(relayPin, LOW);
    setCrossOrigin();
    server.send(200, &quot;text/plain&quot;, &quot;Ligth on&quot;);
}

void handleOff() {
    digitalWrite(relayPin, HIGH);
    setCrossOrigin();
    server.send(200, &quot;text/plain&quot;, &quot;Ligth off&quot;);
}

void handlePing() {
    setCrossOrigin();
    server.send(200, &quot;text/plain&quot;, &quot;ligth&quot;);
}

void configPin(){
    pinMode(relayPin, OUTPUT);
    digitalWrite(relayPin, HIGH);
}

void configWifi(){
    WiFi.setAutoConnect(false);
    WiFi.disconnect();
    WiFi.hostname(deviceName);
    WiFi.config(ip, subnet, gateway, dns);
    WiFi.begin(ssid, password);    
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    }
}

void setCrossOrigin(){
    server.sendHeader(F(&quot;Access-Control-Allow-Origin&quot;), F(&quot;*&quot;));
    server.sendHeader(F(&quot;Access-Control-Max-Age&quot;), F(&quot;600&quot;));
    server.sendHeader(F(&quot;Access-Control-Allow-Methods&quot;), F(&quot;PUT,POST,GET,OPTIONS&quot;));
    server.sendHeader(F(&quot;Access-Control-Allow-Headers&quot;), F(&quot;*&quot;));
}

void sendCrossOriginHeader(){
    setCrossOrigin();
    server.send(204);
}

void configServerPaths(){
    server.on(F(&quot;/ping&quot;), HTTP_OPTIONS, sendCrossOriginHeader);
    server.on(F(&quot;/ping&quot;), HTTP_GET, handlePing);

    server.on(F(&quot;/on&quot;), HTTP_OPTIONS, sendCrossOriginHeader);
    server.on(F(&quot;/on&quot;), HTTP_GET, handleOn);

    server.on(F(&quot;/off&quot;), HTTP_OPTIONS, sendCrossOriginHeader);
    server.on(F(&quot;/off&quot;), HTTP_GET, handleOff);
    
    server.begin();
}

void setup() {
    configPin();    
    configWifi();
    configServerPaths();
}

void loop() {
    server.handleClient();
}

Now, I want to use the GPIO2 pin to also turn the relay on and off. The desired behavior is to change the state of the relay regardless of whether it comes from the button or from an http request.
I have tried the following modifications to the code:
 
const int buttonPin = 2;
int buttonState = 0;

void configPin(){
    ...
    digitalWrite(buttonPin, LOW);
}

void handleButton(){
    buttonState=digitalRead(buttonPin);
    if(buttonState == HIGH) {
        digitalWrite( relayPin, !digitalRead(relayPin) );
        delay(1000);
    } 
}

void loop() {
    server.handleClient();
    handleButton();
}

With these modifications to the code, I have tested the following two modifications to the hardware:

Placing a switch between ground and GPIO2. As a result of this, each time I activate the push-button, the relay remains intermittent, going from one state to the other infinitely.
Placing a switch between ground and GPIO2, but adding a resistor between the switch and the GPIO2 pin, but this also does not work.

Is there a way to deactivate / activate the relay using GPIO2 with a push button and via http as well?
Thanks!
Contrary to esp-01, on the esp-01S io 0 and io 2 pins have a pull-up resistor.
Wire a button between io 2 and ground and you will read LOW if the button is pushed.
Use a state variable for the state of the relay, don't read back the pin (does it even work to read the state of an output pin with digitalRead on esp8266?).