Skip to content

Commit 086af80

Browse files
committed
Merge pull request esp8266#1188 from Links2004/httpUpdate
allow SPIFFS update over http
2 parents 33bc6f8 + fdadadc commit 086af80

File tree

3 files changed

+147
-19
lines changed

3 files changed

+147
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* httpUpdateSPIFFS.ino
3+
*
4+
* Created on: 05.12.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266HTTPClient.h>
14+
#include <ESP8266httpUpdate.h>
15+
16+
#define USE_SERIAL Serial
17+
18+
ESP8266WiFiMulti WiFiMulti;
19+
20+
void setup() {
21+
22+
USE_SERIAL.begin(115200);
23+
// USE_SERIAL.setDebugOutput(true);
24+
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
USE_SERIAL.println();
28+
29+
for(uint8_t t = 4; t > 0; t--) {
30+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
31+
USE_SERIAL.flush();
32+
delay(1000);
33+
}
34+
35+
WiFiMulti.addAP("SSID", "PASSWORD");
36+
37+
}
38+
39+
void loop() {
40+
// wait for WiFi connection
41+
if((WiFiMulti.run() == WL_CONNECTED)) {
42+
43+
USE_SERIAL.println("Update SPIFFS...");
44+
t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs("https://server/spiffs.bin");
45+
if(ret == HTTP_UPDATE_OK) {
46+
USE_SERIAL.println("Update sketch...");
47+
ret = ESPhttpUpdate.update("https://server/file.bin");
48+
49+
switch(ret) {
50+
case HTTP_UPDATE_FAILED:
51+
USE_SERIAL.println("HTTP_UPDATE_FAILD");
52+
break;
53+
54+
case HTTP_UPDATE_NO_UPDATES:
55+
USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
56+
break;
57+
58+
case HTTP_UPDATE_OK:
59+
USE_SERIAL.println("HTTP_UPDATE_OK");
60+
break;
61+
}
62+
}
63+
}
64+
}
65+

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

+78-17
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
*
2424
*/
2525

26-
2726
#include "ESP8266httpUpdate.h"
27+
#include <StreamString.h>
2828

29+
extern "C" uint32_t _SPIFFS_start;
30+
extern "C" uint32_t _SPIFFS_end;
2931

3032
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
3133
}
@@ -46,6 +48,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
4648
return handleUpdate(&http, current_version);
4749
}
4850

51+
/**
52+
*
53+
* @param url const char *
54+
* @param current_version const char *
55+
* @param httpsFingerprint const char *
56+
* @return t_httpUpdate_return
57+
*/
58+
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint) {
59+
HTTPClient http;
60+
http.begin(url, httpsFingerprint);
61+
return handleUpdate(&http, current_version, false, true);
62+
}
63+
4964
/**
5065
*
5166
* @param host const char *
@@ -73,7 +88,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
7388
* @param current_version const char *
7489
* @return t_httpUpdate_return
7590
*/
76-
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version) {
91+
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) {
7792

7893
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
7994

@@ -85,6 +100,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
85100
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
86101
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
87102

103+
if(spiffs) {
104+
http->addHeader("x-ESP8266-mode", "spiffs");
105+
} else {
106+
http->addHeader("x-ESP8266-mode", "sketch");
107+
}
108+
88109
if(current_version && current_version[0] != 0x00) {
89110
http->addHeader("x-ESP8266-version", current_version);
90111
}
@@ -99,6 +120,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
99120
int code = http->GET();
100121
int len = http->getSize();
101122

123+
if(code <= 0) {
124+
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str());
125+
http->end();
126+
return HTTP_UPDATE_FAILED;
127+
}
128+
102129
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
103130
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
104131
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
@@ -117,11 +144,24 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
117144
}
118145

119146
switch(code) {
120-
case 200: ///< OK (Start Update)
147+
case HTTP_CODE_OK: ///< OK (Start Update)
121148
if(len > 0) {
122-
if(len > ESP.getFreeSketchSpace()) {
149+
bool startUpdate = true;
150+
if(spiffs) {
151+
size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
152+
if(len > (int) spiffsSize) {
153+
DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len);
154+
startUpdate = false;
155+
}
156+
} else {
157+
if(len > (int) ESP.getFreeSketchSpace()) {
158+
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
159+
startUpdate = false;
160+
}
161+
}
162+
163+
if(!startUpdate) {
123164
ret = HTTP_UPDATE_FAILED;
124-
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
125165
} else {
126166

127167
WiFiClient * tcp = http->getStreamPtr();
@@ -131,11 +171,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
131171

132172
delay(100);
133173

134-
if(runUpdate(*tcp, len, http->header("x-MD5"))) {
174+
int command;
175+
176+
if(spiffs) {
177+
command = U_SPIFFS;
178+
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n");
179+
} else {
180+
command = U_FLASH;
181+
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n");
182+
}
183+
184+
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) {
135185
ret = HTTP_UPDATE_OK;
136186
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
137187
http->end();
138-
ESP.restart();
188+
189+
if(reboot) {
190+
ESP.restart();
191+
}
192+
139193
} else {
140194
ret = HTTP_UPDATE_FAILED;
141195
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
@@ -146,19 +200,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
146200
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
147201
}
148202
break;
149-
case 304:
203+
case HTTP_CODE_NOT_MODIFIED:
150204
///< Not Modified (No updates)
151205
ret = HTTP_UPDATE_NO_UPDATES;
152206
break;
153-
case 403:
154-
///< Forbidden
155-
// todo handle login
156207
default:
157208
ret = HTTP_UPDATE_FAILED;
158-
DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code);
209+
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
210+
//http->writeToStream(&Serial1);
159211
break;
160212
}
161213

214+
162215
http->end();
163216

164217
return ret;
@@ -171,10 +224,14 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
171224
* @param md5 String
172225
* @return true if Update ok
173226
*/
174-
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
227+
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) {
228+
229+
StreamString error;
175230

176-
if(!Update.begin(size)) {
177-
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed!\n");
231+
if(!Update.begin(size, command)) {
232+
Update.printError(error);
233+
error.trim(); // remove line ending
234+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
178235
return false;
179236
}
180237

@@ -183,12 +240,16 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
183240
}
184241

185242
if(Update.writeStream(in) != size) {
186-
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed!\n");
243+
Update.printError(error);
244+
error.trim(); // remove line ending
245+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
187246
return false;
188247
}
189248

190249
if(!Update.end()) {
191-
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed!\n");
250+
Update.printError(error);
251+
error.trim(); // remove line ending
252+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());
192253
return false;
193254
}
194255

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class ESP8266HTTPUpdate {
5353
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "");
5454
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "");
5555

56+
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "");
57+
5658
protected:
57-
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version);
58-
bool runUpdate(Stream& in, uint32_t size, String md5);
59+
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
60+
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
5961
};
6062

6163
extern ESP8266HTTPUpdate ESPhttpUpdate;

0 commit comments

Comments
 (0)