skizziks_53

NodeMCU hotspot 2-pin switch v1.0

Nov 24th, 2019
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.17 KB | None | 0 0
  1. /*
  2.    NodeMCU stand-alone wifi hot spot - v1.0
  3.    November 24, 2019
  4.  
  5.    This sketch is a combination of two others:
  6.    https://randomnerdtutorials.com/esp8266-web-server/ (a wifi/web page that controls 2 I/O pins)
  7.    https://tttapa.github.io/ESP8266/Chap07%20-%20Wi-Fi%20Connections.html (see "Access Point Mode")
  8.  
  9.    What it does: it creates a stand-alone access point that allows controlling 2 I/O pins.
  10.  
  11.    It works but is kinda slow.
  12. */
  13.  
  14. #include <ESP8266WiFi.h>
  15. #include <WiFiClient.h>
  16. #include <ESP8266WebServer.h>
  17.  
  18. const char *ssid = "esp8266stn"; // ------ (8 chars minimum?)
  19. const char *password = "asdf1234"; // ------- (8 chars minimum?)
  20.  
  21. WiFiServer server(80);
  22.  
  23. String header;
  24.  
  25. String output5State = "off";
  26. String output4State = "off";
  27.  
  28. const int output5 = 5;
  29. const int output4 = 4;
  30.  
  31. unsigned long currentTime = millis();
  32. unsigned long previousTime = 0;
  33.  
  34. const long timeoutTime = 2000;
  35.  
  36.  
  37.  
  38. void setup() {
  39.   Serial.begin(115200);
  40.   Serial.println();
  41.   Serial.print("Configuring access point...");
  42.   /* You can remove the password parameter if you want the AP to be open. */
  43.   WiFi.softAP(ssid, password);
  44.   server.begin();
  45.   Serial.println("HTTP server started");
  46.  
  47.   pinMode(output5, OUTPUT);
  48.   pinMode(output4, OUTPUT);
  49.   digitalWrite(output5, LOW);
  50.   digitalWrite(output4, LOW);
  51. }
  52.  
  53. void loop() {
  54.   WiFiClient client = server.available();   // Listen for incoming clients
  55.   if (client) {                             // If a new client connects,
  56.     Serial.println("New Client.");          // print a message out in the serial port
  57.     String currentLine = "";                // make a String to hold incoming data from the client
  58.     currentTime = millis();
  59.     previousTime = currentTime;
  60.     while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  61.       currentTime = millis();
  62.       if (client.available()) {             // if there's bytes to read from the client,
  63.         char c = client.read();             // read a byte, then
  64.         Serial.write(c);                    // print it out the serial monitor
  65.         header += c;
  66.         if (c == '\n') {                    // if the byte is a newline character
  67.           // if the current line is blank, you got two newline characters in a row.
  68.           // that's the end of the client HTTP request, so send a response:
  69.           if (currentLine.length() == 0) {
  70.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  71.             // and a content-type so the client knows what's coming, then a blank line:
  72.             client.println("HTTP/1.1 200 OK");
  73.             client.println("Content-type:text/html");
  74.             client.println("Connection: close");
  75.             client.println();
  76.  
  77.             // turns the GPIOs on and off
  78.             if (header.indexOf("GET /5/on") >= 0) {
  79.               Serial.println("GPIO 5 on");
  80.               output5State = "on";
  81.               digitalWrite(output5, HIGH);
  82.             } else if (header.indexOf("GET /5/off") >= 0) {
  83.               Serial.println("GPIO 5 off");
  84.               output5State = "off";
  85.               digitalWrite(output5, LOW);
  86.             } else if (header.indexOf("GET /4/on") >= 0) {
  87.               Serial.println("GPIO 4 on");
  88.               output4State = "on";
  89.               digitalWrite(output4, HIGH);
  90.             } else if (header.indexOf("GET /4/off") >= 0) {
  91.               Serial.println("GPIO 4 off");
  92.               output4State = "off";
  93.               digitalWrite(output4, LOW);
  94.             }
  95.  
  96.             // Display the HTML web page
  97.             client.println("<!DOCTYPE html><html>");
  98.             client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  99.             client.println("<link rel=\"icon\" href=\"data:,\">");
  100.             // CSS to style the on/off buttons
  101.             // Feel free to change the background-color and font-size attributes to fit your preferences
  102.             client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  103.             client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  104.             client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  105.             client.println(".button2 {background-color: #77878A;}</style></head>");
  106.  
  107.             // Web Page Heading
  108.             client.println("<body><h1>ESP8266 Web Server</h1>");
  109.  
  110.             // Display current state, and ON/OFF buttons for GPIO 5
  111.             client.println("<p>GPIO 5 - State " + output5State + "</p>");
  112.             // If the output5State is off, it displays the ON button
  113.             if (output5State == "off") {
  114.               client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
  115.             } else {
  116.               client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
  117.             }
  118.  
  119.             // Display current state, and ON/OFF buttons for GPIO 4
  120.             client.println("<p>GPIO 4 - State " + output4State + "</p>");
  121.             // If the output4State is off, it displays the ON button
  122.             if (output4State == "off") {
  123.               client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
  124.             } else {
  125.               client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
  126.             }
  127.             client.println("</body></html>");
  128.  
  129.             // The HTTP response ends with another blank line
  130.             client.println();
  131.             // Break out of the while loop
  132.             break;
  133.           } else { // if you got a newline, then clear currentLine
  134.             currentLine = "";
  135.           }
  136.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  137.           currentLine += c;      // add it to the end of the currentLine
  138.         }
  139.       }
  140.     }
  141.     // Clear the header variable
  142.     header = "";
  143.     // Close the connection
  144.     client.stop();
  145.     Serial.println("Client disconnected.");
  146.     Serial.println("");
  147.   }
  148.  
  149. }
  150.  
  151.  
  152. // ~~~~~~~~ the end ~~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment