This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:espressif_es8266_http_led_control [2023/10/10 08:13] – created ktokarz | en:iot-open:espressif_es8266_http_led_control [2023/11/23 10:46] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Controlling LED with Simple Web Server ====== | ||
+ | {{: | ||
+ | A sample Web application hosted on ESP8266 MCU is presented below.\\ | ||
+ | This application allows it to control the state of the LED remotely, connecting to the ESP8266 board with a web browser. The program presented is based on the example " | ||
+ | |||
+ | Assuming the address in the terminal is '' | ||
< | < | ||
+ | http:// | ||
+ | http:// | ||
+ | </ | ||
- | const int led_red = 13; | + | <code c> |
- | pinMode(led_red, | + | #include < |
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #ifndef STASSID | ||
+ | #define STASSID " | ||
+ | #define STAPSK | ||
+ | #endif | ||
+ | |||
+ | const char* ssid = STASSID; | ||
+ | const char* password = STAPSK; | ||
+ | |||
+ | ESP8266WebServer server(80); | ||
+ | |||
+ | const int led = 2; | ||
void handleRoot() { | void handleRoot() { | ||
- | digitalWrite(led, | + | |
- | server.send(200, | + | //so it is required to comment the lines which modify the LED state |
- | digitalWrite(led, | + | //digitalWrite(led, |
+ | server.send(200, | ||
+ | | ||
} | } | ||
- | | + | void handleNotFound() { |
+ | // | ||
+ | String message = "File Not Found\n\n"; | ||
+ | message += "URI: "; | ||
+ | message += server.uri(); | ||
+ | message += " | ||
+ | message += (server.method() == HTTP_GET) ? " | ||
+ | message += " | ||
+ | message += server.args(); | ||
+ | message += " | ||
+ | for (uint8_t i = 0; i < server.args(); | ||
+ | message += " " + server.argName(i) + ": " + server.arg(i) + " | ||
+ | } | ||
+ | server.send(404, | ||
+ | // | ||
+ | } | ||
+ | |||
+ | void setup(void) { | ||
+ | pinMode(led, | ||
+ | // | ||
+ | Serial.begin(115200); | ||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(ssid, | ||
+ | Serial.println("" | ||
+ | |||
+ | // Wait for connection | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | Serial.println("" | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | if (MDNS.begin(" | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | server.on("/", | ||
+ | |||
+ | // request for turning led on | ||
+ | | ||
server.send(200, | server.send(200, | ||
digitalWrite(led, | digitalWrite(led, | ||
}); | }); | ||
+ | // request for turning led off | ||
server.on("/ | server.on("/ | ||
server.send(200, | server.send(200, | ||
Line 22: | Line 91: | ||
}); | }); | ||
+ | server.onNotFound(handleNotFound); | ||
+ | |||
+ | server.begin(); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | void loop(void) { | ||
+ | server.handleClient(); | ||
+ | MDNS.update(); | ||
+ | } | ||
</ | </ | ||