SHARE
TWEET

ESP-!"E-original

a guest May 23rd, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********
  2.     Complete project details at http://randomnerdtutorials.com  
  3. *********/
  4.  
  5. // Including the ESP8266 WiFi library
  6. #include <ESP8266WiFi.h>
  7.  
  8. // Replace with your network details
  9. const char* ssid = "VTR-9016224";
  10. const char* password = "jhgh5gsJxnct";
  11.  
  12. // Web Server on port 8888
  13. WiFiServer server(8888);
  14.  
  15. // variables
  16. String header;
  17. String gpio5_state = "Off";
  18. String gpio4_state = "Off";
  19. int gpio5_pin = 5;
  20. int gpio4_pin = 4;
  21.  
  22. // only runs once
  23. void setup() {
  24.   // Initializing serial port for debugging purposes
  25.   Serial.begin(115200);
  26.   delay(10);
  27.  
  28.   // preparing GPIOs
  29.   pinMode(gpio5_pin, OUTPUT);
  30.   digitalWrite(gpio5_pin, LOW);
  31.   pinMode(gpio4_pin, OUTPUT);
  32.   digitalWrite(gpio4_pin, LOW);
  33.  
  34.   // Connecting to WiFi network
  35.   Serial.println();
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.  
  39.   WiFi.begin(ssid, password);
  40.  
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.   Serial.println("");
  46.   Serial.println("WiFi connected");
  47.  
  48.   // Starting the web server
  49.   server.begin();
  50.   Serial.println("Web server running. Waiting for the ESP IP...");
  51.   delay(10000);
  52.  
  53.   // Printing the ESP IP address
  54.   Serial.println(WiFi.localIP());
  55. }
  56.  
  57. // runs over and over again
  58. void loop() {
  59.   // Listenning for new clients
  60.   WiFiClient client = server.available();
  61.  
  62.   if (client) {
  63.     Serial.println("New client");
  64.     // boolean to locate when the http request ends
  65.     boolean blank_line = true;
  66.     while (client.connected()) {
  67.       if (client.available()) {
  68.         char c = client.read();
  69.         header += c;
  70.  
  71.         if (c == '\n' && blank_line) {
  72.  
  73.           // checking if header is valid
  74.           // dXNlcjpwYXNz = 'user:pass' (user:pass) base64 encode
  75.           // Z29kbzoyMDE3 = ‘godo:2017´ (godo:2017) base64 codificación
  76.          // https://www.base64encode.org
  77.           Serial.print(header);
  78.          
  79.           // Finding the right credential string
  80.           if(header.indexOf("Z29kbzoyMDE3") >= 0) {
  81.             //successful login
  82.             client.println("HTTP/1.1 200 OK");
  83.             client.println("Content-Type: text/html");
  84.             client.println("Connection: close");
  85.             client.println();
  86.             // turns the GPIOs on and off
  87.             if(header.indexOf("GET / HTTP/1.1") >= 0) {
  88.                 Serial.println("Main Web Page");
  89.             }  
  90.             else if(header.indexOf("GET /gpio5on HTTP/1.1") >= 0){
  91.                 Serial.println("GPIO 5 On");
  92.                 gpio5_state = "On";
  93.                 digitalWrite(gpio5_pin, HIGH);
  94.             }
  95.             else if(header.indexOf("GET /gpio5off HTTP/1.1") >= 0){
  96.                 Serial.println("GPIO 5 Off");
  97.                 gpio5_state = "Off";
  98.                 digitalWrite(gpio5_pin, LOW);
  99.             }
  100.             else if(header.indexOf("GET /gpio4on HTTP/1.1") >= 0){
  101.                 Serial.println("GPIO 4 On");
  102.                 gpio4_state = "On";
  103.                 digitalWrite(gpio4_pin, HIGH);
  104.             }
  105.             else if(header.indexOf("GET /gpio4off HTTP/1.1") >= 0){
  106.                 Serial.println("GPIO 4 Off");
  107.                 gpio4_state = "Off";
  108.                 digitalWrite(gpio4_pin, LOW);
  109.             }
  110.             // your web page
  111.             client.println("<!DOCTYPE HTML>");
  112.             client.println("<html>");
  113.             client.println("<head>");
  114.             client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  115.             client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");                                                                    
  116.             client.println("</head><div class=\"container\">");
  117.             client.println("<h1>Web Server</h1>");
  118.             client.println("<h2>GPIO 5 - Current State: " + gpio5_state);          
  119.             client.println("<div class=\"row\">");
  120.             client.println("<div class=\"col-md-2\"><a href=\"/gpio5on\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div>");
  121.             client.println("<div class=\"col-md-2\"><a href=\"/gpio5off\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div>");                                                                    
  122.             client.println("</div>");
  123.             client.println("<h2>GPIO 4 - Current State: " + gpio4_state);      
  124.             client.println("<div class=\"row\">");
  125.             client.println("<div class=\"col-md-2\"><a href=\"/gpio4on\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div>");                                                                    
  126.             client.println("<div class=\"col-md-2\"><a href=\"/gpio4off\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div>");
  127.             client.println("</div></div></html>");    
  128.           }
  129.        // wrong user or passm, so http request fails...  
  130.         else {            
  131.            client.println("HTTP/1.1 401 Unauthorized");
  132.            client.println("WWW-Authenticate: Basic realm=\"Secure\"");
  133.            client.println("Content-Type: text/html");
  134.            client.println();
  135.            client.println("<html>Authentication failed</html>");
  136.         }  
  137.         header = "";
  138.         break;
  139.         }
  140.         if (c == '\n') {
  141.           // when starts reading a new line
  142.           blank_line = true;
  143.         }
  144.         else if (c != '\r') {
  145.           // when finds a character on the current line
  146.           blank_line = false;
  147.         }
  148.       }
  149.     }  
  150.     // closing the client connection
  151.     delay(1);
  152.     client.stop();
  153.     Serial.println("Client disconnected.");
  154.   }
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top