From ecce35d24058e3a6b77a86f845c732f2592b5540 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Sat, 5 Dec 2015 20:33:08 +0100 Subject: [PATCH 1/6] allow SPIFFS --- .../src/ESP8266httpUpdate.cpp | 61 ++++++++++++++++--- .../ESP8266httpUpdate/src/ESP8266httpUpdate.h | 6 +- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 39ba0555c..8907df24a 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -23,9 +23,10 @@ * */ - #include "ESP8266httpUpdate.h" +extern "C" uint32_t _SPIFFS_start; +extern "C" uint32_t _SPIFFS_end; ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) { } @@ -46,6 +47,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur return handleUpdate(&http, current_version); } +/** + * + * @param url const char * + * @param current_version const char * + * @param httpsFingerprint const char * + * @return t_httpUpdate_return + */ +t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint) { + HTTPClient http; + http.begin(url, httpsFingerprint); + return handleUpdate(&http, current_version, false, true); +} + /** * * @param host const char * @@ -73,7 +87,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String * @param current_version const char * * @return t_httpUpdate_return */ -t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version) { +t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) { t_httpUpdate_return ret = HTTP_UPDATE_FAILED; @@ -85,6 +99,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize())); http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion()); + if(spiffs) { + http->addHeader("x-ESP8266-mode", "spiffs"); + } else { + http->addHeader("x-ESP8266-mode", "sketch"); + } + if(current_version && current_version[0] != 0x00) { http->addHeader("x-ESP8266-version", current_version); } @@ -119,9 +139,20 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha switch(code) { case 200: ///< OK (Start Update) if(len > 0) { - if(len > ESP.getFreeSketchSpace()) { + bool startUpdate = false; + if(spiffs) { + size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start); + if(len > (int) spiffsSize) { + DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); + } + } else { + if(len > (int) ESP.getFreeSketchSpace()) { + DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); + } + } + + if(!startUpdate) { ret = HTTP_UPDATE_FAILED; - DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); } else { WiFiClient * tcp = http->getStreamPtr(); @@ -131,11 +162,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha delay(100); - if(runUpdate(*tcp, len, http->header("x-MD5"))) { + int command; + + if(spiffs) { + command = U_SPIFFS; + DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n"); + } else { + command = U_FLASH; + DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n"); + } + + if(runUpdate(*tcp, len, http->header("x-MD5"), command)) { ret = HTTP_UPDATE_OK; DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); http->end(); - ESP.restart(); + + if(reboot) { + ESP.restart(); + } + } else { ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n"); @@ -171,9 +216,9 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha * @param md5 String * @return true if Update ok */ -bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) { +bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) { - if(!Update.begin(size)) { + if(!Update.begin(size, command)) { DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed!\n"); return false; } diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index a494695ff..f9a4d8030 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -53,9 +53,11 @@ class ESP8266HTTPUpdate { t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = ""); t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = ""); + t_httpUpdate_return updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint); + protected: - t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version); - bool runUpdate(Stream& in, uint32_t size, String md5); + t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false); + bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH); }; extern ESP8266HTTPUpdate ESPhttpUpdate; From 95d53c37c381536cb9e87a0516082b1f5c2f26ba Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Sat, 5 Dec 2015 20:47:58 +0100 Subject: [PATCH 2/6] add httpUpdateSPIFFS.ino example --- .../httpUpdateSPIFFS/httpUpdateSPIFFS.ino | 65 +++++++++++++++++++ .../src/ESP8266httpUpdate.cpp | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino new file mode 100644 index 000000000..3d746af6c --- /dev/null +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino @@ -0,0 +1,65 @@ +/** + * httpUpdateSPIFFS.ino + * + * Created on: 05.12.2015 + * + */ + +#include + +#include +#include + +#include +#include + +#define USE_SERIAL Serial + +ESP8266WiFiMulti WiFiMulti; + +void setup() { + + USE_SERIAL.begin(115200); + // USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFiMulti.addAP("SSID", "PASSWORD"); + +} + +void loop() { + // wait for WiFi connection + if((WiFiMulti.run() == WL_CONNECTED)) { + + USE_SERIAL.println("Update SPIFFS..."); + t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs("https://server/spiffs.bin"); + if(ret == HTTP_UPDATE_OK) { + USE_SERIAL.println("Update sketch..."); + ret = ESPhttpUpdate.update("https://server/file.bin"); + + switch(ret) { + case HTTP_UPDATE_FAILED: + USE_SERIAL.println("HTTP_UPDATE_FAILD"); + break; + + case HTTP_UPDATE_NO_UPDATES: + USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); + break; + + case HTTP_UPDATE_OK: + USE_SERIAL.println("HTTP_UPDATE_OK"); + break; + } + } + } +} + diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 8907df24a..52a094c69 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -143,7 +143,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha if(spiffs) { size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start); if(len > (int) spiffsSize) { - DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); + DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len); } } else { if(len > (int) ESP.getFreeSketchSpace()) { From d8e3c766a33041158af3f97499742ee4eb522e0c Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Sun, 6 Dec 2015 11:07:25 +0100 Subject: [PATCH 3/6] ESP8266HTTPUpdate - improve error displaying --- .../ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 52a094c69..92b047b30 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -24,6 +24,7 @@ */ #include "ESP8266httpUpdate.h" +#include extern "C" uint32_t _SPIFFS_start; extern "C" uint32_t _SPIFFS_end; @@ -218,8 +219,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha */ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) { + StreamString error; + if(!Update.begin(size, command)) { - DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed!\n"); + Update.printError(error); + error.trim(); // remove line ending + DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str()); return false; } @@ -228,12 +233,16 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com } if(Update.writeStream(in) != size) { - DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed!\n"); + Update.printError(error); + error.trim(); // remove line ending + DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str()); return false; } if(!Update.end()) { - DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed!\n"); + Update.printError(error); + error.trim(); // remove line ending + DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str()); return false; } From b1ea7b602e3b731f7b9e5f666aabfe7d5e743d41 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 9 Dec 2015 16:27:04 +0100 Subject: [PATCH 4/6] httpUpdate Improve error handling --- .../ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 92b047b30..763efea4b 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -120,6 +120,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha int code = http->GET(); int len = http->getSize(); + if(code <= 0) { + DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str()); + http->end(); + return HTTP_UPDATE_FAILED; + } + DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n"); DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n"); DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code); @@ -138,7 +144,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha } switch(code) { - case 200: ///< OK (Start Update) + case HTTP_CODE_OK: ///< OK (Start Update) if(len > 0) { bool startUpdate = false; if(spiffs) { @@ -192,19 +198,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n"); } break; - case 304: + case HTTP_CODE_NOT_MODIFIED: ///< Not Modified (No updates) ret = HTTP_UPDATE_NO_UPDATES; break; - case 403: - ///< Forbidden - // todo handle login default: ret = HTTP_UPDATE_FAILED; - DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code); + DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code); + //http->writeToStream(&Serial1); break; } + http->end(); return ret; From 95089b75b909da9828f81047d90162a272b548dd Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 9 Dec 2015 19:56:14 +0100 Subject: [PATCH 5/6] fix startUpdate always false --- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 763efea4b..785258de9 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -146,15 +146,17 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha switch(code) { case HTTP_CODE_OK: ///< OK (Start Update) if(len > 0) { - bool startUpdate = false; + bool startUpdate = true; if(spiffs) { size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start); if(len > (int) spiffsSize) { DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len); + startUpdate = false; } } else { if(len > (int) ESP.getFreeSketchSpace()) { DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); + startUpdate = false; } } From fdadadc7875386b749ee23728bc6abc31accc259 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 10 Dec 2015 12:03:53 +0100 Subject: [PATCH 6/6] add default values for updateSpiffs --- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index f9a4d8030..d3fe4ca19 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -53,7 +53,7 @@ class ESP8266HTTPUpdate { t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = ""); t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = ""); - t_httpUpdate_return updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint); + t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = ""); protected: t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);