-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path9_WebUpdate.ino
308 lines (294 loc) · 12.7 KB
/
9_WebUpdate.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// WebUpdate Board configuration ESP8266
// "configuration": "xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=basic,mmu=4816H,non32xfer=fast,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=921600",
bool upTools(String url, String fname, WiFiClientSecure &clientup)
{
HTTPClient https;
char line[120];
millis2wait(100);
if (https.begin(clientup, url + fname))
{
int16_t httpCode = https.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
uint8_t buff[128] = {0};
WiFiClient *stream = https.getStreamPtr(); // get tcp stream
bool check = LittleFS.remove("/" + fname);
fsUploadFile = LittleFS.open("/" + fname, "w");
int32_t len = https.getSize();
int32_t startlen = len;
while (https.connected() && (len > 0 || len == -1))
{
size_t size = stream->available(); // get available data size
if (size > 0)
{
int32_t c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // lese maximal 128 byte in buff
fsUploadFile.write(buff, c); // schreibe buff in Datei
if (len > 0)
{
len -= c;
}
}
delay(1);
}
fsUploadFile.close();
fsUploadFile = LittleFS.open("/" + fname, "r");
if (fsUploadFile.size() == startlen)
{
sprintf(line, "Framework/Tools update %s getSize: %d fileSize: %d", fname.c_str(), startlen, fsUploadFile.size());
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
}
else
{
sprintf(line, "Framework/Tools update Fehler %s getSize: %d fileSize: %d", fname.c_str(), startlen, fsUploadFile.size());
debugLog(UPDATELOG, line);
DEBUG_ERROR("SYS", "%s", line);
}
fsUploadFile.close();
https.end();
return true;
}
else
return false;
}
else
{
sprintf(line, "Framework/Tools update Fehler %s %s", fname.c_str(), https.errorToString(httpCode).c_str());
debugLog(UPDATELOG, line);
DEBUG_ERROR("SYS", "%s", line);
https.end();
return false;
}
}
else
{
sprintf(line, "Framework/Tools update Fehler https start %s", fname.c_str());
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
return false;
}
}
void upFirm()
{
WiFiClientSecure clientup;
clientup.setInsecure();
char line[120];
#ifdef ESP32
httpUpdate.onEnd(update_finished);
httpUpdate.onError(update_error);
t_httpUpdate_return ret;
if (LittleFS.exists(DEVBRANCH))
ret = httpUpdate.update(clientup, "https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/build/MQTTDevice32.ino.bin");
else
ret = httpUpdate.update(clientup, "https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/build/MQTTDevice32.ino.bin");
#elif ESP8266
ESPhttpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
ESPhttpUpdate.onEnd(update_finished);
ESPhttpUpdate.onError(update_error);
t_httpUpdate_return ret;
if (LittleFS.exists(DEVBRANCH))
ret = ESPhttpUpdate.update(clientup, "https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/build/MQTTDevice4.ino.bin");
else
ret = ESPhttpUpdate.update(clientup, "https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/build/MQTTDevice4.ino.bin");
#endif
return;
}
void updateTools()
{
setTagLevel("SYS", INNU_INFO);
configTzTime(ntpZone, ntpServer);
getLocalTime(&timeinfo);
strftime(zeit, sizeof(zeit), "%H:%M:%S", &timeinfo);
WiFiClientSecure clientup;
clientup.setInsecure();
fsUploadFile = LittleFS.open(LOGUPDATETOOLS, "r");
int8_t anzahlVersuche = 0;
if (fsUploadFile)
{
char anzahlV = char(fsUploadFile.read()) - '0';
anzahlVersuche = (int8_t)anzahlV;
}
fsUploadFile.close();
if (anzahlVersuche > 3)
{
LittleFS.remove(UPDATETOOLS);
DEBUG_INFO("SYS", "ERROR update tools - %d", anzahlVersuche);
return;
}
anzahlVersuche++;
fsUploadFile = LittleFS.open(LOGUPDATETOOLS, "w");
int32_t bytesWritten = fsUploadFile.print((anzahlVersuche));
fsUploadFile.close();
DEBUG_INFO("SYS", "ESP8266 IP address: %s time: %s WLAN Signal: %d", WiFi.localIP().toString().c_str(), zeit, WiFi.RSSI());
char line[120];
sprintf(line, "Update Framework/Tools #%d started - free heap: %d", anzahlVersuche, ESP.getFreeHeap());
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
if (anzahlVersuche == 1)
{
sprintf(line, "Firmware Version: %s", Version);
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
}
bool test;
if (LittleFS.exists(DEVBRANCH))
{
#ifdef ESP32
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "mqttfont.ttf", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "mqttdevice.min.css", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "mqttdevice.min.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "lang.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "en.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "de.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "no.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/development/data/", "favicon.ico", clientup);
#elif ESP8266
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "mqttfont.ttf", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "mqttdevice.min.css", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "mqttdevice.min.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "lang.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "de.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "en.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "no.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/development/data/", "favicon.ico", clientup);
#endif
}
else
{
#ifdef ESP32
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "mqttfont.ttf", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "mqttdevice.min.css", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "mqttdevice.min.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "lang.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "en.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "de.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "no.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice32/master/data/", "favicon.ico", clientup);
#elif ESP8266
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "mqttfont.ttf", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "mqttdevice.min.css", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "mqttdevice.min.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "lang.js", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "en.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "de.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "no.json", clientup);
test = upTools("https://raw.githubusercontent.com/InnuendoPi/MQTTDevice4/master/data/", "favicon.ico", clientup);
#endif
}
LittleFS.remove(UPDATETOOLS);
LittleFS.end();
millis2wait(1000);
ESP.restart();
}
void updateSys()
{
setTagLevel("SYS", INNU_INFO);
configTzTime(ntpZone, ntpServer);
getLocalTime(&timeinfo);
strftime(zeit, sizeof(zeit), "%H:%M:%S", &timeinfo);
char line[120];
fsUploadFile = LittleFS.open(LOGUPDATESYS, "r");
int8_t anzahlVersuche = 0;
if (fsUploadFile)
{
char anzahlV = char(fsUploadFile.read()) - '0';
anzahlVersuche = (int8_t)anzahlV;
}
fsUploadFile.close();
if (anzahlVersuche > 3)
{
LittleFS.remove(UPDATESYS);
return;
}
fsUploadFile = LittleFS.open(LOGUPDATESYS, "w");
anzahlVersuche++;
int32_t bytesWritten = fsUploadFile.print(anzahlVersuche);
fsUploadFile.close();
if (anzahlVersuche == 1)
bool check = LittleFS.remove(UPDATELOG);
sprintf(line, "WebUpdate firmware #%d started - free heap: %d", anzahlVersuche, ESP.getFreeHeap());
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
sprintf(line, "Firmware Version: %s", Version);
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
upFirm();
}
void startHTTPUpdate()
{
replyOK();
fsUploadFile = LittleFS.open(UPDATESYS, "w");
if (!fsUploadFile)
{
DEBUG_INFO("SYS", "%s", "Error WebUpdate firmware create file (LittleFS)");
return;
}
else
{
DEBUG_INFO("SYS", "%s", "WebUpdate firmware create file (LittleFS)");
int32_t bytesWritten = fsUploadFile.print("0");
fsUploadFile.close();
}
char line[120];
bool check = LittleFS.remove(UPDATELOG);
debugLog(UPDATELOG, "WebUpdate started");
sprintf(line, "Firmware version: %s", Version);
debugLog(UPDATELOG, line);
DEBUG_INFO("SYS", "%s", line);
if (devBranch)
{
fsUploadFile = LittleFS.open(DEVBRANCH, "w");
if (!fsUploadFile)
{
DEBUG_INFO("SYS", "%s", "Error WebUpdate firmware dev create file (LittleFS)");
return;
}
else
{
DEBUG_INFO("SYS", "%s", "WebUpdate firmware dev create file (LittleFS)");
int32_t bytesWritten = fsUploadFile.print("0");
debugLog(UPDATELOG, "WebUpdate development branch ausgewählt");
fsUploadFile.close();
}
}
else
{
if (LittleFS.exists(DEVBRANCH))
bool check = LittleFS.remove(DEVBRANCH);
}
DEBUG_INFO("SYS", "%s", "WebUpdate Firmware reboot");
LittleFS.end();
millis2wait(1000);
ESP.restart();
}
void update_finished()
{
debugLog(UPDATELOG, "Firmware update successful");
DEBUG_INFO("SYS", "%s", "Firmware update finished");
fsUploadFile = LittleFS.open(UPDATETOOLS, "w");
if (fsUploadFile)
{
int32_t bytesWritten = fsUploadFile.print(0);
fsUploadFile.close();
}
LittleFS.remove(UPDATESYS);
LittleFS.end();
millis2wait(1000);
ESP.restart();
}
void update_error(int16_t err)
{
char line[120];
#ifdef ESP32
sprintf(line, "Firmware update error code %d: %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
#elif ESP8266
sprintf(line, "Firmware update error code %d: %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
#endif
debugLog(UPDATELOG, line);
DEBUG_ERROR("SYS", "%s", line);
LittleFS.end();
millis2wait(1000);
ESP.restart();
}