Skip to content

Commit b58a350

Browse files
Jeroen88me-no-dev
Jeroen88
authored andcommitted
Feature/http update (#1979)
* Added HTTPUpdate class for downloading sketches from a server * Added HTTPUpdate class for downloading sketches from a server * Added HTTPUpdate to CMakeLists.txt * Change ESP8266 class references to ESP32 for httpUpdate.ino example * Change ESP8266 class references to ESP32 for httpUpdate.ino example. setLedPin() commented out because not all boards support LED_BUITLIN * Added check to handle mixup of old and present api properly * Correct HTTPClient::setTimeout() to convert milliseconds to seconds. Correct WiFiClient::setTimeout() to call Stream::setTimeout() with seconds converted back to milliseconds. Remove inproper checks for _insecure. * Added small comment because it looked like the Travis build did not finish
1 parent 01d22c8 commit b58a350

File tree

10 files changed

+814
-4
lines changed

10 files changed

+814
-4
lines changed

Diff for: CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(LIBRARY_SRCS
4646
libraries/FS/src/FS.cpp
4747
libraries/FS/src/vfs_api.cpp
4848
libraries/HTTPClient/src/HTTPClient.cpp
49+
libraries/HTTPUpdate/src/HTTPUpdate.cpp
4950
libraries/NetBIOS/src/NetBIOS.cpp
5051
libraries/Preferences/src/Preferences.cpp
5152
libraries/SD_MMC/src/SD_MMC.cpp
@@ -181,6 +182,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
181182
libraries/FFat/src
182183
libraries/FS/src
183184
libraries/HTTPClient/src
185+
libraries/HTTPUpdate/src
184186
libraries/NetBIOS/src
185187
libraries/Preferences/src
186188
libraries/SD_MMC/src
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
httpUpdate.ino
3+
4+
Created on: 27.11.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
#include <HTTPUpdate.h>
15+
16+
WiFiMulti WiFiMulti;
17+
18+
void setup() {
19+
20+
Serial.begin(115200);
21+
// Serial.setDebugOutput(true);
22+
23+
Serial.println();
24+
Serial.println();
25+
Serial.println();
26+
27+
for (uint8_t t = 4; t > 0; t--) {
28+
Serial.printf("[SETUP] WAIT %d...\n", t);
29+
Serial.flush();
30+
delay(1000);
31+
}
32+
33+
WiFi.mode(WIFI_STA);
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
37+
}
38+
39+
void loop() {
40+
// wait for WiFi connection
41+
if ((WiFiMulti.run() == WL_CONNECTED)) {
42+
43+
WiFiClient client;
44+
45+
// The line below is optional. It can be used to blink the LED on the board during flashing
46+
// The LED will be on during download of one buffer of data from the network. The LED will
47+
// be off during writing that buffer to flash
48+
// On a good connection the LED should flash regularly. On a bad connection the LED will be
49+
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
50+
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
51+
// httpUpdate.setLedPin(LED_BUILTIN, LOW);
52+
53+
t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
54+
// Or:
55+
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "file.bin");
56+
57+
switch (ret) {
58+
case HTTP_UPDATE_FAILED:
59+
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
60+
break;
61+
62+
case HTTP_UPDATE_NO_UPDATES:
63+
Serial.println("HTTP_UPDATE_NO_UPDATES");
64+
break;
65+
66+
case HTTP_UPDATE_OK:
67+
Serial.println("HTTP_UPDATE_OK");
68+
break;
69+
}
70+
}
71+
}
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
httpUpdateSPIFFS.ino
3+
4+
Created on: 05.12.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
#include <HTTPUpdate.h>
15+
16+
WiFiMulti WiFiMulti;
17+
18+
void setup() {
19+
20+
Serial.begin(115200);
21+
// Serial.setDebugOutput(true);
22+
23+
Serial.println();
24+
Serial.println();
25+
Serial.println();
26+
27+
for (uint8_t t = 4; t > 0; t--) {
28+
Serial.printf("[SETUP] WAIT %d...\n", t);
29+
Serial.flush();
30+
delay(1000);
31+
}
32+
33+
WiFi.mode(WIFI_STA);
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
}
37+
38+
void loop() {
39+
// wait for WiFi connection
40+
if ((WiFiMulti.run() == WL_CONNECTED)) {
41+
42+
Serial.println("Update SPIFFS...");
43+
44+
WiFiClient client;
45+
46+
// The line below is optional. It can be used to blink the LED on the board during flashing
47+
// The LED will be on during download of one buffer of data from the network. The LED will
48+
// be off during writing that buffer to flash
49+
// On a good connection the LED should flash regularly. On a bad connection the LED will be
50+
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
51+
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
52+
// httpUpdate.setLedPin(LED_BUILTIN, LOW);
53+
54+
t_httpUpdate_return ret = httpUpdate.updateSpiffs(client, "http://server/spiffs.bin");
55+
if (ret == HTTP_UPDATE_OK) {
56+
Serial.println("Update sketch...");
57+
ret = httpUpdate.update(client, "http://server/file.bin");
58+
59+
switch (ret) {
60+
case HTTP_UPDATE_FAILED:
61+
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
62+
break;
63+
64+
case HTTP_UPDATE_NO_UPDATES:
65+
Serial.println("HTTP_UPDATE_NO_UPDATES");
66+
break;
67+
68+
case HTTP_UPDATE_OK:
69+
Serial.println("HTTP_UPDATE_OK");
70+
break;
71+
}
72+
}
73+
}
74+
}
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
httpUpdateSecure.ino
3+
4+
Created on: 16.10.2018 as an adaptation of the ESP8266 version of httpUpdate.ino
5+
6+
*/
7+
8+
#include <WiFi.h>
9+
#include <WiFiMulti.h>
10+
11+
#include <HTTPClient.h>
12+
#include <HTTPUpdate.h>
13+
14+
#include <time.h>
15+
16+
WiFiMulti WiFiMulti;
17+
18+
// Set time via NTP, as required for x.509 validation
19+
void setClock() {
20+
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC
21+
22+
Serial.print(F("Waiting for NTP time sync: "));
23+
time_t now = time(nullptr);
24+
while (now < 8 * 3600 * 2) {
25+
yield();
26+
delay(500);
27+
Serial.print(F("."));
28+
now = time(nullptr);
29+
}
30+
31+
Serial.println(F(""));
32+
struct tm timeinfo;
33+
gmtime_r(&now, &timeinfo);
34+
Serial.print(F("Current time: "));
35+
Serial.print(asctime(&timeinfo));
36+
}
37+
38+
/**
39+
* This is lets-encrypt-x3-cross-signed.pem
40+
*/
41+
const char* rootCACertificate = \
42+
"-----BEGIN CERTIFICATE-----\n" \
43+
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
44+
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
45+
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
46+
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
47+
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
48+
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
49+
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
50+
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
51+
"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnW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2p8M+X+Q7UNKEkROb3N6\n" \
67+
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
68+
"-----END CERTIFICATE-----\n";
69+
70+
void setup() {
71+
72+
Serial.begin(115200);
73+
// Serial.setDebugOutput(true);
74+
75+
Serial.println();
76+
Serial.println();
77+
Serial.println();
78+
79+
for (uint8_t t = 4; t > 0; t--) {
80+
Serial.printf("[SETUP] WAIT %d...\n", t);
81+
Serial.flush();
82+
delay(1000);
83+
}
84+
85+
WiFi.mode(WIFI_STA);
86+
WiFiMulti.addAP("SSID", "PASSWORD");
87+
}
88+
89+
void loop() {
90+
// wait for WiFi connection
91+
if ((WiFiMulti.run() == WL_CONNECTED)) {
92+
93+
setClock();
94+
95+
WiFiClientSecure client;
96+
client.setCACert(rootCACertificate);
97+
98+
// Reading data over SSL may be slow, use an adequate timeout
99+
client.setTimeout(12000);
100+
101+
// The line below is optional. It can be used to blink the LED on the board during flashing
102+
// The LED will be on during download of one buffer of data from the network. The LED will
103+
// be off during writing that buffer to flash
104+
// On a good connection the LED should flash regularly. On a bad connection the LED will be
105+
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
106+
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
107+
// httpUpdate.setLedPin(LED_BUILTIN, HIGH);
108+
109+
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin");
110+
// Or:
111+
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "file.bin");
112+
113+
114+
switch (ret) {
115+
case HTTP_UPDATE_FAILED:
116+
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
117+
break;
118+
119+
case HTTP_UPDATE_NO_UPDATES:
120+
Serial.println("HTTP_UPDATE_NO_UPDATES");
121+
break;
122+
123+
case HTTP_UPDATE_OK:
124+
Serial.println("HTTP_UPDATE_OK");
125+
break;
126+
}
127+
}
128+
}

Diff for: libraries/HTTPUpdate/keywords.txt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#######################################
2+
# Syntax Coloring Map For ESP8266httpUpdate
3+
#######################################
4+
5+
#######################################
6+
# Library (KEYWORD3)
7+
#######################################
8+
9+
ESP8266httpUpdate KEYWORD3 RESERVED_WORD
10+
11+
#######################################
12+
# Datatypes (KEYWORD1)
13+
#######################################
14+
15+
HTTPUpdateResult KEYWORD1 DATA_TYPE
16+
ESPhttpUpdate KEYWORD1 DATA_TYPE
17+
18+
#######################################
19+
# Methods and Functions (KEYWORD2)
20+
#######################################
21+
22+
rebootOnUpdate KEYWORD2
23+
update KEYWORD2
24+
updateSpiffs KEYWORD2
25+
getLastError KEYWORD2
26+
getLastErrorString KEYWORD2
27+
28+
#######################################
29+
# Constants (LITERAL1)
30+
#######################################
31+
32+
HTTP_UE_TOO_LESS_SPACE LITERAL1 RESERVED_WORD_2
33+
HTTP_UE_SERVER_NOT_REPORT_SIZE LITERAL1 RESERVED_WORD_2
34+
HTTP_UE_SERVER_FILE_NOT_FOUND LITERAL1 RESERVED_WORD_2
35+
HTTP_UE_SERVER_FORBIDDEN LITERAL1 RESERVED_WORD_2
36+
HTTP_UE_SERVER_WRONG_HTTP_CODE LITERAL1 RESERVED_WORD_2
37+
HTTP_UE_SERVER_FAULTY_MD5 LITERAL1 RESERVED_WORD_2
38+
HTTP_UE_BIN_VERIFY_HEADER_FAILED LITERAL1 RESERVED_WORD_2
39+
HTTP_UE_BIN_FOR_WRONG_FLASH LITERAL1 RESERVED_WORD_2
40+
HTTP_UPDATE_FAILED LITERAL1 RESERVED_WORD_2
41+
HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2
42+
HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2

Diff for: libraries/HTTPUpdate/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=HTTPUpdate
2+
version=1.3
3+
author=Markus Sattler
4+
maintainer=Markus Sattler
5+
sentence=Http Update for ESP32
6+
paragraph=
7+
category=Data Processing
8+
url=https://github.com/Links2004/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266httpUpdate
9+
architectures=esp32

0 commit comments

Comments
 (0)