-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.h
77 lines (67 loc) · 2.38 KB
/
update.h
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
const int FW_VERSION = 1;
const char* versionFileUrlBase = "https://raw.githubusercontent.com/sqra/IoTGitHubFrame/master/OTA/IoTGithubFrame.version";
const char* binFileUrlStart = "http://rawcdn.githack.com/sqra/IoTGitHubFrame/v";
const char* binFileUrlEnd = ".0/OTA/IoTGithubFrame.ino.esp32.bin";
// update firmware
void updateNow(int newVersion) {
Serial.println( "Preparing to update firmware" );
String binURL = String( binFileUrlStart );
String binURLTag = String( newVersion );
binURL.concat( binURLTag );
String binURLEnd = String( binFileUrlEnd );
binURL.concat( binURLEnd );
Serial.print( "Firmware .bin file URL: " );
Serial.println( binURL );
HTTPClient espClient;
espClient.begin( binURL );
int httpCode = espClient.GET();
if ( httpCode == 200 ) {
t_httpUpdate_return ret = ESPhttpUpdate.update( binURL );
switch (ret) {
case HTTP_UPDATE_OK:
Serial.printf("HTTP_UPDATE_OK");
Serial.println("");
break;
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
Serial.println("");
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
}
} else {
Serial.print( "Firmware update failed, got HTTP response code " );
Serial.println( httpCode );
}
espClient.end();
}
// check if there is a new version of the firmware
void checkForUpdates() {
String fwURL = String( versionFileUrlBase );
Serial.println( "Checking for firmware updates." );
Serial.print( "Firmware version URL: " );
Serial.println( fwURL );
HTTPClient httpClient;
httpClient.begin( fwURL );
int httpCode = httpClient.GET();
if ( httpCode == 200 ) {
String newFWVersion = httpClient.getString();
Serial.print( "Current firmware version: " );
Serial.println( FW_VERSION );
Serial.print( "Available firmware version: " );
Serial.println( newFWVersion );
int newVersion = newFWVersion.toInt();
if ( newVersion > FW_VERSION ) {
updateNow(newVersion);
}
else {
Serial.println( "Already on latest version." );
}
}
else {
Serial.print( "Firmware version check failed, got HTTP response code " );
Serial.println( httpCode );
}
httpClient.end();
}