[GH-ISSUE #1236] How to use push button interruupts with wifi manager #1058

Open
opened 2026-02-28 01:28:20 +03:00 by kerem · 4 comments
Owner

Originally created by @vxplore on GitHub (Apr 3, 2021).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1236

I am successfully using wifi manager in esp 01 board.
But now i want to add a push button for extra functionaliry like reset, manual operation etc.
But could not figure out how to use both(wifimanager and push button).

My current code is following

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <EEPROM.h>
WiFiServer server(80);
String header;
int outputState = LOW;
const int outputPin = 2;

void setSavedState(int value)
{
outputState = value;
EEPROM.begin(4);
EEPROM.write(0, value?'1':'0');
EEPROM.write(1, 's');
EEPROM.write(2, 'e');
EEPROM.write(3, 't');
EEPROM.commit();
EEPROM.end();
}

int getSavedState()
{
EEPROM.begin(4);
if(
EEPROM.read(1)=='s'&&
EEPROM.read(2)=='e'&&
EEPROM.read(3)=='t'
)
{
return EEPROM.read(0)=='1'?HIGH:LOW;
}
else
{
return LOW;
}

EEPROM.end();
}

WiFiManager wifiManager;

void setup() {
outputState = getSavedState();
Serial.begin(115200);
pinMode(outputPin, OUTPUT);
digitalWrite(outputPin, outputState);
wifiManager.autoConnect("VXSS000000001");
Serial.println("Connected.");
server.begin();
}
String getStateText()
{
if(outputState==LOW)
{
return "off";
}
else
{
return "on";
}
}
bool erase_requested = false;
void loop(){
WiFiClient client = server.available();
if (client)
{
Serial.println("New Client.");
String currentLine = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n')
{
if (currentLine.length() == 0)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /load/on") >= 0)
{
Serial.println("Load On");
setSavedState(HIGH);
digitalWrite(outputPin, outputState);
}
else if (header.indexOf("GET /load/off") >= 0)
{
Serial.println("Load Off");
setSavedState(LOW);
digitalWrite(outputPin, outputState);
}
else if (header.indexOf("GET /reset") >= 0)
{
Serial.println("Erasing");
erase_requested = true;
break;
}
client.println("<html>");
client.println("<head><meta name="viewport" content="width=device-width, initial-scale=1">");
client.println("<link rel="icon" href="data:,">");
client.println("</head>");
client.println("

VXSS000000001

");
client.println("

Load - State " + getStateText() + "

");
if (getStateText()=="off")
{
client.println("

<a href="/load/on"><button class="button">ON

");
}
else
{
client.println("

<a href="/load/off"><button class="button button2">OFF

");
}

        client.println("<p><a href=\"/reset\"><button class=\"button\">RESET</button></a></p>");
        client.println("</body></html>");
        client.println();
        break;
      } 
      else 
      {
        currentLine = "";
      }
    } 
    else if (c != '\r') 
    {
      currentLine += c;
    }
  }
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");

}
if(erase_requested)
{
erase_requested = false;
wifiManager.resetSettings();
Serial.println("Erased");
delay(100);
wifiManager.disconnect();
Serial.println("Disconnected");
delay(100);
ESP.restart();
delay(1000);
}
}

So what will be the solution

Originally created by @vxplore on GitHub (Apr 3, 2021). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1236 I am successfully using wifi manager in esp 01 board. But now i want to add a push button for extra functionaliry like reset, manual operation etc. But could not figure out how to use both(wifimanager and push button). My current code is following #include <ESP8266WiFi.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> #include <EEPROM.h> WiFiServer server(80); String header; int outputState = LOW; const int outputPin = 2; void setSavedState(int value) { outputState = value; EEPROM.begin(4); EEPROM.write(0, value?'1':'0'); EEPROM.write(1, 's'); EEPROM.write(2, 'e'); EEPROM.write(3, 't'); EEPROM.commit(); EEPROM.end(); } int getSavedState() { EEPROM.begin(4); if( EEPROM.read(1)=='s'&& EEPROM.read(2)=='e'&& EEPROM.read(3)=='t' ) { return EEPROM.read(0)=='1'?HIGH:LOW; } else { return LOW; } EEPROM.end(); } WiFiManager wifiManager; void setup() { outputState = getSavedState(); Serial.begin(115200); pinMode(outputPin, OUTPUT); digitalWrite(outputPin, outputState); wifiManager.autoConnect("VXSS000000001"); Serial.println("Connected."); server.begin(); } String getStateText() { if(outputState==LOW) { return "off"; } else { return "on"; } } bool erase_requested = false; void loop(){ WiFiClient client = server.available(); if (client) { Serial.println("New Client."); String currentLine = ""; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); header += c; if (c == '\n') { if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); if (header.indexOf("GET /load/on") >= 0) { Serial.println("Load On"); setSavedState(HIGH); digitalWrite(outputPin, outputState); } else if (header.indexOf("GET /load/off") >= 0) { Serial.println("Load Off"); setSavedState(LOW); digitalWrite(outputPin, outputState); } else if (header.indexOf("GET /reset") >= 0) { Serial.println("Erasing"); erase_requested = true; break; } client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println("</style></head>"); client.println("<body><h1>VXSS000000001</h1>"); client.println("<p>Load - State " + getStateText() + "</p>"); if (getStateText()=="off") { client.println("<p><a href=\"/load/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/load/off\"><button class=\"button button2\">OFF</button></a></p>"); } client.println("<p><a href=\"/reset\"><button class=\"button\">RESET</button></a></p>"); client.println("</body></html>"); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } } } header = ""; client.stop(); Serial.println("Client disconnected."); Serial.println(""); } if(erase_requested) { erase_requested = false; wifiManager.resetSettings(); Serial.println("Erased"); delay(100); wifiManager.disconnect(); Serial.println("Disconnected"); delay(100); ESP.restart(); delay(1000); } } So what will be the solution
Author
Owner

@EgHubs commented on GitHub (Apr 4, 2021):

Will, a push-button requires an input pin to read its state, not an output pin,
if outputPin is the pin you are going to request a reset with then you should assign it as a digital input pin then use digitalRead to read its value.
I would recommend that you use this example and justify it for your needs.

<!-- gh-comment-id:813103638 --> @EgHubs commented on GitHub (Apr 4, 2021): Will, a push-button requires an input pin to read its state, not an output pin, if outputPin is the pin you are going to request a reset with then you should assign it as a digital input pin then use digitalRead to read its value. I would recommend that you use this [example](https://github.com/tzapu/WiFiManager/blob/master/examples/OnDemand/OnDemandConfigPortal/OnDemandConfigPortal.ino) and justify it for your needs.
Author
Owner

@EgHubs commented on GitHub (Apr 4, 2021):

Here is the code for you, connect the TRIGGER_PIN to the ground through a push-button.
if you want to do a reset then click on the button for a second.

/**
 * OnDemandConfigPortal.ino
 * example of running the configPortal AP manually, independantly from the captiveportal
 * trigger pin will start a configPortal AP for 120 seconds then turn it off.
 *
 */
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 0

int timeout = 120; // seconds to run for
  WiFiManager wm;
void setup() {
  WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");
  pinMode(TRIGGER_PIN, INPUT_PULLUP);

  //reset settings - for testing
  //wifiManager.resetSettings();

  // set configportal timeout
  wm.setConfigPortalTimeout(timeout);

  if (!wm.startConfigPortal("OnDemandAP")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.restart();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
}

void loop() {
  // is configuration portal requested?
  if (digitalRead(TRIGGER_PIN) == LOW) {
    delay(500);
    if (digitalRead(TRIGGER_PIN) == LOW) 
    {
        wm.resetSettings();
        ESP.restart();
    }
  }
  
  // put your main code here, to run repeatedly:

}
<!-- gh-comment-id:813104530 --> @EgHubs commented on GitHub (Apr 4, 2021): Here is the code for you, connect the TRIGGER_PIN to the ground through a push-button. if you want to do a reset then click on the button for a second. ``` /** * OnDemandConfigPortal.ino * example of running the configPortal AP manually, independantly from the captiveportal * trigger pin will start a configPortal AP for 120 seconds then turn it off. * */ #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager // select which pin will trigger the configuration portal when set to LOW #define TRIGGER_PIN 0 int timeout = 120; // seconds to run for WiFiManager wm; void setup() { WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // put your setup code here, to run once: Serial.begin(115200); Serial.println("\n Starting"); pinMode(TRIGGER_PIN, INPUT_PULLUP); //reset settings - for testing //wifiManager.resetSettings(); // set configportal timeout wm.setConfigPortalTimeout(timeout); if (!wm.startConfigPortal("OnDemandAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.restart(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } void loop() { // is configuration portal requested? if (digitalRead(TRIGGER_PIN) == LOW) { delay(500); if (digitalRead(TRIGGER_PIN) == LOW) { wm.resetSettings(); ESP.restart(); } } // put your main code here, to run repeatedly: } ```
Author
Owner

@tablatronix commented on GitHub (Apr 4, 2021):

Since wm is blocking you will want to use the new non blocking it will be easier

<!-- gh-comment-id:813108655 --> @tablatronix commented on GitHub (Apr 4, 2021): Since wm is blocking you will want to use the new non blocking it will be easier
Author
Owner

@audiofill42 commented on GitHub (May 31, 2021):

Hello,

I am having same situation with the Wifi Manager.

I am trying to create a project based on ESP8266 & Blynk to use a button and relay to command something.
I would like to have implemented the function of On demand using the reet button if possible or the project button (D0).

Did you solded your problem?
And if Yes, how? Can you share to me the result/solution?

Thanks in advance.

<!-- gh-comment-id:851527349 --> @audiofill42 commented on GitHub (May 31, 2021): Hello, I am having same situation with the Wifi Manager. I am trying to create a project based on ESP8266 & Blynk to use a button and relay to command something. I would like to have implemented the function of On demand using the reet button if possible or the project button (D0). Did you solded your problem? And if Yes, how? Can you share to me the result/solution? Thanks in advance.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1058
No description provided.