daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Sep 25th, 2016 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <ArduinoOTA.h>
  4. #include <ESP8266WebServer.h>
  5.  
  6. //ssid and password of your wifi network
  7. const char* ssid = "wifi_ssid";
  8. const char* password = "wifi_password";
  9.  
  10. //ssid and password to connect to local hotspot of ESP8266
  11. const char *esp_ssid = "your_desired_ssid";
  12. const char *esp_password = "your_desired_password";
  13.  
  14. IPAddress ip(192, 168, 1, xx); // where xx is the desired IP Address
  15. IPAddress gateway(192, 168, 1, 254); // set gateway to match your wifi network
  16. IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your wifi network
  17.  
  18. ESP8266WebServer server(80);
  19. int status = LOW;
  20. const int pin = 5;
  21.  
  22. const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
  23.  
  24. //Authorization username and password before updating firmware
  25. const char* www_username = "admin";
  26. const char* www_password = "esp8266";
  27.  
  28. void connectToWifi() {
  29.   Serial.print("Connecting to ");
  30.   Serial.println(ssid);
  31.  
  32.   WiFi.config(ip, gateway, subnet);
  33.   WiFi.begin(ssid, password);
  34.   while(WiFi.waitForConnectResult() != WL_CONNECTED) {
  35.     delay(500);
  36.     Serial.print(".");
  37.     //ESP.restart();
  38.   }
  39.   Serial.print("WiFi connected to ");
  40.   Serial.println(WiFi.localIP());
  41. }
  42.  
  43. void createAccessPoint() {
  44.   Serial.println("Configuring access point for wifi network *your_desired_ssid*...");
  45.   WiFi.softAP(esp_ssid, esp_password);
  46.   IPAddress accessIP = WiFi.softAPIP();
  47.   Serial.print("ESP AccessPoint IP address: ");
  48.   Serial.println(accessIP);
  49.   /* Go to http://192.168.4.1 in a web browser
  50.    * connected to this access point to see it.
  51.    */
  52. }
  53.  
  54. void serverResponse(){
  55.   server.sendHeader("Connection", "close");
  56.   server.sendHeader("Access-Control-Allow-Origin", "*");
  57.   server.send(200, "text/plain", String(status));
  58. }
  59.  
  60.  
  61. void setup() {
  62.   Serial.begin(115200);
  63.   pinMode(pin, OUTPUT);
  64.   connectToWifi();
  65.   ArduinoOTA.begin();
  66.   server.on("/fupdate", [](){
  67.     if(server.authenticate(www_username, www_password)){
  68.       createAccessPoint();
  69.       server.sendHeader("Connection", "close");
  70.       server.sendHeader("Access-Control-Allow-Origin", "*");
  71.       server.send(200, "text/html", serverIndex);
  72.     }
  73.     else{
  74.        return server.requestAuthentication();
  75.     }
  76.  
  77.   });
  78.   server.on("/status", [](){
  79.     serverResponse();
  80.   });
  81.   server.on("/on", [](){
  82.     status = HIGH;
  83.     serverResponse();
  84.   });
  85.   server.on("/off", [](){
  86.     status = LOW;
  87.     serverResponse();
  88.   });
  89.   server.on("/update", HTTP_POST, [](){
  90.       server.sendHeader("Connection", "close");
  91.       server.sendHeader("Access-Control-Allow-Origin", "*");
  92.       server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
  93.       ESP.restart();
  94.     },[](){
  95.       HTTPUpload upload = server.upload();
  96.       if(upload.status == UPLOAD_FILE_START){
  97.         Serial.setDebugOutput(true);
  98.         WiFiUDP::stopAll();
  99.         Serial.printf("Update: %sn", upload.filename.c_str());
  100.         uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
  101.         if(!Update.begin(maxSketchSpace)){//start with max available size
  102.           Update.printError(Serial);
  103.         }
  104.       } else if(upload.status == UPLOAD_FILE_WRITE){
  105.         if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
  106.           Update.printError(Serial);
  107.         }
  108.       } else if(upload.status == UPLOAD_FILE_END){
  109.         if(Update.end(true)){ //true to set the size to the current progress
  110.           Serial.printf("Update Success: %unRebooting...n", upload.totalSize);
  111.         } else {
  112.           Update.printError(Serial);
  113.         }
  114.         Serial.setDebugOutput(false);
  115.       }
  116.       yield();
  117.     });
  118.   server.begin();
  119.   MDNS.addService("http", "tcp", 80);
  120.  
  121.   Serial.print("Open http://");
  122.   Serial.print(WiFi.localIP());
  123.   Serial.println("/ in your browser to see it working");
  124. }
  125.  
  126. void loop() {
  127.   digitalWrite(pin, status);
  128.   ArduinoOTA.handle();
  129.   server.handleClient();
  130.   delay(1);
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top