Advertisement
RaspBar

ESP8266_LED_Webserver.ino

Jul 6th, 2022 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. /*Put your SSID & Password*/
  5. const char* ssid = "mySSID";  // Enter SSID here
  6. const char* password = "myPassword";  //Enter Password here
  7.  
  8. ESP8266WebServer server(80);
  9.  
  10. uint8_t LEDrot = D0;  //GPIO-16 Rot LED1
  11. bool LED1status = LOW;
  12.  
  13. uint8_t LEDblau = D3; //GPIO-0 Blau LED2
  14. bool LED2status = LOW;
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   delay(100);
  19.   pinMode(LEDrot, OUTPUT);
  20.   pinMode(LEDblau, OUTPUT);
  21.  
  22.   Serial.println("Connecting to ");
  23.   Serial.println(ssid);
  24.  
  25.   //connect to your local wi-fi network
  26.   WiFi.begin(ssid, password);
  27.  
  28.   //check wi-fi is connected to wi-fi network
  29.   while (WiFi.status() != WL_CONNECTED) {
  30.   delay(1000);
  31.   Serial.print(".");
  32.   }
  33.   Serial.println("");
  34.   Serial.println("WiFi connected..!");
  35.   Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  36.  
  37.   server.on("/", handle_OnConnect);
  38.   server.on("/led1on", handle_led1on);
  39.   server.on("/led1off", handle_led1off);
  40.   server.on("/led2on", handle_led2on);
  41.   server.on("/led2off", handle_led2off);
  42.   server.onNotFound(handle_NotFound);
  43.  
  44.   server.begin();
  45.   Serial.println("HTTP server started");
  46. }
  47. void loop() {
  48.   server.handleClient();
  49.   if(LED1status)
  50.   {digitalWrite(LEDrot, HIGH);}
  51.   else
  52.   {digitalWrite(LEDrot, LOW);}
  53.  
  54.   if(LED2status)
  55.   {digitalWrite(LEDblau, HIGH);}
  56.   else
  57.   {digitalWrite(LEDblau, LOW);}
  58. }
  59.  
  60. void handle_OnConnect() {
  61.   LED1status = LOW;
  62.   LED2status = LOW;
  63.   Serial.println("LED-Rot Status: OFF | LED-Blau Status: OFF");
  64.   server.send(200, "text/html", SendHTML(LED1status,LED2status));
  65. }
  66.  
  67. void handle_led1on() {
  68.   LED1status = HIGH;
  69.   Serial.println("LED-Rot Status: ON");
  70.   server.send(200, "text/html", SendHTML(true,LED2status));
  71. }
  72.  
  73. void handle_led1off() {
  74.   LED1status = LOW;
  75.   Serial.println("LED-Rot Status: OFF");
  76.   server.send(200, "text/html", SendHTML(false,LED2status));
  77. }
  78.  
  79. void handle_led2on() {
  80.   LED2status = HIGH;
  81.   Serial.println("LED-Blau Status: ON");
  82.   server.send(200, "text/html", SendHTML(LED1status,true));
  83. }
  84.  
  85. void handle_led2off() {
  86.   LED2status = LOW;
  87.   Serial.println("LED-Blau Status: OFF");
  88.   server.send(200, "text/html", SendHTML(LED1status,false));
  89. }
  90.  
  91. void handle_NotFound(){
  92.   server.send(404, "text/plain", "Not found");
  93. }
  94.  
  95. String SendHTML(uint8_t led1stat,uint8_t led2stat){
  96.   String ptr = "<!DOCTYPE html> <html>\n";
  97.   ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  98.   ptr +="<title>LED Control</title>\n";
  99.   ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  100.   ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  101.   ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  102.   ptr +=".button-on {background-color: #1abc9c;}\n";
  103.   ptr +=".button-on:active {background-color: #16a085;}\n";
  104.   ptr +=".button-off {background-color: #34495e;}\n";
  105.   ptr +=".button-off:active {background-color: #2c3e50;}\n";
  106.   ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  107.   ptr +="</style>\n";
  108.   ptr +="</head>\n";
  109.   ptr +="<body>\n";
  110.   ptr +="<h1>ESP8266 Web Server</h1>\n";
  111.     ptr +="<h3>Station(STA) Mode</h3>\n";
  112.  
  113.    if(led1stat)
  114.   {ptr +="<p>LED Rot Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
  115.   else
  116.   {ptr +="<p>LED Rot Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
  117.  
  118.   if(led2stat)
  119.   {ptr +="<p>LED Blau Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
  120.   else
  121.   {ptr +="<p>LED Blau Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
  122.  
  123.   ptr +="</body>\n";
  124.   ptr +="</html>\n";
  125.   return ptr;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement