Advertisement
Guest User

Untitled

a guest
Mar 5th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.37 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 <ESP8266WiFi.h>
  8.  
  9. // Replace with your network credentials
  10. const char* ssid     = "*****";
  11. const char* password = "*****";
  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 output5State = "off";
  21. String output4State = "off";
  22.  
  23. // Assign output variables to GPIO pins
  24. // const int output5 = 5;
  25. const int output4 = 2;
  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(output5, OUTPUT);
  38.   pinMode(output4, OUTPUT);
  39.   // Set outputs to LOW
  40.   //digitalWrite(output5, LOW);
  41.   digitalWrite(output4, 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.     Serial.println("New Client.");          // print a message out in the serial port
  64.     String currentLine = "";                // make a String to hold incoming data from the client
  65.     currentTime = millis();
  66.     previousTime = currentTime;
  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 /5/on") >= 0) {
  86.               Serial.println("GPIO 5 on");
  87.               output5State = "on";
  88.               //digitalWrite(output5, HIGH);
  89.             } else if (header.indexOf("GET /5/off") >= 0) {
  90.               Serial.println("GPIO 5 off");
  91.               output5State = "off";
  92.               //digitalWrite(output5, LOW);
  93.             } else if (header.indexOf("GET /4/on") >= 0) {
  94.               Serial.println("GPIO 4 on");
  95.               output4State = "on";
  96.               digitalWrite(output4, HIGH);
  97.             } else if (header.indexOf("GET /4/off") >= 0) {
  98.               Serial.println("GPIO 4 off");
  99.               output4State = "off";
  100.               digitalWrite(output4, 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: #195B6A; 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: #77878A;}</style></head>");
  113.            
  114.             // Web Page Heading
  115.             client.println("<body><h1>ESP8266 Web Server</h1>");
  116.            
  117.             // Display current state, and ON/OFF buttons for GPIO 5  
  118.             client.println("<p>GPIO 5 - State " + output5State + "</p>");
  119.             // If the output5State is off, it displays the ON button      
  120.             if (output5State=="off") {
  121.               client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
  122.             } else {
  123.               client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
  124.             }
  125.                
  126.             // Display current state, and ON/OFF buttons for GPIO 4  
  127.             client.println("<p>GPIO 4 - State " + output4State + "</p>");
  128.             // If the output4State is off, it displays the ON button      
  129.             if (output4State=="off") {
  130.               client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
  131.             } else {
  132.               client.println("<p><a href=\"/4/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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement