Advertisement
KenArduino

ESP IP Button v1.01

Jan 16th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. /* This program is a mash up of the example given in Rui Santos'
  2. * Home Automation course, some code I found at
  3. * https://tttapa.github.io/ESP8266/Chap10%20-%20Simple%20Web%20Server.html
  4. * (Be sure to read the whole beginner's guide https://tttapa.github.io) for
  5. * tons of good info! EXCELLENT!) and I used some html code found on the web,
  6. * that I cannot seem to find again. The latter taught me to put the web
  7. * pagebutton into a string * variable, and send it as a string...good idea!
  8. *
  9. * Rui's code allowed toggling pins on an ESP8266 from a web page
  10. * served from the ESP, as did Pieter's little GitHub Page from the link above,
  11. * but the pages did not include a pin status on the control webpage after it
  12. * updated. With a bit of tweaking, I now have this feature running on an
  13. * ESP8266-07, and I am quite thrilled! :) It simply controls the
  14. * built in LED on the board, (the first step to controlling the WORLD!),
  15. * and the page notifies you of the current state of the LED (on/off).
  16. *
  17. * Thanks to those that made this possible!!
  18. *
  19. * Please feel free to use this code as you wish.
  20. * 2018-01-14 DK Posted on Facebook by ‎Dave Kosewick https://www.facebook.com/dave.kosewick
  21. * 2018-01-16 KJK Added code for user authentication. Search for KJK1.
  22. */
  23.  
  24. #include <ESP8266WiFi.h>
  25. #include <WiFiClient.h>
  26. #include <ESP8266WiFiMulti.h>
  27. #include <ESP8266mDNS.h>
  28. #include <ESP8266WebServer.h>
  29.  
  30.  
  31. String form = // String form to sent to the client-browser
  32. "<p>"
  33. "<center>"
  34. "<h1>ESP8266 Pin D2 LED Controller</h1>"
  35. "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>" //display a form button"
  36. "</center>";
  37.  
  38. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  39.  
  40. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  41. const char* www_username = "admin"; // KJK1 Authentication
  42. const char* www_password = "esp8266"; // KJK1 Authentication
  43.  
  44. String ledStatus;
  45. const int led = 2;
  46.  
  47. void handleRoot(); // function prototypes for HTTP handlers
  48. void handleLED();
  49. void handleNotFound();
  50.  
  51. void setup(void){
  52. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  53. delay(10);
  54. Serial.println('\n');
  55. Serial.println("ESP IP Button");
  56. Serial.println();
  57.  
  58. pinMode(led, OUTPUT);
  59.  
  60. wifiMulti.addAP("SSID", "PASSWORD"); // add Wi-Fi networks you want to connect to
  61. wifiMulti.addAP("SSID", "PASSWORD");
  62. wifiMulti.addAP("SSID", "PASSWORD");
  63.  
  64. Serial.println("Connecting ...");
  65. int i = 0;
  66. while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
  67. delay(250);
  68. Serial.print('.');
  69. }
  70. Serial.println('\n');
  71. Serial.print("Connected to ");
  72. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  73. Serial.print("IP address:\t");
  74. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  75.  
  76. if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
  77. Serial.println("mDNS responder started");
  78. } else {
  79. Serial.println("Error setting up MDNS responder!");
  80. }
  81.  
  82. server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
  83. server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
  84. server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  85.  
  86. server.begin(); // Actually start the server
  87. Serial.println("HTTP server started");
  88. }
  89.  
  90. void loop(void){
  91. server.handleClient(); // Listen for HTTP requests from clients
  92. }
  93.  
  94. void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
  95. bool ledState = digitalRead(led); //state storage
  96.  
  97. if(!server.authenticate(www_username, www_password)) // KJK1 Authentication
  98. return server.requestAuthentication(); // KJK1 Authentication
  99. if (ledState == false){
  100. ledStatus= "<center><p> LED is ON </p></center>";
  101. }
  102. else{
  103. ledStatus= "<center><p> LED is OFF </p></center>";
  104. }
  105. Serial.println(!ledState);
  106.  
  107. server.send(200, "text/html", form+ledStatus); //display a form button
  108. }
  109.  
  110. void handleLED() { // If a POST request is made to URI /LED
  111. digitalWrite(led,!digitalRead(led)); // Change the state of the LED
  112. server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
  113. server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
  114. }
  115.  
  116. void handleNotFound(){
  117. server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement