This is an old revision of the document!


Controlling LED with simple Web server

It is possible to control the state of the LED connected to the ESP8266 board using HTTP requests from a web browser. The program presented in this subchapter is based on the example “HelloServer” available in the ESP8266WebServer library. Some modifications were made to simplify the program and to handle requests to turn the LED on and off. To check if it works it is required to add WiFi network credentials, and set the led variable with the number of GPIO to which the LED is connected. After a successful connection to the WiFi ESP8266 would present through the serial monitor the IP address (e.g. 192.168.1.100). Writing in the address bar in the browser “192.168.1.100” should return to the serial monitor message “hello from esp8266!”. LED behaviour is controlled with the following requests: “192.168.1.100/LED0” and “192.168.1.100/LED1”. The original example uses the MDNS protocol that allows to access the board with the address <code> http://esp8266/ </code> It is possible to use it instead of IP address: <code> http://esp8266/LED0 http://esp8266/LED1 </code> <code c> #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #ifndef STASSID #define STASSID “*” #define STAPSK “*” #endif const char* ssid = STASSID; const char* password = STAPSK; ESP8266WebServer server(80); const int led = 2; void handleRoot() { Originally LED was controlled for every root request

//so it is required to comment the lines which modify the LED state
//digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!\r\n");
//digitalWrite(led, 0);

}

void handleNotFound() {

//digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
  message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
//digitalWrite(led, 0);

}

void setup(void) {

pinMode(led, OUTPUT);
//digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
  Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
// request for turning led on
server.on("/LED1", [](){
  server.send(200, "text/plain", "LED is ON");
  digitalWrite(led, 1);
});
// request for turning led off
server.on("/LED0", [](){
  server.send(200, "text/plain", "LED is OFF");
  digitalWrite(led, 0);
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");

}

void loop(void) {

server.handleClient();
MDNS.update();

} </code>

en/iot-open/espressif_es8266_http_led_control.1697017013.txt.gz · Last modified: 2023/10/11 09:36 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0