-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEspAsyncExampleUpdateInoFromBrowser.ino
More file actions
180 lines (158 loc) · 6.39 KB
/
EspAsyncExampleUpdateInoFromBrowser.ino
File metadata and controls
180 lines (158 loc) · 6.39 KB
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
//EspAsyncExampleUpdateInoFromBrowser.ino
// taken from https://github.com/me-no-dev/ESPAsyncWebServer#setting-up-the-server 20200702
//#include "ESPAsyncTCP.h" // for 8266
#include "AsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include <SPIFFS.h> // fix error: 'SPIFFS' was not declared in this scope
#include <Update.h> // required to fix 'Update' was not declared in this scope
AsyncWebServer server(80);
AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws
AsyncEventSource events("/events"); // event source (Server-Sent events)
const char* ssid = "your-ssid";
const char* password = "your-pass";
const char* http_username = "admin";
const char* http_password = "admin";
//flag to use from web update to reboot the ESP
bool shouldReboot = false;
void onRequest(AsyncWebServerRequest *request) {
Serial.printf("onRequest 404:%s\n", request->url().c_str());
//Handle Unknown Request
request->send(404);
}
void onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
Serial.printf("onBody:%s\n", request->url().c_str());
//Handle body
}
void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
Serial.printf("onUpload:%s\n", request->url().c_str());
//Handle upload
}
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
//Handle WebSocket event
}
void setup() {
Serial.begin(115200);
Serial.printf("it begins\n");
WiFi.mode(WIFI_STA);
delay(2000);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
Serial.printf("maybe SSID(%s) or password(%s) needs adjusting\n", ssid, password);
return;
}
Serial.printf("Connected\n");
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
// attach AsyncWebSocket
ws.onEvent(onEvent);
server.addHandler(&ws);
// attach AsyncEventSource
server.addHandler(&events);
// respond to GET requests on URL /heap
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest * request) {
Serial.printf("get request:%s\n", request->url().c_str());
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
// upload a file to /upload
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest * request) {
Serial.printf("post request:%s\n", request->url().c_str());
request->send(200);
}, onUpload);
// send a file when /index is requested
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest * request) {
String targetFile = request->url();
targetFile += ".html";
Serial.printf(" request:%s\n", request->url().c_str());
if (SPIFFS.exists(targetFile))
{
request->send(SPIFFS, targetFile);
}
else
{
Serial.printf("spiffs says file:%s, does not exist\n", targetFile.c_str());
}
});
server.on("/", HTTP_ANY, [](AsyncWebServerRequest * request) {
String targetFile = request->url();
targetFile = "/index.html";
Serial.printf(" request:%s\n", request->url().c_str());
if (SPIFFS.exists(targetFile))
{
request->send(SPIFFS, targetFile);
}
else
{
Serial.printf("spiffs says file:%s, does not exist\n", targetFile.c_str());
}
});
// HTTP basic authentication
server.on("/login", HTTP_GET, [](AsyncWebServerRequest * request) {
Serial.printf("get request:%s\n", request->url().c_str());
if (!request->authenticate(http_username, http_password))
return request->requestAuthentication();
else
Serial.printf("request->authenticate returned true, close browser to try again\n");
request->send(200, "text/plain", "Login Success!");
});
// Simple Firmware Update Form
server.on("/update", HTTP_GET, [](AsyncWebServerRequest * request) {
Serial.printf("get request:%s\n", request->url().c_str());
request->send(200, "text/html", "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");
});
server.on("/update", HTTP_POST, [](AsyncWebServerRequest * request) {
Serial.printf("post request:%s\n", request->url().c_str());
shouldReboot = !Update.hasError();
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", shouldReboot ? "OK" : "FAIL");
response->addHeader("Connection", "close");
request->send(response);
}, [](AsyncWebServerRequest * request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
Serial.printf("Update Start: %s\n", filename.c_str());
//Update.runAsync(true); // error: 'class UpdateClass' has no member named 'runAsync'
// https://gitter.im/espressif/arduino-esp32?at=5cc1efb7a4ef097471face9c
// Me No Dev @me-no-dev Apr 26 2019 05:05 remove the call it's not needed in ESP32
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
Update.printError(Serial);
}
}
if (!Update.hasError()) {
if (Update.write(data, len) != len) {
Update.printError(Serial);
}
}
if (final) {
if (Update.end(true)) {
Serial.printf("Update Success: %uB\n", index + len);
} else {
Update.printError(Serial);
}
}
});
// attach filesystem root at URL /fs
server.serveStatic("/fs", SPIFFS, "/");
// Catch-All Handlers
// Any request that can not find a Handler that canHandle it
// ends in the callbacks below.
server.onNotFound(onRequest);
server.onFileUpload(onUpload);
server.onRequestBody(onBody);
server.begin();
}
void loop() {
if (shouldReboot) {
Serial.println("Rebooting...");
delay(100);
ESP.restart();
}
static char temp[128];
sprintf(temp, "Seconds since boot: %lu", millis() / 1000); // u to lu, format '%u' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Werror=format=]
events.send(temp, "time"); //send event "time"
}