Here is my setup:
And the code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char *ssid = "ESPap";
const char *password = "thereisnospoon";
int count = 0;
bool countalarm = 0;
String statustext = "sample";
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "Button's " + statustext + ". Button was pressed " + String(count) + " times");
}
void setup() {
delay(1000);
Serial.begin(115200);
pinMode(2, INPUT);
WiFi.softAP(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
server.on("/", handleRoot);
server.begin();
}
void loop() {
if(digitalRead(2)==1){
statustext = "pressed";
if(countalarm == 0){
count+=1;
}
countalarm = 1;
} else {
statustext = "not pressed";
countalarm = 0;
}
server.handleClient();
}