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 <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

#ifndef STASSID
#define STASSID "wifi_name"
#define STAPSK  "wifi_password"

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 = "ligth101";

const int relayPin = 0;

void handleOn() {
    digitalWrite(relayPin, LOW);
    server.send(200, "text/plain", "Ligth on");

void handleOff() {
    digitalWrite(relayPin, HIGH);
    server.send(200, "text/plain", "Ligth off");

void handlePing() {
    server.send(200, "text/plain", "ligth");

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

void configWifi(){
    WiFi.config(ip, subnet, gateway, dns);
    WiFi.begin(ssid, password);    
    while (WiFi.status() != WL_CONNECTED) {

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

void sendCrossOriginHeader(){

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

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

    server.on(F("/off"), HTTP_OPTIONS, sendCrossOriginHeader);
    server.on(F("/off"), HTTP_GET, handleOff);

void setup() {

void loop() {

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(){
    if(buttonState == HIGH) {
        digitalWrite( relayPin, !digitalRead(relayPin) );

void loop() {

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

  1. 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.
  2. 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?


  • There are a number of issues here. 1) GPIO2 is the TX1 output of the first Serial Port. You can't use it if you also have serial output via Serial 1 (which might or might not be the case). 2) Why do you set buttonPin to LOW when you're using it as an input? 3) You're not debouncing the button in any way. 4) Do you use a pull-up or pull-down for your button, or neither? It's unclear from your description. – StarCat Aug 14 '20 at 8:34
  • 1
    remove digitalWrite from handleOn(), handleOff() and from handleButton() ... only set or clear a flag in those three functions .... put digitalWrite (relay control) at the end of loop() ... if the flag is set, then turn on relay ... if the flag is cleared, then turn off relay .... you can then easily expand your code to set and clear the flag using a timer if you like – jsotola Aug 15 '20 at 1:10

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?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.