This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:espressif_es8266_http_led_control [2023/10/10 12:03] – ktokarz | en:iot-open:espressif_es8266_http_led_control [2023/11/23 12: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:// | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | #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() { | ||
| + | // | ||
| + | //so it is required to comment the lines which modify the LED state | ||
| + | // | ||
| + | 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.on("/ | ||
| + | server.send(200, | ||
| + | digitalWrite(led, | ||
| + | }); | ||
| + | |||
| + | // request for turning led off | ||
| + | server.on("/ | ||
| + | server.send(200, | ||
| + | digitalWrite(led, | ||
| + | }); | ||
| + | |||
| + | server.onNotFound(handleNotFound); | ||
| + | |||
| + | server.begin(); | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | void loop(void) { | ||
| + | server.handleClient(); | ||
| + | MDNS.update(); | ||
| + | } | ||
| + | </ | ||