fabelizer

ESP IP Button

Jan 14th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 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. */
  21.  
  22. #include <ESP8266WiFi.h>
  23. #include <WiFiClient.h>
  24. #include <ESP8266WiFiMulti.h>
  25. #include <ESP8266mDNS.h>
  26. #include <ESP8266WebServer.h>
  27.  
  28.  
  29. String form = // String form to sent to the client-browser
  30. "<p>"
  31. "<center>"
  32. "<h1>ESP8266 Pin D2 LED Controller</h1>"
  33. "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>" //display a form button"
  34. "</center>";
  35.  
  36. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  37.  
  38. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  39. String ledStatus;
  40. const int led = 2;
  41.  
  42. void handleRoot(); // function prototypes for HTTP handlers
  43. void handleLED();
  44. void handleNotFound();
  45.  
  46. void setup(void){
  47. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  48. delay(10);
  49. Serial.println('\n');
  50. Serial.println("ESP IP Button");
  51. Serial.println();
  52.  
  53. pinMode(led, OUTPUT);
  54.  
  55. wifiMulti.addAP("SSID", "PASSWORD"); // add Wi-Fi networks you want to connect to
  56. wifiMulti.addAP("SSID", "PASSWORD");
  57. wifiMulti.addAP("SSID", "PASSWORD");
  58.  
  59. Serial.println("Connecting ...");
  60. int i = 0;
  61. 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
  62. delay(250);
  63. Serial.print('.');
  64. }
  65. Serial.println('\n');
  66. Serial.print("Connected to ");
  67. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  68. Serial.print("IP address:\t");
  69. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  70.  
  71. if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
  72. Serial.println("mDNS responder started");
  73. } else {
  74. Serial.println("Error setting up MDNS responder!");
  75. }
  76.  
  77. server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
  78. server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
  79. server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  80.  
  81. server.begin(); // Actually start the server
  82. Serial.println("HTTP server started");
  83. }
  84.  
  85. void loop(void){
  86. server.handleClient(); // Listen for HTTP requests from clients
  87. }
  88.  
  89. void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
  90. bool ledState = digitalRead(led); //state storage
  91. if (ledState == false){
  92. ledStatus= "<center><p> LED is ON </p></center>";
  93. }
  94. else{
  95. ledStatus= "<center><p> LED is OFF </p></center>";
  96. }
  97. Serial.println(!ledState);
  98.  
  99. server.send(200, "text/html", form+ledStatus); //display a form button
  100. }
  101.  
  102. void handleLED() { // If a POST request is made to URI /LED
  103. digitalWrite(led,!digitalRead(led)); // Change the state of the LED
  104. server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
  105. server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
  106. }
  107.  
  108. void handleNotFound(){
  109. 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
  110. }
Add Comment
Please, Sign In to add comment