node mcu programming

Hi dears,

I want to program node mcu as an accesspoint that when the state of one of the pins of the node changed,it send me "start" or "stop" on webserver.
I wrote my codes,and I didn't get any error but "start" is only shown on the webserver.
thank you all for your helps.
and I will attach my codes.

[tt]


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const int  buttonPin = 0;   
int buttonState = 0;        
int lastButtonState = 0; 
int x =100;
const char* ssid = "ESPWebServer";
const char* password = "12345678";

ESP8266WebServer server(80); //Server on port 80

//==============================================================
//     This rutine is exicuted when you open its IP in browser
//==============================================================

void handleRoot() {
      server.send(200, "text/plain", "start");
}

void handleRoot1(){
  server.send(200, "text/plain", "stop");
}
    


//===============================================================
//                  SETUP
//===============================================================
void setup(void){

  pinMode(buttonPin, INPUT);
  Serial.begin(115200);

  Serial.begin(115200);
  Serial.println("");
  WiFi.mode(WIFI_AP);           //Only Access point
  WiFi.softAP(ssid, password);  //Start HOTspot removing password will disable security

  IPAddress myIP = WiFi.softAPIP(); //Get IP address
  Serial.print("HotSpt IP:");
  Serial.println(myIP);
 
  server.on("/", handleRoot);      //Which routine to handle at root location
  server.begin();                  //Start server
  Serial.println("HTTP server started");
}
//===============================================================
//                     LOOP
//===============================================================
void loop(void){buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      handleRoot();
    } else {
      handleRoot1();
    }
    delay(50);
  }
  lastButtonState = buttonState;
  
  
  server.handleClient();          //Handle client requests
}

[/tt]

try moving "lastButtonState = buttonState;" into your "if (buttonState != lastButtonState)" statement

How is the button wired ?
Do you have a pulldown resistor in place to keep the input LOW except when the button is pressed ?

UKHeliBob:
How is the button wired ?
Do you have a pulldown resistor in place to keep the input LOW except when the button is pressed ?

yes I use a 10kohm resistor to make it pull up

Rather than using an external resistor it is easier to use INPUT_PULLUP in pinMode() to turn on the built in pullup resistor.
Is the button input wired to go LOW when the button is pressed ?
What do you see if you print buttonState ?

OP, you have a misunderstanding of how the web server handles client requests. This call in your setup() function:

server.on("/", handleRoot);

registers your function handleRoot() as the callback function for when the client browsers requests the root page (i.e. IP Address xxx.xxx.xxx.xxx). Calling handleRoot() and handleRoot1() in the loop() function is never going to change that. In fact, the result may be undefined if there's no client connected to the server at the time.

What you should do is get rid of the handleRoot1() function. Then modify handleRoot() to read buttonPin and decide which response to send based on the value read. You can then get rid of everything in the loop() function except for:

server.handleClient();