4
4
*/
5
5
6
6
#include < ArduinoOTA.h>
7
- #include < HTTPUpdateServer.h>
8
7
#include < HardwareSerial.h>
9
8
#include < MycilaESPConnect.h>
9
+ #include < StreamString.h>
10
+ #include < WebServer.h>
10
11
11
12
#include < esp_ota_ops.h>
12
13
#include < esp_partition.h>
21
22
#define LOG (format, ...)
22
23
#endif
23
24
25
+ extern const char * __COMPILED_APP_VERSION__;
26
+ extern const uint8_t update_html_start[] asm (" _binary__pio_embed_website_html_gz_start" );
27
+ extern const uint8_t update_html_end[] asm (" _binary__pio_embed_website_html_gz_end" );
28
+ static const char * successResponse = " Update Success! Rebooting..." ;
29
+ static const char * cancelResponse = " Rebooting..." ;
30
+
24
31
static WebServer webServer (80 );
25
- static HTTPUpdateServer httpUpdater;
26
32
static Mycila::ESPConnect espConnect;
27
33
static Mycila::ESPConnect::Config espConnectConfig;
34
+ static StreamString updaterError;
28
35
29
36
static String getChipIDStr () {
30
37
uint32_t chipId = 0 ;
@@ -36,33 +43,87 @@ static String getChipIDStr() {
36
43
return espId;
37
44
}
38
45
39
- void setup () {
40
- #ifdef SAFEBOOT_LOGGING
41
- Serial.begin (115200 );
42
- #if ARDUINO_USB_CDC_ON_BOOT
43
- Serial.setTxTimeoutMs (0 );
44
- delay (100 );
45
- #else
46
- while (!Serial)
47
- yield ();
48
- #endif
49
- #endif
46
+ static void start_web_server () {
47
+ webServer.on (" /cancel" , HTTP_POST, [&]() {
48
+ webServer.send (200 , " text/plain" , cancelResponse);
49
+ webServer.client ().stop ();
50
+ delay (500 );
51
+ ESP.restart (); }, [&]() {});
50
52
51
- LOG (" Version: %s\n " , __COMPILED_APP_VERSION__);
53
+ webServer.on (" /" , HTTP_GET, [&]() {
54
+ webServer.sendHeader (" Content-Encoding" , " gzip" );
55
+ webServer.send_P (200 , " text/html" , reinterpret_cast <const char *>(update_html_start), update_html_end - update_html_start);
56
+ });
52
57
53
- // Set next boot partition
54
- const esp_partition_t * partition = esp_partition_find_first (esp_partition_type_t ::ESP_PARTITION_TYPE_APP, esp_partition_subtype_t ::ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr );
55
- if (partition) {
56
- esp_ota_set_boot_partition (partition);
57
- }
58
+ webServer.on (" /" , HTTP_POST, [&]() {
59
+ if (Update.hasError ()) {
60
+ webServer.send (500 , " text/plain" , " Update error: " + updaterError);
61
+ } else {
62
+ webServer.client ().setNoDelay (true );
63
+ webServer.send (200 , " text/plain" , successResponse);
64
+ webServer.client ().stop ();
65
+ delay (500 );
66
+ ESP.restart ();
67
+ } }, [&]() {
68
+ // handler for the file upload, gets the sketch bytes, and writes
69
+ // them through the Update object
70
+ HTTPUpload& upload = webServer.upload ();
71
+
72
+ if (upload.status == UPLOAD_FILE_START) {
73
+ updaterError.clear ();
74
+ int otaMode = U_FLASH;
75
+ if (webServer.hasArg (" mode" ) && webServer.arg (" mode" ) == " 1" ) {
76
+ otaMode = U_SPIFFS;
77
+ }
78
+ LOG (" Mode: %d\n " , otaMode);
79
+ if (!Update.begin (UPDATE_SIZE_UNKNOWN, otaMode)) { // start with max available size
80
+ Update.printError (updaterError);
81
+ }
82
+ } else if (upload.status == UPLOAD_FILE_WRITE && !updaterError.length ()) {
83
+ if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ) {
84
+ Update.printError (updaterError);
85
+ }
86
+ } else if (upload.status == UPLOAD_FILE_END && !updaterError.length ()) {
87
+ if (!Update.end (true )) { // true to set the size to the current progress
88
+ Update.printError (updaterError);
89
+ }
90
+ } else if (upload.status == UPLOAD_FILE_ABORTED) {
91
+ Update.end ();
92
+ }
93
+ delay (0 ); });
94
+
95
+ webServer.on (" /chipspecs" , HTTP_GET, [&]() {
96
+ String chipSpecs = ESP.getChipModel ();
97
+ chipSpecs += " (" + String (ESP.getFlashChipSize () >> 20 ) + " MB)" ;
98
+ webServer.send (200 , " text/plain" , chipSpecs.c_str ());
99
+ });
100
+
101
+ webServer.on (" /sbversion" , HTTP_GET, [&]() {
102
+ webServer.send (200 , " text/plain" , __COMPILED_APP_VERSION__);
103
+ });
58
104
59
- // setup routes
60
- httpUpdater.setup (&webServer, " /" );
61
105
webServer.onNotFound ([]() {
62
106
webServer.sendHeader (" Location" , " /" );
63
107
webServer.send (302 , " text/plain" , " " );
64
108
});
65
109
110
+ webServer.begin ();
111
+
112
+ #ifndef MYCILA_SAFEBOOT_NO_MDNS
113
+ MDNS.addService (" http" , " tcp" , 80 );
114
+ #endif
115
+
116
+ LOG (" Web Server started\n " );
117
+ }
118
+
119
+ static void set_next_partition_to_boot () {
120
+ const esp_partition_t * partition = esp_partition_find_first (esp_partition_type_t ::ESP_PARTITION_TYPE_APP, esp_partition_subtype_t ::ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr );
121
+ if (partition) {
122
+ esp_ota_set_boot_partition (partition);
123
+ }
124
+ }
125
+
126
+ static void start_network_manager () {
66
127
// load ESPConnect configuration
67
128
espConnect.loadConfiguration (espConnectConfig);
68
129
espConnect.setBlocking (true );
@@ -105,25 +166,43 @@ void setup() {
105
166
}
106
167
LOG (" IP: %s\n " , espConnect.getIPAddress ().toString ().c_str ());
107
168
LOG (" Hostname: %s\n " , espConnect.getHostname ().c_str ());
169
+ }
108
170
109
- // start http
110
- webServer.begin ();
111
- LOG (" Web Server started\n " );
112
-
171
+ static void start_mdns () {
113
172
#ifndef MYCILA_SAFEBOOT_NO_MDNS
114
- // Start mDNS
115
173
MDNS.begin (espConnectConfig.hostname .c_str ());
116
- MDNS. addService ( " http " , " tcp " , 80 );
174
+ LOG ( " mDNS started \n " );
117
175
#endif
176
+ }
118
177
119
- // Start OTA
178
+ static void start_arduino_ota () {
120
179
ArduinoOTA.setHostname (espConnectConfig.hostname .c_str ());
121
180
ArduinoOTA.setRebootOnSuccess (true );
122
181
ArduinoOTA.setMdnsEnabled (true );
123
182
ArduinoOTA.begin ();
124
183
LOG (" OTA Server started on port 3232\n " );
125
184
}
126
185
186
+ void setup () {
187
+ #ifdef SAFEBOOT_LOGGING
188
+ Serial.begin (115200 );
189
+ #if ARDUINO_USB_CDC_ON_BOOT
190
+ Serial.setTxTimeoutMs (0 );
191
+ delay (100 );
192
+ #else
193
+ while (!Serial)
194
+ yield ();
195
+ #endif
196
+ #endif
197
+
198
+ LOG (" Version: %s\n " , __COMPILED_APP_VERSION__);
199
+ set_next_partition_to_boot ();
200
+ start_network_manager ();
201
+ start_mdns ();
202
+ start_web_server ();
203
+ start_arduino_ota ();
204
+ }
205
+
127
206
void loop () {
128
207
webServer.handleClient ();
129
208
ArduinoOTA.handle ();
0 commit comments