forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because eboot first erases the space required for the new sketch, and if the new sketch is larger then the old one, with the old way, part of the beginning of new sketch will be deleted. Therefore for now I opted to keep the max update size either half the available space for sketches or what's left from the first one, whichever is smaller. To be able to create a simple post mechanism for updates, I needed to have a way to write the new binary, without knowing it's final size, so I added an option to the end() method. Example in the WebServer examples.
- Loading branch information
Showing
4 changed files
with
98 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
libraries/ESP8266WebServer/examples/WebUpdate/WebUpdate.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <WiFiClient.h> | ||
#include <ESP8266WebServer.h> | ||
#include <ESP8266mDNS.h> | ||
|
||
const char* host = "esp8266-webupdate"; | ||
const char* ssid = "........"; | ||
const char* password = "........"; | ||
|
||
ESP8266WebServer server(80); | ||
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>"; | ||
|
||
void setup(void){ | ||
Serial.begin(115200); | ||
Serial.println(); | ||
Serial.println("Booting Sketch..."); | ||
WiFi.mode(WIFI_AP_STA); | ||
WiFi.begin(ssid, password); | ||
if(WiFi.waitForConnectResult() == WL_CONNECTED){ | ||
MDNS.begin(host); | ||
server.on("/", HTTP_GET, [](){ | ||
server.sendHeader("Connection", "close"); | ||
server.sendHeader("Access-Control-Allow-Origin", "*"); | ||
server.send(200, "text/html", serverIndex); | ||
}); | ||
server.onFileUpload([](){ | ||
if(server.uri() != "/update") return; | ||
HTTPUpload& upload = server.upload(); | ||
if(upload.status == UPLOAD_FILE_START){ | ||
Serial.setDebugOutput(true); | ||
Serial.printf("Update: %s\n", upload.filename.c_str()); | ||
uint32_t maxSketchSpace = ((ESP.getFreeSketchSpace() + ESP.getSketchSize()) / 2) & 0xFFFFF000; | ||
if(!Update.begin(maxSketchSpace)){//start with max available size | ||
Update.printError(Serial); | ||
return; | ||
} | ||
} else if(upload.status == UPLOAD_FILE_WRITE){ | ||
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){ | ||
Update.printError(Serial); | ||
return; | ||
} | ||
} else if(upload.status == UPLOAD_FILE_END){ | ||
if(Update.end(true)){ //true to set the size to the current progress | ||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); | ||
} else { | ||
Update.printError(Serial); | ||
} | ||
Serial.setDebugOutput(false); | ||
} | ||
yield(); | ||
}); | ||
server.on("/update", HTTP_POST, [](){ | ||
server.sendHeader("Connection", "close"); | ||
server.sendHeader("Access-Control-Allow-Origin", "*"); | ||
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK"); | ||
ESP.restart(); | ||
}); | ||
server.begin(); | ||
MDNS.addService("http", "tcp", 80); | ||
|
||
Serial.println("Ready! Open http:\/\/%s.local in your browser\n", host); | ||
} else { | ||
Serial.println("WiFi Failed"); | ||
} | ||
} | ||
|
||
void loop(void){ | ||
server.handleClient(); | ||
delay(1); | ||
} |