-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.h
More file actions
244 lines (220 loc) · 7.77 KB
/
WebServer.h
File metadata and controls
244 lines (220 loc) · 7.77 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <stdio.h>
#include "Config.h"
#include "Sensors.h"
#include "MQTTManager.h"
extern ESP8266WebServer server;
extern DNSServer dns;
extern DeviceConfig config;
extern bool shouldRestart;
extern unsigned long uptimeMillis;
inline String htmlHeader() {
return F(
"<!DOCTYPE html><html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'>"
"<title>IKEAAirMonitor</title><style>"
"body{font-family:Arial,sans-serif;margin:20px;background:#f5f5f5;color:#333;}"
"nav{margin-bottom:20px;}nav a{margin-right:15px;text-decoration:none;color:#0366d6;}"
".card{background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 4px rgba(0,0,0,0.1);}"
"label{display:block;margin-top:10px;}"
"input{width:100%;padding:8px;margin-top:5px;border:1px solid #ccc;border-radius:4px;}"
"button{margin-top:15px;padding:10px 15px;background:#0366d6;color:#fff;border:none;border-radius:4px;}"
"</style></head><body><nav><a href='/'>Status</a><a href='/config'>Konfiguration</a></nav><div class='card'>"
);
}
inline void formatUptime(unsigned long ms, char* buf, size_t len) {
unsigned long seconds = ms / 1000;
unsigned long days = seconds / 86400;
seconds %= 86400;
unsigned long hours = seconds / 3600;
seconds %= 3600;
unsigned long minutes = seconds / 60;
seconds %= 60;
snprintf(buf, len, "%lu d %02lu:%02lu:%02lu", days, hours, minutes, seconds);
}
inline void handleRoot() {
uint16_t pm; float t, h, p;
readMeasurements(pm, t, h, p, config);
char uptimeStr[32];
formatUptime(uptimeMillis, uptimeStr, sizeof(uptimeStr));
char html[1024];
if (WiFi.status() == WL_CONNECTED) {
snprintf(html, sizeof(html),
"%s"
"<h1>Status</h1>"
"<p>PM2.5: %u µg/m³</p>"
"<p>Temperatur: %.1f °C</p>"
"<p>Luftfeuchte: %.1f %%</p>"
"<p>Luftdruck: %.1f hPa</p>"
"<p>Uptime: %s</p>"
"<p>MQTT Status: %s</p>"
"<p>OTA Status: Aktiv (Port 8266, Hostname: %s)</p>"
"</div></body></html>",
htmlHeader().c_str(),
pm, t, h, p, uptimeStr,
mqttConnected ? "Verbunden" : "Nicht verbunden",
config.hostname
);
} else {
snprintf(html, sizeof(html),
"%s"
"<h1>Status</h1>"
"<p>PM2.5: %u µg/m³</p>"
"<p>Temperatur: %.1f °C</p>"
"<p>Luftfeuchte: %.1f %%</p>"
"<p>Luftdruck: %.1f hPa</p>"
"<p>Uptime: %s</p>"
"<p>MQTT Status: %s</p>"
"<p>OTA Status: Inaktiv (WiFi nicht verbunden)</p>"
"</div></body></html>",
htmlHeader().c_str(),
pm, t, h, p, uptimeStr,
mqttConnected ? "Verbunden" : "Nicht verbunden"
);
}
server.send(200, "text/html", html);
}
inline void handleConfig() {
char html[2048];
int len = snprintf(html, sizeof(html),
"%s"
"<h1>Konfiguration</h1><form method='POST' action='/save'>"
"<label>SSID<input name='ssid' value='%s'></label>"
"<label>Passwort<input type='password' name='password' value='%s'></label>"
"<label>Hostname<input name='hostname' value='%s'></label>"
"<label>MQTT Host<input name='mqttHost' value='%s'></label>"
"<label>MQTT Port<input name='mqttPort' value='%u'></label>"
"<label>MQTT Benutzer<input name='mqttUser' value='%s'></label>"
"<label>MQTT Passwort<input type='password' name='mqttPassword' value='%s'></label>"
"<label>MQTT Topic<input name='mqttTopic' value='%s'></label>"
"<label>Sendeintervall (s)<input name='sendInterval' value='%lu'></label>"
"<label>Temperatur-Offset<input name='tempOffset' value='%.1f'></label>"
"<button type='submit'>Speichern</button></form></div></body></html>",
htmlHeader().c_str(),
config.ssid, config.password, config.hostname, config.mqttHost, config.mqttPort,
config.mqttUser, config.mqttPassword, config.mqttTopic,
config.sendInterval / 1000, config.tempOffset
);
server.send(200, "text/html", html);
}
inline void handleSave() {
// Validate and set SSID
if (server.hasArg("ssid")) {
String ssid = server.arg("ssid");
if (ssid.length() < sizeof(config.ssid)) {
ssid.toCharArray(config.ssid, sizeof(config.ssid));
}
}
// Validate and set password
if (server.hasArg("password")) {
String password = server.arg("password");
if (password.length() < sizeof(config.password)) {
password.toCharArray(config.password, sizeof(config.password));
}
}
// Validate and set hostname
if (server.hasArg("hostname")) {
String hostname = server.arg("hostname");
if (hostname.length() > 0 && hostname.length() < sizeof(config.hostname)) {
hostname.toCharArray(config.hostname, sizeof(config.hostname));
}
}
// Validate and set MQTT host
if (server.hasArg("mqttHost")) {
String mqttHost = server.arg("mqttHost");
if (mqttHost.length() < sizeof(config.mqttHost)) {
mqttHost.toCharArray(config.mqttHost, sizeof(config.mqttHost));
}
}
// Validate and set MQTT port (1-65535)
if (server.hasArg("mqttPort")) {
int port = server.arg("mqttPort").toInt();
if (port > 0 && port <= 65535) {
config.mqttPort = port;
}
}
// Validate and set MQTT user
if (server.hasArg("mqttUser")) {
String mqttUser = server.arg("mqttUser");
if (mqttUser.length() < sizeof(config.mqttUser)) {
mqttUser.toCharArray(config.mqttUser, sizeof(config.mqttUser));
}
}
// Validate and set MQTT password
if (server.hasArg("mqttPassword")) {
String mqttPassword = server.arg("mqttPassword");
if (mqttPassword.length() < sizeof(config.mqttPassword)) {
mqttPassword.toCharArray(config.mqttPassword, sizeof(config.mqttPassword));
}
}
// Validate and set MQTT topic
if (server.hasArg("mqttTopic")) {
String mqttTopic = server.arg("mqttTopic");
if (mqttTopic.length() < sizeof(config.mqttTopic)) {
mqttTopic.toCharArray(config.mqttTopic, sizeof(config.mqttTopic));
}
}
// Validate and set send interval (1-3600 seconds)
if (server.hasArg("sendInterval")) {
unsigned long interval = server.arg("sendInterval").toInt();
if (interval >= 1 && interval <= 3600) {
config.sendInterval = interval * 1000;
}
}
// Validate and set temperature offset (-50.0 to 50.0)
if (server.hasArg("tempOffset")) {
float offset = server.arg("tempOffset").toFloat();
if (offset >= -50.0f && offset <= 50.0f) {
config.tempOffset = offset;
}
}
saveConfig(config);
DBG_PRINTLN("Configuration saved");
WiFi.mode(WIFI_STA);
WiFi.hostname(config.hostname);
WiFi.begin(config.ssid, config.password);
// Note: WiFi connection will be checked in next loop iteration
// This is in handleSave() which is called from server.handleClient(),
// so blocking here is acceptable for configuration save
char html[512];
if (WiFi.status() == WL_CONNECTED) {
IPAddress ip = WiFi.localIP();
snprintf(html, sizeof(html),
"%s"
"<p>Verbunden mit %s</p>"
"<p>IP: %u.%u.%u.%u</p>"
"<p>Neustart in 5s...</p></div></body></html>",
htmlHeader().c_str(),
config.ssid, ip[0], ip[1], ip[2], ip[3]
);
} else {
snprintf(html, sizeof(html),
"%s"
"<p>Verbindung fehlgeschlagen.</p>"
"<p>Neustart in 5s...</p></div></body></html>",
htmlHeader().c_str()
);
}
server.send(200, "text/html", html);
shouldRestart = true;
}
inline void setupWeb() {
server.on("/", handleRoot);
server.on("/config", handleConfig);
server.on("/save", HTTP_POST, handleSave);
server.begin();
DBG_PRINTLN("Web server started");
}
inline void startAP() {
WiFi.mode(WIFI_AP);
WiFi.softAP("IKEAAirMonitor-Setup");
dns.start(53, "*", WiFi.softAPIP());
DBG_PRINTLN("Starting setup access point");
setupWeb();
}
inline void handleWeb() {
dns.processNextRequest();
server.handleClient();
}