Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:espressif_es8266_http_led_control [2023/10/10 08:13] – created ktokarzen:iot-open:espressif_es8266_http_led_control [2023/11/23 10:46] (current) pczekalski
Line 1: Line 1:
 +====== Controlling LED with Simple Web Server ======
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\
 +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 "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, adding WiFi network credentials and setting the ''led'' constant with the number of GPIO to which the LED is connected is required. After a successful connection to the WiFi, ESP8266 would present through the serial monitor the IP address (e.g. 192.168.4.1). Writing in the address bar in the browser "HTTP://192.168.4.1" should return the serial monitor message "hello from esp8266!".\\
 +
 +Assuming the address in the terminal is ''192.168.4.1'' one may use the following URLs to disable and enable the LED, respectively:
 <code> <code>
 +http://192.168.4.1/LED0 
 +http://192.168.4.1/LED1 
 +</code>
  
-const int led_red = 13;+<code c>
  
-pinMode(led_red, OUTPUT);+#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() { void handleRoot() {
-  digitalWrite(led, 1); +  //Originally LED was controlled for every root request 
-  server.send(200, "text/plain", "hello from esp8266!"); +  //so it is required to comment the lines which modify the LED state 
-  digitalWrite(led, 0);+  //digitalWrite(led, 1); 
 +  server.send(200, "text/plain", "hello from esp8266!\r\n"); 
 +  //digitalWrite(led, 0);
 } }
  
- server.on("/LED1", [](){+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");     server.send(200, "text/plain", "LED is ON");
     digitalWrite(led, 1);     digitalWrite(led, 1);
   });   });
  
 +  // request for turning led off
   server.on("/LED0", [](){   server.on("/LED0", [](){
     server.send(200, "text/plain", "LED is OFF");     server.send(200, "text/plain", "LED is OFF");
Line 22: Line 91:
   });   });
  
 +  server.onNotFound(handleNotFound);
 +
 +  server.begin();
 +  Serial.println("HTTP server started");
 +}
 +
 +void loop(void) {
 +  server.handleClient();
 +  MDNS.update();
 +}
 </code> </code>
  
en/iot-open/espressif_es8266_http_led_control.1696925588.txt.gz · Last modified: 2023/10/10 08:13 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