skizziks_53

NodeMCU WIFI control V2.0

Jul 21st, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. /*
  2.   November 25, 2019
  3.   nodemcu_experiment_005
  4.   NodeMCU stand-alone hot spot/web server to control I/O pins
  5.  
  6.   Code copied from:
  7.   https://lastminuteengineers.com/creating-esp8266-web-server-arduino-ide/
  8.  
  9.   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  10.  
  11.   Notes:
  12.   1. This sketch is for a NodeMCU board.
  13.  
  14.   2. It allows you to control two of the I/O pins, using the web browser on a mobile phone.
  15.  
  16.   3. The NodeMCU board acts as a stand-alone hot spot, and serves a web page with buttons on it that you view in your phone's browser.
  17.       The page text should change when you change the button states.
  18.  
  19.   4. Once the sketch is loaded on the NodeMCU and the NodeMCU is running, this is how you connect to it:
  20.       A. You open your phone's wifi control panel, and you should see the name that is contained in the variable 'ssid' below.
  21.       B. When you try to connect, this sketch asks for a password. That is shown below in the variable 'password'.
  22.       C. Once connected, you enter the address { 192.168.1.1 } into your phone's browser address bar and hit [go/enter] in the browser. Then you should see the web page.
  23.  
  24.   5. You can store the NodeMCU wifi info in your phone's wifi settings list, so that you don't need to re-enter it again.
  25.  
  26.   6. You can store the home page address (192.168.1.1) as a bookmark in your phone's browser, so that you don't need to re-enter it again.
  27.  
  28.   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  29. */
  30.  
  31. #include <ESP8266WiFi.h>
  32. #include <ESP8266WebServer.h>
  33.  
  34. /// Put your SSID & Password (this is the hotspot name and password that you want the NodeMCU to have)
  35. const char* ssid = "NodeMCU";  // Enter SSID here
  36. const char* password = "12345678";  //Enter Password here
  37.  
  38. /// Put IP Address details
  39. IPAddress local_ip(192, 168, 1, 1);
  40. IPAddress gateway(192, 168, 1, 1);
  41. IPAddress subnet(255, 255, 255, 0);
  42.  
  43. ESP8266WebServer server(80);
  44.  
  45. uint8_t LED1pin = D7;
  46. bool LED1status = LOW;
  47.  
  48. uint8_t LED2pin = D6;
  49. bool LED2status = LOW;
  50.  
  51. void setup() {
  52.   Serial.begin(115200);
  53.   pinMode(LED1pin, OUTPUT);
  54.   pinMode(LED2pin, OUTPUT);
  55.  
  56.   WiFi.softAP(ssid, password);
  57.   WiFi.softAPConfig(local_ip, gateway, subnet);
  58.   delay(100);
  59.  
  60.   server.on("/", handle_OnConnect);
  61.   server.on("/led1on", handle_led1on);
  62.   server.on("/led1off", handle_led1off);
  63.   server.on("/led2on", handle_led2on);
  64.   server.on("/led2off", handle_led2off);
  65.   server.onNotFound(handle_NotFound);
  66.  
  67.   server.begin();
  68.   Serial.println("HTTP server started");
  69. }
  70. void loop() {
  71.   server.handleClient();
  72.   if (LED1status)
  73.   {
  74.     digitalWrite(LED1pin, HIGH);
  75.   }
  76.   else
  77.   {
  78.     digitalWrite(LED1pin, LOW);
  79.   }
  80.  
  81.   if (LED2status)
  82.   {
  83.     digitalWrite(LED2pin, HIGH);
  84.   }
  85.   else
  86.   {
  87.     digitalWrite(LED2pin, LOW);
  88.   }
  89. }
  90.  
  91. void handle_OnConnect() {
  92.   LED1status = LOW;
  93.   LED2status = LOW;
  94.   Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
  95.   server.send(200, "text/html", SendHTML(LED1status, LED2status));
  96. }
  97.  
  98. void handle_led1on() {
  99.   LED1status = HIGH;
  100.   Serial.println("GPIO7 Status: ON");
  101.   server.send(200, "text/html", SendHTML(true, LED2status));
  102. }
  103.  
  104. void handle_led1off() {
  105.   LED1status = LOW;
  106.   Serial.println("GPIO7 Status: OFF");
  107.   server.send(200, "text/html", SendHTML(false, LED2status));
  108. }
  109.  
  110. void handle_led2on() {
  111.   LED2status = HIGH;
  112.   Serial.println("GPIO6 Status: ON");
  113.   server.send(200, "text/html", SendHTML(LED1status, true));
  114. }
  115.  
  116. void handle_led2off() {
  117.   LED2status = LOW;
  118.   Serial.println("GPIO6 Status: OFF");
  119.   server.send(200, "text/html", SendHTML(LED1status, false));
  120. }
  121.  
  122. void handle_NotFound() {
  123.   server.send(404, "text/plain", "Not found");
  124. }
  125.  
  126. String SendHTML(uint8_t led1stat, uint8_t led2stat) {
  127.   String ptr = "<!DOCTYPE html> <html>\n";
  128.   ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  129.   ptr += "<title>LED Control</title>\n";
  130.   ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  131.   ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  132.   ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  133.   ptr += ".button-on {background-color: #1abc9c;}\n";
  134.   ptr += ".button-on:active {background-color: #16a085;}\n";
  135.   ptr += ".button-off {background-color: #34495e;}\n";
  136.   ptr += ".button-off:active {background-color: #2c3e50;}\n";
  137.   ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  138.   ptr += "</style>\n";
  139.   ptr += "</head>\n";
  140.   ptr += "<body>\n";
  141.   ptr += "<h1>ESP8266 Web Server</h1>\n";
  142.   ptr += "<h3>Using Access Point(AP) Mode</h3>\n";
  143.  
  144.   if (led1stat)
  145.   {
  146.     ptr += "<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";
  147.   }
  148.   else
  149.   {
  150.     ptr += "<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";
  151.   }
  152.  
  153.   if (led2stat)
  154.   {
  155.     ptr += "<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";
  156.   }
  157.   else
  158.   {
  159.     ptr += "<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";
  160.   }
  161.  
  162.   ptr += "</body>\n";
  163.   ptr += "</html>\n";
  164.   return ptr;
  165. }
Add Comment
Please, Sign In to add comment