-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_FOTA.ino
87 lines (77 loc) · 2.12 KB
/
2_FOTA.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void connect_wifi() {
Serial.println("Waiting for WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void firmwareUpdate(void) {
WiFiClientSecure client;
client.setCACert(rootCACertificate);
httpUpdate.setLedPin(LED_BUILTIN, LOW);
t_httpUpdate_return ret = httpUpdate.update(client, URL_fw_Bin);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
int FirmwareVersionCheck(void) {
String payload;
int httpCode;
String fwurl = "";
fwurl += URL_fw_Version;
fwurl += "?";
fwurl += String(rand());
Serial.println(fwurl);
WiFiClientSecure * client = new WiFiClientSecure;
if (client)
{
client -> setCACert(rootCACertificate);
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
if (https.begin( * client, fwurl))
{ // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
delay(100);
httpCode = https.GET();
delay(100);
if (httpCode == HTTP_CODE_OK) // if version received
{
payload = https.getString(); // save received version
} else {
Serial.print("error in downloading version file:");
Serial.println(httpCode);
}
https.end();
}
delete client;
}
if (httpCode == HTTP_CODE_OK) // if version received
{
payload.trim();
if (payload.equals(FirmwareVer)) {
Serial.printf("\nDevice already on latest firmware version:%s\n", FirmwareVer);
return 0;
}
else
{
Serial.println(payload);
Serial.println("New firmware detected");
return 1;
}
}
return 0;
}