diff --git a/libraries/ESP8266WebServer/examples/LedSwitch/LedSwitch.ino b/libraries/ESP8266WebServer/examples/LedSwitch/LedSwitch.ino new file mode 100644 index 0000000000..d0c30302ab --- /dev/null +++ b/libraries/ESP8266WebServer/examples/LedSwitch/LedSwitch.ino @@ -0,0 +1,92 @@ +#include +#include +#include + + +/* Put your SSID & Password */ +const char* ssid = "put your wifi name"; // Enter SSID here +const char* password = "input your wifi password"; //Enter Password here + + +ESP8266WebServer server(80); + +uint8_t ledpin = 2;// connect your led to GPIO 2 of the esp +bool ledstatus = LOW; + + +void setup() { + Serial.begin(115200); + pinMode(ledpin, OUTPUT); + + + WiFi.softAP(ssid, password); // remove password if your want it to be open + IPAddress ip = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(ip); + + server.on("/", handle_OnConnect); + server.on("/ledon", handle_ledon); + server.on("/ledoff", handle_ledoff); + server.onNotFound(handle_NotFound); + + server.begin(); + Serial.println("HTTP server started"); +} +void loop(){ + server.handleClient(); + if(ledstatus){ + digitalWrite(ledpin, HIGH); + } + else{ + digitalWrite(ledpin, LOW); + } +} + +void handle_OnConnect() { + ledstatus = LOW; + Serial.println("led status is off"); + server.send(200, "text/html", SendHTML(ledstatus)); +} + +void handle_ledon() { + ledstatus = HIGH; + Serial.println("led status is on"); + server.send(200, "text/html", SendHTML(true)); +} + +void handle_ledoff() { + ledstatus = LOW; + Serial.println("led status is off"); + server.send(200, "text/html", SendHTML(false)); +} + +void handle_NotFound() { + server.send(404, "text/plain", "404: Not found"); +} + +String SendHTML(uint8_t ledstat){ + String ptr = "\n"; + ptr += "\n"; + ptr += "ESP8266 Web Server\n"; + ptr += "\n"; + ptr += "\n"; + ptr += "

ESP8266 Web Server

\n"; + ptr += "

Using access point

\n"; + + if(ledstat){ + ptr += "

LED is on

Turn LED on\n"; + } + else{ + ptr += "

LED is off

Turn LED off\n"; + } + ptr += "\n"; + return ptr; +}