|
| 1 | +#include <Arduino.h> |
| 2 | +#include <WiFiClient.h> |
| 3 | +#include <WiFiServer.h> |
| 4 | +#include <ESP8266WebServer.h> |
| 5 | +#include <WiFiUdp.h> |
| 6 | +#include "ESP8266HTTPUpdateServer.h" |
| 7 | + |
| 8 | + |
| 9 | +const char* ESP8266HTTPUpdateServer::_serverIndex = |
| 10 | +R"(<html><body><form method='POST' action='/update' enctype='multipart/form-data'> |
| 11 | + <input type='file' name='update'> |
| 12 | + <input type='submit' value='Update'> |
| 13 | + </form> |
| 14 | + </body></html>)"; |
| 15 | + |
| 16 | +ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug) |
| 17 | +{ |
| 18 | + _serial_output = serial_debug; |
| 19 | + _server = NULL; |
| 20 | +} |
| 21 | + |
| 22 | +void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server) |
| 23 | +{ |
| 24 | + _server = server; |
| 25 | + |
| 26 | + // handler for the /update form page |
| 27 | + _server->on("/update", HTTP_GET, [&](){ |
| 28 | + _server->sendHeader("Connection", "close"); |
| 29 | + _server->sendHeader("Access-Control-Allow-Origin", "*"); |
| 30 | + _server->send(200, "text/html", _serverIndex); |
| 31 | + }); |
| 32 | + |
| 33 | + // handler for the /update form POST (once file upload finishes) |
| 34 | + _server->on("/update", HTTP_POST, [&](){ |
| 35 | + _server->sendHeader("Connection", "close"); |
| 36 | + _server->sendHeader("Access-Control-Allow-Origin", "*"); |
| 37 | + _server->send(200, "text/plain", (Update.hasError())?"FAIL":"OK"); |
| 38 | + ESP.restart(); |
| 39 | + }); |
| 40 | + |
| 41 | + // handler for the file upload, get's the sketch bytes, and writes |
| 42 | + // them through the Update object. |
| 43 | + _server->onFileUpload([&](){ |
| 44 | + if(_server->uri() != "/update") return; |
| 45 | + HTTPUpload& upload = _server->upload(); |
| 46 | + if(upload.status == UPLOAD_FILE_START){ |
| 47 | + if (_serial_output) |
| 48 | + Serial.setDebugOutput(true); |
| 49 | + WiFiUDP::stopAll(); |
| 50 | + if (_serial_output) |
| 51 | + Serial.printf("Update: %s\n", upload.filename.c_str()); |
| 52 | + uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; |
| 53 | + if(!Update.begin(maxSketchSpace)){//start with max available size |
| 54 | + if (_serial_output) Update.printError(Serial); |
| 55 | + } |
| 56 | + } else if(upload.status == UPLOAD_FILE_WRITE){ |
| 57 | + if (_serial_output) Serial.printf("."); |
| 58 | + if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){ |
| 59 | + if (_serial_output) Update.printError(Serial); |
| 60 | + |
| 61 | + } |
| 62 | + } else if(upload.status == UPLOAD_FILE_END){ |
| 63 | + if(Update.end(true)){ //true to set the size to the current progress |
| 64 | + if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); |
| 65 | + } else { |
| 66 | + if (_serial_output) Update.printError(Serial); |
| 67 | + } |
| 68 | + if (_serial_output) Serial.setDebugOutput(false); |
| 69 | + } else if(upload.status == UPLOAD_FILE_ABORTED){ |
| 70 | + Update.end(); |
| 71 | + if (_serial_output) Serial.println("Update was aborted"); |
| 72 | + } |
| 73 | + yield(); |
| 74 | + }); |
| 75 | +} |
0 commit comments