Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3.  
  4. //ESP Web Server Library to host a web page
  5. #include <ESP8266WebServer.h>
  6.  
  7. //---------------------------------------------------------------
  8. //Our HTML webpage contents in program memory
  9. const char MAIN_page[] PROGMEM = R"=====(
  10. <!DOCTYPE html>
  11. <html>
  12. <body>
  13. <center>
  14. <h1>WiFi LED on off demo: 1</h1><br>
  15. Ciclk to turn <a href="ledOn">LED ON</a><br>
  16. Ciclk to turn <a href="ledOff">LED OFF</a><br>
  17. <hr>
  18. </center>
  19.  
  20. </body>
  21. </html>
  22. )=====";
  23. //---------------------------------------------------------------
  24. //On board LED Connected to GPIO2
  25. #define LED 2
  26.  
  27. //SSID and Password of your WiFi router
  28. const char* ssid = "kamilwifi";
  29. const char* password = "potato123";
  30.  
  31. //Declare a global object variable from the ESP8266WebServer class.
  32. ESP8266WebServer server(80); //Server on port 80
  33.  
  34. //===============================================================
  35. // This routine is executed when you open its IP in browser
  36. //===============================================================
  37. void handleRoot() {
  38. Serial.println("You called root page");
  39. String s = MAIN_page; //Read HTML contents
  40. server.send(200, "text/html", s); //Send web page
  41. }
  42.  
  43. void handleLEDon() {
  44. Serial.println("LED on page");
  45. digitalWrite(LED,LOW); //LED is connected in reverse
  46. server.send(200, "text/html", "LED is ON"); //Send ADC value only to client ajax request
  47. }
  48.  
  49. void handleLEDoff() {
  50. Serial.println("LED off page");
  51. digitalWrite(LED,HIGH); //LED off
  52. server.send(200, "text/html", "LED is OFF"); //Send ADC value only to client ajax request
  53. }
  54. //==============================================================
  55. // SETUP
  56. //==============================================================
  57. void setup(void){
  58. Serial.begin(9600);
  59.  
  60. WiFi.begin(ssid, password); //Connect to your WiFi router
  61. Serial.println("");
  62.  
  63. //Onboard LED port Direction output
  64. pinMode(LED,OUTPUT);
  65. //Power on LED state off
  66. digitalWrite(LED,HIGH);
  67.  
  68. // Wait for connection
  69. while (WiFi.status() != WL_CONNECTED) {
  70. delay(500);
  71. Serial.print(".");
  72. }
  73.  
  74. //If connection successful show IP address in serial monitor
  75. Serial.println("");
  76. Serial.print("Connected to ");
  77. Serial.println(ssid);
  78. Serial.print("IP address: ");
  79. Serial.println(WiFi.localIP()); //IP address assigned to your ESP
  80.  
  81. server.on("/", handleRoot); //Which routine to handle at root location. This is display page
  82. server.on("/ledOn", handleLEDon); //as Per <a href="ledOn">, Subroutine to be called
  83. server.on("/ledOff", handleLEDoff);
  84.  
  85. server.begin(); //Start server
  86. Serial.println("HTTP server started");
  87. }
  88. //==============================================================
  89. // LOOP
  90. //==============================================================
  91. void loop(void){
  92. server.handleClient(); //Handle client requests
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement