Skip to content

Commit 77aebaa

Browse files
committed
Added support for downloading updates from a URL
1 parent b76fffd commit 77aebaa

File tree

4 files changed

+63
-28
lines changed

4 files changed

+63
-28
lines changed

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ version = -D BUILD_TAG=4.1.0
3333
monitor_speed = 115200
3434
lib_deps =
3535
36-
jeremypoulter/[email protected].16
36+
jeremypoulter/[email protected].17
3737
jeremypoulter/Micro [email protected]
3838
jeremypoulter/[email protected]
3939
jeremypoulter/[email protected]
@@ -141,6 +141,7 @@ build_flags =
141141
-D TX1=27
142142
-ggdb
143143
#upload_port = openevse.local
144+
#upload_protocol = espota
144145
upload_speed = 2000000
145146
#upload_protocol = ftdi
146147
monitor_speed = 115200

src/emoncms.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static MongooseHttpClient client;
2626
static void emoncms_result(bool success, String message)
2727
{
2828
StaticJsonDocument<128> event;
29-
29+
3030
emoncms_connected = success;
3131
event["emoncms_connected"] = (int)emoncms_connected;
3232
event["emoncms_message"] = message.substring(0, 64);
@@ -77,12 +77,12 @@ void emoncms_publish(JsonDocument &data)
7777
DEBUG.printf("%.*s\n", result.length(), (const char *)result);
7878
emoncms_result(false, result.toString());
7979
}
80-
}, [](MongooseHttpClientResponse *response)
80+
}, []()
8181
{
82-
DBUGF("onClose %p", response);
83-
if(NULL == response) {
84-
emoncms_result(false, String("Failed to connect"));
85-
}
82+
//DBUGF("onClose %p", response);
83+
//if(NULL == response) {
84+
// emoncms_result(false, String("Failed to connect"));
85+
//}
8686
});
8787
} else {
8888
if(false != emoncms_connected) {

src/http_update.cpp

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
#if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_HTTP_UPATE)
2+
#undef ENABLE_DEBUG
3+
#endif
4+
15
#include "http_update.h"
26
#include "lcd.h"
37
#include "debug.h"
8+
#include "emonesp.h"
49

510
#include <MongooseHttpClient.h>
611
#include <Update.h>
@@ -15,46 +20,62 @@ bool http_update_from_url(String url,
1520
std::function<void(int)> success,
1621
std::function<void(int)> error)
1722
{
18-
MongooseHttpClientRequest *request =
19-
client.beginRequest(url.c_str())
20-
->setMethod(HTTP_GET)
21-
// ->onBody([progress, error](const uint8_t *data, size_t len) {
22-
// if(http_update_write(data, len)) {
23-
// progress(len, update_total_size);
24-
// } else {
25-
// error(HTTP_UPDATE_ERROR_WRITE_FAILED);
26-
// }
27-
// }
28-
->onResponse([url,error](MongooseHttpClientResponse *response)
23+
MongooseHttpClientRequest *request = client.beginRequest(url.c_str());
24+
if(request)
25+
{
26+
request->setMethod(HTTP_GET);
27+
28+
request->onBody([url,progress,error,request](MongooseHttpClientResponse *response)
2929
{
3030
if(response->respCode() == 200)
3131
{
3232
size_t total = response->contentLength();
33-
if(!http_update_start(url, total)) {
33+
DBUGVAR(total);
34+
if(Update.isRunning() || http_update_start(url, total))
35+
{
36+
uint8_t *data = (uint8_t *)response->body().c_str();
37+
size_t len = response->body().length();
38+
if(http_update_write(data, len))
39+
{
40+
progress(len, total);
41+
return;
42+
} else {
43+
error(HTTP_UPDATE_ERROR_WRITE_FAILED);
44+
}
45+
} else {
3446
error(HTTP_UPDATE_ERROR_FAILED_TO_START_UPDATE);
3547
}
3648
} else {
3749
error(response->respCode());
3850
}
39-
})
40-
->onClose([success, error](MongooseHttpClientResponse *response) {
41-
if(http_update_end()) {
51+
request->abort();
52+
});
53+
54+
request->onClose([success, error]()
55+
{
56+
if(http_update_end())
57+
{
4258
success(HTTP_UPDATE_OK);
59+
restart_system();
4360
} else {
4461
error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE);
4562
}
4663
});
47-
client.send(request);
64+
client.send(request);
4865

49-
return true;
66+
return true;
67+
}
68+
69+
return false;
5070
}
5171

5272
bool http_update_start(String source, size_t total)
5373
{
74+
update_position = 0;
5475
update_total_size = total;
5576
if(Update.begin())
5677
{
57-
DEBUG_PORT.printf("Update Start: %s\n", source.c_str());
78+
DEBUG_PORT.printf("Update Start: %s %zu\n", source.c_str(), total);
5879

5980
lcd.display(F("Updating WiFi"), 0, 0, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
6081
lcd.display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
@@ -67,8 +88,10 @@ bool http_update_start(String source, size_t total)
6788

6889
bool http_update_write(uint8_t *data, size_t len)
6990
{
70-
DBUGF("Update Writing %u", update_position);
71-
if(Update.write(data, len) == len)
91+
DBUGF("Update Writing %u, %u", update_position, len);
92+
size_t written = Update.write(data, len);
93+
DBUGVAR(written);
94+
if(written == len)
7295
{
7396
update_position += len;
7497
if(update_total_size > 0)
@@ -80,6 +103,7 @@ bool http_update_write(uint8_t *data, size_t len)
80103
{
81104
String text = String(percent) + F("%");
82105
lcd.display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW);
106+
83107
DEBUG_PORT.printf("Update: %d%%\n", percent);
84108
lastPercent = percent;
85109
}
@@ -96,8 +120,9 @@ bool http_update_end()
96120
DBUGLN("Upload finished");
97121
if(Update.end(true))
98122
{
99-
DBUGF("Update Success: %uB", update_position);
123+
DBUGF("Update Success: %u", update_position);
100124
lcd.display(F("Complete"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
125+
return true;
101126
} else {
102127
DBUGF("Update failed: %d", Update.getError());
103128
lcd.display(F("Error"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);

test/update.http

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ Content-Type: application/javascript
5353
{
5454
"url": "{{latest.response.body.assets[1].browser_download_url}}"
5555
}
56+
57+
###
58+
59+
POST {{baseUrl}}/update
60+
Content-Type: application/javascript
61+
62+
{
63+
"url": "http://frenzy.lan:8000/.pio/build/{{config.response.body.buildenv}}/firmware.bin"
64+
}

0 commit comments

Comments
 (0)