web server and push button control one led using nodemcu/wemos d1


please 1 me..  new in arduino

this code.. how combine push button cord control same led..
please me.. tq :smiley-confuse:




#include <esp8266wifi.h>
 
const char* ssid = "ssid name";
const char* password = "ssid password";
 
int ledpin = d5;
wifiserver server(80);
 
void setup() {
  serial.begin(115200);
  delay(10);
 
 
  pinmode(ledpin, output);
  digitalwrite(ledpin, low);
 
  // connect wifi network
  serial.println();
  serial.println();
  serial.print("connecting ");
  serial.println(ssid);
 
  wifi.begin(ssid, password);
 
  while (wifi.status() != wl_connected) {
    delay(500);
    serial.print(".");
  }
  serial.println("");
  serial.println("wifi connected");
 
  // start server
  server.begin();
  serial.println("server started");
 
  // print ip address
  serial.print("use url : ");
  serial.print("http://");
  serial.print(wifi.localip());
  serial.println("/");
 
}
 
void loop() {
  // check if client has connected
  wificlient client = server.available();
  if (!client) {
    return;
  }
 
  // wait until client sends data
  serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // read first line of request
  string request = client.readstringuntil('\r');
  serial.println(request);
  client.flush();
 
  // match request
 
  int value = low;
  if (request.indexof("/led=on") != -1) {
    digitalwrite(ledpin, high);
    value = high;
  }
  if (request.indexof("/led=off") != -1){
    digitalwrite(ledpin, low);
    value = low;
  }
 
 
 
  // return response
  client.println("http/1.1 200 ok");
  client.println("content-type: text/html");
  client.println(""); //  not forget one
  client.println("<!doctype html>");
  client.println("<html>");
 
  client.print("led pin now: ");
 
  if(value == high) {
    client.print("on"); 
  } else {
    client.print("off");
  }
  client.println("<br><br>");
  client.println("click <a href=\"/led=on\">here</a> turn led on pin 5 on<br>");
  client.println("click <a href=\"/led=off\">here</a> turn led on pin 5 off<br>");
  client.println("</html>");
 
  delay(1);
  serial.println("client disconnected");
  serial.println("");
 
}

start getting led working button alone. find file > examples > 02.digital > digitalinputpullup useful. depending on whether you're using momentary or maintained button may need have @ file > examples > 02.digital > debounce. make sure understand every single line of code does.

next esp8266 code working perfectly. make sure understand every single line of code does.

now you're ready combine 2 programs together.

please use code tags(</> button on toolbar) when post code, warning/error messages or other output on forum. make sure correctly formatted you.


Arduino Forum > Using Arduino > Installation & Troubleshooting > web server and push button control one led using nodemcu/wemos d1


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'