godo2017

SP8266_01

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