0

I've connected a 5-way tactile switch to a ESP8266 module and am using the Arduino IDE to create an automatically updating web server that displays the direction that the button is pressed in when pressed. Currently the webpage displays the first direction that the button was pressed in but does not update the page after I press the button several more times. Below is my code:

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

//ssid and password info, ommitted here for obvious reasons

int upPin = 5; 
int leftPin = 4; 
int centerPin = 0; 
int rightPin = 2; 
int downPin = 14; 

ESP8266WebServer server ( 80 );

void handleRoot() {
    server.send(200, "text/html", "<html>\
  <head>\
    <meta http-equiv='refresh' content='1'/>\
  </head>\
  <body>\
  </body>\
</html>");   // Send HTTP status 200 (Ok) and send some text to the 
browser/client
}

void setup(){
 Serial.begin ( 115200 );
 WiFi.mode ( WIFI_STA );
 WiFi.begin ( ssid, password );

 server.on ( "/", handleRoot );
 server.on ("/", button );
 server.begin();

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

void button(){
  if (digitalRead(rightPin) == LOW)
  {
    server.send(200, "text/html", "<p>*RIGHT#</p>");
    delay(10);
  }

  if (digitalRead(leftPin) == LOW)
  {
    server.send(200, "text/html", "<p>*LEFT#</p>");
    delay(10);   
  }

  if (digitalRead(upPin) == LOW)
  {
    server.send(200, "text/html", "<p>*UP#</p>");
    delay(10);
  }


  if (digitalRead(downPin) == LOW)
  {
    server.send(200, "text/html", "<p>*DOWN#</p>");
    delay(10);
  } 

  if (digitalRead(centerPin) == LOW)
  {
    server.send(200, "text/html", "<p>*CENTER#</p>");  
    delay(10);
  }

}
  • so / is root or button? do you hold a switch all the time? – Juraj Jul 11 '18 at 19:58
0

I changed the code so that the button function runs with "/" and took out the server.on("/", handleRoot) line, thanks to @Juraj. Sigh

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.