skizziks_53

NodeMCU hotspot 2-pin switch v1.0

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