Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program is a mash up of the example given in Rui Santos'
- * Home Automation course, some code I found at
- * https://tttapa.github.io/ESP8266/Chap10%20-%20Simple%20Web%20Server.html
- * (Be sure to read the whole beginner's guide https://tttapa.github.io) for
- * tons of good info! EXCELLENT!) and I used some html code found on the web,
- * that I cannot seem to find again. The latter taught me to put the web
- * pagebutton into a string * variable, and send it as a string...good idea!
- *
- * Rui's code allowed toggling pins on an ESP8266 from a web page
- * served from the ESP, as did Pieter's little GitHub Page from the link above,
- * but the pages did not include a pin status on the control webpage after it
- * updated. With a bit of tweaking, I now have this feature running on an
- * ESP8266-07, and I am quite thrilled! :) It simply controls the
- * built in LED on the board, (the first step to controlling the WORLD!),
- * and the page notifies you of the current state of the LED (on/off).
- *
- * Thanks to those that made this possible!!
- *
- * Please feel free to use this code as you wish.
- * 2018-01-14 DK Posted on Facebook by ‎Dave Kosewick https://www.facebook.com/dave.kosewick
- * 2018-01-16 KJK Added code for user authentication. Search for KJK1.
- */
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266WebServer.h>
- String form = // String form to sent to the client-browser
- "<p>"
- "<center>"
- "<h1>ESP8266 Pin D2 LED Controller</h1>"
- "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>" //display a form button"
- "</center>";
- ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
- ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
- const char* www_username = "admin"; // KJK1 Authentication
- const char* www_password = "esp8266"; // KJK1 Authentication
- String ledStatus;
- const int led = 2;
- void handleRoot(); // function prototypes for HTTP handlers
- void handleLED();
- void handleNotFound();
- void setup(void){
- Serial.begin(115200); // Start the Serial communication to send messages to the computer
- delay(10);
- Serial.println('\n');
- Serial.println("ESP IP Button");
- Serial.println();
- pinMode(led, OUTPUT);
- wifiMulti.addAP("SSID", "PASSWORD"); // add Wi-Fi networks you want to connect to
- wifiMulti.addAP("SSID", "PASSWORD");
- wifiMulti.addAP("SSID", "PASSWORD");
- Serial.println("Connecting ...");
- int i = 0;
- 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
- delay(250);
- Serial.print('.');
- }
- Serial.println('\n');
- Serial.print("Connected to ");
- Serial.println(WiFi.SSID()); // Tell us what network we're connected to
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
- if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
- Serial.println("mDNS responder started");
- } else {
- Serial.println("Error setting up MDNS responder!");
- }
- server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
- server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
- server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
- server.begin(); // Actually start the server
- Serial.println("HTTP server started");
- }
- void loop(void){
- server.handleClient(); // Listen for HTTP requests from clients
- }
- void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
- bool ledState = digitalRead(led); //state storage
- if(!server.authenticate(www_username, www_password)) // KJK1 Authentication
- return server.requestAuthentication(); // KJK1 Authentication
- if (ledState == false){
- ledStatus= "<center><p> LED is ON </p></center>";
- }
- else{
- ledStatus= "<center><p> LED is OFF </p></center>";
- }
- Serial.println(!ledState);
- server.send(200, "text/html", form+ledStatus); //display a form button
- }
- void handleLED() { // If a POST request is made to URI /LED
- digitalWrite(led,!digitalRead(led)); // Change the state of the LED
- server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
- server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
- }
- void handleNotFound(){
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement