SHARE
TWEET

Esp8266 Web Server TimeLEd

Regela Feb 26th, 2020 (edited) 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. const char* ssid = "WiFi";
  5. const char* password = "Password";
  6. int led1=4,led2=5;
  7. ESP8266WebServer server(80);
  8. String head = "<html><head><title>LED</title><meta charset='utf-8'></head><body>";
  9. String tail = "</body></html>";
  10. void handleRoot() {
  11.   String s = "<form action='/LedOn1'> <input type='text' name='time' />"
  12.   " <input type='submit' value='Led1 On'/> </form>"
  13.   "<form action='/LedOn2'> <input type='text' name='time' />"
  14.   " <input type='submit' value='Led2 On'/> </form>";
  15.   server.send(200, "text/html", head + s + tail);
  16. }
  17. void ledOn1() {
  18.   int time =  server.arg(0).toInt();
  19.   server.send(200, "text/plain",  "OK" );
  20.   digitalWrite(led1,1);
  21.   delay(time*1000);
  22.   digitalWrite(led1,0);
  23. }
  24. void ledOn2() {
  25.   int time =  server.arg(0).toInt();
  26.   server.send(200, "text/plain",  "OK" );
  27.   digitalWrite(led2,1);
  28.   delay(time*1000);
  29.   digitalWrite(led2,0);
  30. }
  31. void setup(void) {
  32.   pinMode(led1,OUTPUT);
  33.   pinMode(led2,OUTPUT);
  34.   Serial.begin(115200);
  35.   WiFi.mode(WIFI_STA);
  36.   WiFi.begin(ssid, password);
  37.   // Wait for connection
  38.   while (WiFi.status() != WL_CONNECTED) {
  39.     delay(500);
  40.     Serial.print(".");
  41.   }
  42.   Serial.println(WiFi.localIP());
  43.   server.on("/", handleRoot);
  44.   server.on("/LedOn1", ledOn1);
  45.   server.on("/LedOn2", ledOn2);
  46.   server.begin();
  47.   Serial.println("HTTP server started");
  48. }
  49. void loop(void) {
  50.   server.handleClient();
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top