safwan092

esp32 simple webserver 2 leds

Jun 17th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None | 0 0
  1. /*********
  2.   Rui Santos
  3.   Complete project details at https://randomnerdtutorials.com  
  4. *********/
  5.  
  6. // Load Wi-Fi library
  7. #include <WiFi.h>
  8.  
  9. // Replace with your network credentials
  10. const char* ssid = "Network";
  11. const char* password = "123456789";
  12.  
  13. // Set web server port number to 80
  14. WiFiServer server(80);
  15.  
  16. // Variable to store the HTTP request
  17. String header;
  18.  
  19. // Auxiliar variables to store the current output state
  20. String output26State = "off";
  21. String output27State = "off";
  22.  
  23. // Assign output variables to GPIO pins
  24. const int output26 = 26;
  25. const int output27 = 27;
  26.  
  27. // Current time
  28. unsigned long currentTime = millis();
  29. // Previous time
  30. unsigned long previousTime = 0;
  31. // Define timeout time in milliseconds (example: 2000ms = 2s)
  32. const long timeoutTime = 2000;
  33.  
  34. void setup() {
  35.   Serial.begin(115200);
  36.   // Initialize the output variables as outputs
  37.   pinMode(output26, OUTPUT);
  38.   pinMode(output27, OUTPUT);
  39.   // Set outputs to LOW
  40.   digitalWrite(output26, LOW);
  41.   digitalWrite(output27, LOW);
  42.  
  43.   // Connect to Wi-Fi network with SSID and password
  44.   Serial.print("Connecting to ");
  45.   Serial.println(ssid);
  46.   WiFi.begin(ssid, password);
  47.   while (WiFi.status() != WL_CONNECTED) {
  48.     delay(500);
  49.     Serial.print(".");
  50.   }
  51.   // Print local IP address and start web server
  52.   Serial.println("");
  53.   Serial.println("WiFi connected.");
  54.   Serial.println("IP address: ");
  55.   Serial.println(WiFi.localIP());
  56.   server.begin();
  57. }
  58.  
  59. void loop(){
  60.   WiFiClient client = server.available();   // Listen for incoming clients
  61.  
  62.   if (client) {                             // If a new client connects,
  63.     currentTime = millis();
  64.     previousTime = currentTime;
  65.     Serial.println("New Client.");          // print a message out in the serial port
  66.     String currentLine = "";                // make a String to hold incoming data from the client
  67.     while (client.connected() && currentTime - previousTime <= timeoutTime) {  // loop while the client's connected
  68.       currentTime = millis();
  69.       if (client.available()) {             // if there's bytes to read from the client,
  70.         char c = client.read();             // read a byte, then
  71.         Serial.write(c);                    // print it out the serial monitor
  72.         header += c;
  73.         if (c == '\n') {                    // if the byte is a newline character
  74.           // if the current line is blank, you got two newline characters in a row.
  75.           // that's the end of the client HTTP request, so send a response:
  76.           if (currentLine.length() == 0) {
  77.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  78.             // and a content-type so the client knows what's coming, then a blank line:
  79.             client.println("HTTP/1.1 200 OK");
  80.             client.println("Content-type:text/html");
  81.             client.println("Connection: close");
  82.             client.println();
  83.            
  84.             // turns the GPIOs on and off
  85.             if (header.indexOf("GET /26/on") >= 0) {
  86.               Serial.println("GPIO 26 on");
  87.               output26State = "on";
  88.               digitalWrite(output26, HIGH);
  89.             } else if (header.indexOf("GET /26/off") >= 0) {
  90.               Serial.println("GPIO 26 off");
  91.               output26State = "off";
  92.               digitalWrite(output26, LOW);
  93.             } else if (header.indexOf("GET /27/on") >= 0) {
  94.               Serial.println("GPIO 27 on");
  95.               output27State = "on";
  96.               digitalWrite(output27, HIGH);
  97.             } else if (header.indexOf("GET /27/off") >= 0) {
  98.               Serial.println("GPIO 27 off");
  99.               output27State = "off";
  100.               digitalWrite(output27, LOW);
  101.             }
  102.            
  103.             // Display the HTML web page
  104.             client.println("<!DOCTYPE html><html>");
  105.             client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  106.             client.println("<link rel=\"icon\" href=\"data:,\">");
  107.             // CSS to style the on/off buttons
  108.             // Feel free to change the background-color and font-size attributes to fit your preferences
  109.             client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  110.             client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
  111.             client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  112.             client.println(".button2 {background-color: #555555;}</style></head>");
  113.            
  114.             // Web Page Heading
  115.             client.println("<body><h1>ESP32 Web Server</h1>");
  116.            
  117.             // Display current state, and ON/OFF buttons for GPIO 26  
  118.             client.println("<p>GPIO 26 - State " + output26State + "</p>");
  119.             // If the output26State is off, it displays the ON button      
  120.             if (output26State=="off") {
  121.               client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
  122.             } else {
  123.               client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
  124.             }
  125.                
  126.             // Display current state, and ON/OFF buttons for GPIO 27  
  127.             client.println("<p>GPIO 27 - State " + output27State + "</p>");
  128.             // If the output27State is off, it displays the ON button      
  129.             if (output27State=="off") {
  130.               client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
  131.             } else {
  132.               client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
  133.             }
  134.             client.println("</body></html>");
  135.            
  136.             // The HTTP response ends with another blank line
  137.             client.println();
  138.             // Break out of the while loop
  139.             break;
  140.           } else { // if you got a newline, then clear currentLine
  141.             currentLine = "";
  142.           }
  143.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  144.           currentLine += c;      // add it to the end of the currentLine
  145.         }
  146.       }
  147.     }
  148.     // Clear the header variable
  149.     header = "";
  150.     // Close the connection
  151.     client.stop();
  152.     Serial.println("Client disconnected.");
  153.     Serial.println("");
  154.   }
  155. }
Add Comment
Please, Sign In to add comment