Advertisement
MrRockchip

blinker_part8266_webserver

Nov 9th, 2021
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. /* Set up your SSID and Password */
  5. const char*      ssid = "NodeMCU"; // SSID
  6. const char* password = "19216811"; // Password
  7.  
  8. /* Set up your IP addresses */
  9. IPAddress local_ip(192,168,1,1);
  10. IPAddress  gateway(192,168,1,1);
  11. IPAddress subnet(255,255,255,0);
  12.  
  13. ESP8266WebServer server(80);
  14.  
  15. bool LED1status = LOW;
  16.  
  17. // Initial setup
  18. void setup()
  19. {
  20.  /*
  21.   * It is recommended to use the lower baud rate. The higher the baud rate,
  22.   * the higher the bit error rate will be, and the control action might fail.
  23.   */
  24.   Serial.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  25.  
  26.   WiFi.softAP(ssid, password);
  27.   WiFi.softAPConfig(local_ip, gateway, subnet);
  28.  
  29.   delay(100);
  30.  
  31.   server.on("/",        handle_OnConnect);
  32.   server.on("/led1on",  handle_led1on);
  33.   server.on("/led1off", handle_led1off);
  34.   server.onNotFound(handle_NotFound);
  35.  
  36.   server.begin();
  37.   //// Serial.println("HTTP server started");
  38. }
  39.  
  40. // The loop function runs over and over again forever
  41. void loop()
  42. {
  43.   server.handleClient();
  44.   if (LED1status)
  45.     Serial.print("1"); // Command for MEGA2560 to turn on  MEGA2560's L LED
  46.   else
  47.     Serial.print("0"); // Command for MEGA2560 to turn off MEGA2560's L LED
  48. }
  49.  
  50. void handle_OnConnect()
  51. {
  52.   /*
  53.    * Serial.print("LED1status: ");
  54.    * if (LED1status)
  55.    *   Serial.println("ON");
  56.    * else
  57.    *   Serial.println("OFF");
  58.    */
  59.   server.send(200, "text/html", SendHTML(LED1status));
  60. }
  61.  
  62. void handle_led1on()
  63. {
  64.   LED1status = HIGH;
  65.   //// Serial.println("LED1status: ON");
  66.   server.send(200, "text/html", SendHTML(true));
  67. }
  68.  
  69. void handle_led1off()
  70. {
  71.   LED1status = LOW;
  72.   //// Serial.println("LED1status: OFF");
  73.   server.send(200, "text/html", SendHTML(false));
  74. }
  75.  
  76. void handle_NotFound()
  77. {
  78.   server.send(404, "text/plain", "ERROR 404: Not Found");
  79. }
  80.  
  81. String SendHTML(uint8_t led1stat)
  82. {
  83.   String ptr = "";
  84.   ptr += "<!DOCTYPE html>\n";
  85.   ptr += "<html>\n";
  86.   ptr += "<head>\n";
  87.   ptr += "<meta charset=\"utf-8\">\n";
  88.   ptr += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  89.   ptr += "<title>LED Control</title>\n";
  90.   ptr += "<style>html { background-color: white; color: black; display: inline-block; font-family: \"Liberation Mono\", monospace; font-size: 20px; text-align: center; position: absolute; left: 0px; }\n";
  91.   ptr += "body { color: black; }\n";
  92.   ptr += "h1 { color: darkgray; }\n";
  93.   ptr += "h2 { color: gray; }\n";
  94.   ptr += "h3 { color: lightgray; }\n";
  95.   ptr += ".button { background-color: white; color: white; border: none; border-radius: 5px; cursor: pointer; text-decoration: none; margin: 5px 5px 5px 5px; padding: 3px 3px 3px 3px; }\n";
  96.   ptr += ".button-on { background-color: green; }\n";
  97.   ptr += ".button-on:active { background-color: darkgreen; }\n";
  98.   ptr += ".button-off { background-color: blue; }\n";
  99.   ptr += ".button-off:active { background-color: darkblue; }\n";
  100.   ptr += "p { font-size: 15px; }\n";
  101.   ptr += "</style>\n";
  102.   ptr += "</head>\n";
  103.   ptr += "<body>\n";
  104.   //// ptr += "<h1>ESP8266 Web Server</h1>\n";
  105.   //// ptr += "<h3>Using Access Point(AP) Mode</h3>\n";
  106.  
  107.   if (led1stat)
  108.     ptr += "<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\"><button>OFF</button></a>\n";
  109.   else
  110.     ptr += "<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\"><button>ON</button></a>\n";
  111.  
  112.   ptr += "</body>\n";
  113.   ptr += "</html>\n";
  114.   return ptr;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement