-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncWebServer.cpp
More file actions
224 lines (190 loc) · 6.04 KB
/
AsyncWebServer.cpp
File metadata and controls
224 lines (190 loc) · 6.04 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
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
// For project PSUFan
//
#include <ESP8266SSDP.h>
#include <StreamString.h>
#include <ArduinoJson.h>
#include "AsyncWebServer.h"
//SSDP properties
constexpr auto modelName = "PSU Fan control";
constexpr auto friendlyName = "PSU Fan control";
constexpr auto modelNumber = "";
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
AsyncWebSocket wsd("/wsd");
extern bool manual;
///////////////////////////////////// SSDP network discovery //////////////////////////////////////
void setupSSDP() {
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setDeviceType("upnp:rootdevice");
SSDP.setModelName(modelName);
SSDP.setModelNumber(modelNumber);
SSDP.begin();
server.on("/description.xml", HTTP_GET, [](AsyncWebServerRequest* request) {
char hostName[25] = "PSU Fan control";
char buf[7];
StreamString output;
if (output.reserve(1024)) {
IPAddress ip = WiFi.localIP();
uint32_t chipId = ESP.getChipId();
output.printf(ssdpTemplate,
ip[0], ip[1], ip[2], ip[3],
hostName,
ip[0], ip[1], ip[2], ip[3],
chipId,
modelName,
modelNumber,
(uint8_t)((chipId >> 16) & 0xff),
(uint8_t)((chipId >> 8) & 0xff),
(uint8_t)chipId & 0xff
);
request->send(200, "text/xml", (String)output);
}
else {
request->send(500);
}
});
}
//Return a string with the system parameters as a json encapsulated in a function for processing on the browser
extern int fanSpeed;
extern int temperature;
extern bool fanState;
void sendSettings() {
const size_t capacity = JSON_OBJECT_SIZE(20);
DynamicJsonDocument root(capacity);
// Add values in the object
root["temp"] = temperature;
root["fan"] = fanSpeed;
root["fanbtn"] = fanState;
String jsonString;
serializeJson(root, jsonString);
ws.textAll(jsonString);
}
void onRequest(AsyncWebServerRequest *request) {
//Handle Unknown Request
request->send(404);
}
void onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
//Handle body
}
void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
//Handle upload
}
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
//Handle WebSocket event
}
void setupWebserver() {
// Start and init debug winsocket
ws.onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{
Serial.printf("wsd event: %d\n", type);
if (type == WS_EVT_CONNECT)
{
//Serial.printf("wsd[%s][%u] connected\n", server->url(), client->id());
client->ping();
}
});
server.addHandler(&ws);
// Start and init debug winsocket
wsd.onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{
//Serial.printf("wsd event: %d\n", type);
if (type == WS_EVT_CONNECT)
{
//Serial.printf("wsd[%s][%u] connected\n", server->url(), client->id());
client->ping();
}
});
server.addHandler(&wsd);
server.on("/devReboot", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Rebooting...");
shouldReboot = true;
});
// Reset all system parameters to default
server.on("/resetParam", HTTP_GET, [](AsyncWebServerRequest *request) {
resetParameters();
request->send(200, "text/plain", "All parameters reset to default");
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/status.html");
});
server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/status.html");
});
server.on("/setup", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send(SPIFFS, "/setup.html");
});
server.on("/refresh", HTTP_GET, [](AsyncWebServerRequest* request) {
sendSettings();
request->send(200);
});
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request) {
DEBUG_PRINTLN("settings");
String strHTML = buildSettingsXML();
request->send(200, "text/xml", strHTML);
});
server.on("/save", HTTP_POST, [](AsyncWebServerRequest *request) {
DEBUG_PRINTLN("save");
if (request->args() > 0)
{
fanOnTemp = (int)request->arg("i").toInt();
fanOffTemp = (int)request->arg("c").toInt();
saveSettings();
}
request->send(200, "text/plain", "Saved settings");
});
server.on("/fanon", HTTP_POST, [](AsyncWebServerRequest *request) {
DEBUG_PRINTLN("Fan on");
turnFan(1023);
manual = true;
request->send(200, "text/html", "Fan on");
});
server.on("/fanoff", HTTP_POST, [](AsyncWebServerRequest *request) {
DEBUG_PRINTLN("Fan off");
manual = false;
turnFan(0);
request->send(200, "text/html", "Fan off");
});
//Serve all files.
server.serveStatic("/", SPIFFS, "/", "max-age=2592000");
// 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);
setupSSDP();
server.begin();
Serial.println("Server setup");
}
void sendWsMessage(String message, String key) {
String jsonString;
// Add values in the object
const size_t capacity = JSON_OBJECT_SIZE(4);
DynamicJsonDocument root(capacity);
root[key] = message;
serializeJson(root, jsonString);
ws.textAll(jsonString);
}
void setupOTAUpdate(void)
{
// Authentication password
ArduinoOTA.setPassword(otaPassword);
ArduinoOTA.onStart([]() {
DEBUG_PRINTLN("Start");
});
ArduinoOTA.onEnd([]() {
DEBUG_PRINTLN("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) DEBUG_PRINTLN("Auth Failed");
else if (error == OTA_BEGIN_ERROR) DEBUG_PRINTLN("Begin Failed");
else if (error == OTA_CONNECT_ERROR) DEBUG_PRINTLN("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) DEBUG_PRINTLN("Receive Failed");
else if (error == OTA_END_ERROR) DEBUG_PRINTLN("End Failed");
});
ArduinoOTA.begin();
}