Advertisement
GuillemLtd

Untitled

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