-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathc_ota.h
208 lines (167 loc) · 6.6 KB
/
c_ota.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
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
/***************************************************
Copyright (C) 2016 Steffen Ochs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
HISTORY: Please refer Github History
****************************************************/
/*
* Example:
*
* Check for new update
* http://update.wlanthermo.de/checkUpdate.php?device="nano"&serial="Serialnummer"&hw_version="v1"&sw_version="currentVersion"
* ----------------------------------------------------------------------------------------------------------------------------------------
* Download Firmware-version XYZ
* http://update.wlanthermo.de/checkUpdate.php?device="nano"serial="Serialnummer"&hw_version="v1"&sw_version="currentVersion"&getFirmware="XYZ"
* ----------------------------------------------------------------------------------------------------------------------------------------
* Download Spiffs-version XYZ
* http://update.wlanthermo.de/checkUpdate.php?device="nano"serial="Serialnummer"&hw_version="v1"&sw_version="currentVersion"&getSpiffs="XYZ"
* ----------------------------------------------------------------------------------------------------------------------------------------
*/
#ifdef OTA
#include <ArduinoOTA.h> // OTA
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Configuration OTA
void set_ota(){
ArduinoOTA.setHostname((const char *)sys.host.c_str());
ArduinoOTA.onStart([]() {
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 10, "OTA Update");
display.display();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
display.drawProgressBar(4, 32, 120, 8, progress / (total / 100) );
display.display();
});
ArduinoOTA.onEnd([]() {
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, "Restart");
display.display();
});
ArduinoOTA.onError([](ota_error_t error) {
DPRINTF("Error[%u]: ", error);
switch (error) {
case OTA_AUTH_ERROR:
DPRINTPLN("Auth Failed");
break;
case OTA_BEGIN_ERROR:
DPRINTPLN("Connect Failed");
break;
case OTA_CONNECT_ERROR:
DPRINTPLN("Connect Failed");
break;
case OTA_RECEIVE_ERROR:
DPRINTPLN("Receive Failed");
break;
case OTA_END_ERROR:
DPRINTPLN("End Failed");
break;
default:
DPRINTPLN("OTA unknown ERROR");
break;
}
});
}
#endif
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// HTTP UPDATE
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Do http update
void do_http_update() {
// UPDATE beendet
if (update.state == 4){
question.typ = OTAUPDATE;
drawQuestion(0);
update.get = "false";
update.state = 0;
setconfig(eSYSTEM,{}); // Speichern
update.state = -1; // Neue Suche anstoßen
IPRINTPLN("u:finish"); // Update finished
return;
}
if((wifi.mode == 1)) { // nur bei STA
if (update.get != "false") {
// UPDATE Adresse
String adress;
if (update.state == 1 || update.state == 3) { // nicht im Neuaufbau während Update
// UPDATE 2x Wiederholen falls schief gelaufen
if (update.count < 3) update.count++; // Wiederholung
else {
update.state = 0;
setconfig(eSYSTEM,{});
question.typ = OTAUPDATE;
drawQuestion(0);
IPRINTPLN("u:cancel"); // Update canceled
displayblocked = false;
update.count = 0;
return;
}
}
// UPDATE spiffs oder firmware
displayblocked = true;
t_httpUpdate_return ret;
if (update.state == 1 && update.spiffsUrl != "") { // erst wenn API abgefragt
update.state = 2; // Nächster Updatestatus
drawUpdate("Webinterface");
setconfig(eSYSTEM,{}); // SPEICHERN
IPRINTPLN("u:SPIFFS ...");
adress = update.spiffsUrl + adress; // https://.... + adress
Serial.println(adress);
ret = ESPhttpUpdate.updateSpiffs(adress);
} else if (update.state == 3 && update.firmwareUrl != "") { // erst wenn API abgefragt
update.state = 4;
drawUpdate("Firmware");
setconfig(eSYSTEM,{}); // SPEICHERN
IPRINTPLN("u:FW ...");
adress = update.firmwareUrl + adress; // https://.... + adress
Serial.println(adress);
ret = ESPhttpUpdate.update(adress);
}
// UPDATE Ereigniskontrolle
switch(ret) {
case HTTP_UPDATE_FAILED:
DPRINTF("[HTTP]\tUPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
DPRINTPLN("");
if (update.state == 2) update.state = 1; // Spiffs wiederholen
else update.state = 3; // Firmware wiederholen
//setconfig(eSYSTEM,{});
drawUpdate("error");
break;
case HTTP_UPDATE_NO_UPDATES:
DPRINTPLN("[HTTP]\tNO_UPDATES");
displayblocked = false;
break;
case HTTP_UPDATE_OK:
DPRINTPLN("[HTTP]\tUPDATE_OK");
if (update.state == 2) ESP.restart(); // falls nach spiffs kein automatischer Restart durchgeführt wird
break;
}
} else {
if (update.state != 2) { // nicht während Neustarts im Updateprozess
IPRINTPLN("u:no");
update.state = 0; // Vorgang beenden
}
}
}
}
/*
// FIRMWARE
#define FIRMWARESERVER "update.wlanthermo.de/getFirmware.php"
// "nano.norma.uberspace.de/update/checkUpdate.php"
// SPIFFS
#define SPIFFSSERVER "update.wlanthermo.de/getSpiffs.php"
// "nano.norma.uberspace.de/update/checkUpdate.php"
*/