3 #include <ESPAsyncWebServer.h>
4 #include <StreamString.h>
7 bool isApEnabled = false;
8 bool isWebStarted = false;
10 bool pendingWiFi = false;
11 bool pendingAuth = false;
13 AsyncWebServer server(80);
14 AsyncEventSource events("/events");
21 void reportChange(const __FlashStringHelper* name) {
23 ConfigParameter* param = cfg.getParam(name);
25 switch (param->getType()) {
27 sprintf(buf,"{\"%s\":%s}", name, param->getBoolValue()?"true":"false");
30 sprintf(buf,"{\"%s\":%d}", name, param->getIntValue());
33 sprintf(buf,"{\"%s\":%f}", name, param->getFloatValue());
36 sprintf(buf,"{\"%s\":\"%s\"}", name, param->getCharValue());
39 events.send(buf,"update",millis());
43 void reportMessage(const __FlashStringHelper* msg) {
45 strcpy_P(buf, (PGM_P)msg);
46 events.send(buf,"message",millis());
49 void sendInitial(AsyncEventSourceClient *client) {
50 String mac = WiFi.macAddress();
52 sprintf(buf,"{\"_mac\":\"%s\",\"_weather\":\"%s\"}", mac.c_str(), weatherData);
53 client->send(buf,"update",millis());
59 sprintf(buf,"{\"_weather\":\"%s\"}",weatherData);
60 events.send(buf,"update",millis());
63 void sendKeepalive() {
64 static unsigned long lastMillis = 0;
65 static unsigned long uptimeCorrection = 0;
66 unsigned long currentMillis = millis();
67 unsigned long uptime = millis()/1000;
68 if (currentMillis < lastMillis) {
69 uptimeCorrection += lastMillis/1000;
71 lastMillis = millis();
72 uptime = uptime + uptimeCorrection;
73 int days = uptime / 86400;
74 uptime = uptime % 86400;
75 int hrs = uptime / 3600;
76 uptime = uptime % 3600;
77 int mins = uptime / 60;
79 int heap = ESP.getFreeHeap();
80 int rssi = WiFi.RSSI();
81 struct tm* timeinfo = localtime(&last_sync);
82 bool changed = cfg.getTimestamp() != 0;
83 char sync[16] = "--:--:--";
85 sprintf(sync, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
89 sprintf(buf,"{\"_uptime\":\"%d д %d ч %d % м %d с\", \"_date\":\"%02d.%2d.%04d\", \"_time\":\"%02d:%02d\",\"_heap\":\"%d б\", \"_rssi\":\"%d\", \"_last_sync\":\"%s\", \"_changed\":%s}", days, hrs, mins, uptime, dd, mm, yy, hh, mi, heap, rssi, sync, changed?"true":"false");
91 sprintf(buf,"{\"_uptime\":\"%d ч %d % м %d с\", \"_date\":\"%02d.%2d.%04d\", \"_time\":\"%02d:%02d\",\"_heap\":\"%d б\", \"_rssi\":\"%d\", \"_last_sync\":\"%s\", \"_changed\":%s}", hrs, mins, uptime, dd, mm, yy, hh, mi, heap, rssi, sync, changed?"true":"false");
93 sprintf(buf,"{\"_uptime\":\"%d м %d с\", \"_date\":\"%02d.%2d.%04d\", \"_time\":\"%02d:%02d\",\"_heap\":\"%d б\", \"_rssi\":\"%d\", \"_last_sync\":\"%s\", \"_changed\":%s}", mins, uptime, dd, mm, yy, hh, mi, heap, rssi, sync, changed?"true":"false");
95 events.send(buf,"update",millis());
98 void apply(const char* name) {
99 if (strcmp(name,"sta_ssid") == 0 || strcmp(name,"sta_psk") == 0) {
103 } else if (strcmp(name,"ap_ssid") == 0 || strcmp(name,"ap_psk") == 0) {
107 } else if (strcmp(name,"auth_user") == 0 || strcmp(name,"auth_pwd") == 0) {
109 } else if (strcmp(name,"ntp_server") == 0 || strcmp(name,"tz") == 0) {
111 } else if (strcmp(name,"pin_sda") == 0 || strcmp(name,"pin_scl") == 0 ||
112 strcmp(name,"i2c_speed") == 0 || strcmp(name,"enable_rtc") == 0 ||
113 strcmp(name,"enable_button") == 0 || strcmp(name,"button_pin") == 0 || strcmp(name,"button_inversed") == 0 ||
114 strcmp(name,"enable_buzzer") == 0 || strcmp(name,"buzzer_pin") == 0) {
116 } else if (strcmp(name,"pin_din") == 0 || strcmp(name,"pin_clk") == 0 ||
117 strcmp_P(name,"pin_cs") == 0 || strcmp(name,"led_modules") == 0){
119 } else if (strcmp(name,"panel_brightness_day") == 0 || strcmp_P(name,"panel_brightness_night") == 0 ||
120 strcmp_P(name,"day_from") == 0 || strcmp(name,"night_from") == 0){
121 setPanelBrightness();
125 char* actionScheduled = nullptr;
126 unsigned long millisScheduled = 0;
138 strncpy(auth_user,cfg.getCharValue(F("auth_user")),31);
139 strncpy(auth_pwd,cfg.getCharValue(F("auth_pwd")),31);
141 server.on("/action", HTTP_GET, [](AsyncWebServerRequest* request) {
142 if (auth_user && auth_pwd && auth_user[0] && auth_pwd[0] && !request->authenticate(auth_user, auth_pwd)) {
143 return request-> requestAuthentication();
145 if(request->hasParam("name")) {
146 const char* action = request->getParam("name")->value().c_str();
147 if (strcmp(action,"restart") == 0) {
149 request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки WiFi\", \"page\":\"wifi\"}");
150 } else if (pendingAuth) {
151 request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки авторизации\", \"page\":\"system\"}");
153 request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Перезагружаюсь\"}");
154 millisScheduled = millis();
155 actionScheduled = "restart";
157 } else if (strcmp(action,"wifi") == 0) {
159 request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки авторизации\", \"page\":\"system\"}");
161 request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Применяю настройки\"}");
162 millisScheduled = millis();
163 actionScheduled = "wifi";
165 } else if (strcmp(action,"auth") == 0) {
166 request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Применяю настройки\"}");
167 millisScheduled = millis();
168 actionScheduled = "auth";
169 } else if (strcmp(action,"save") == 0) {
171 request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки WiFi\", \"page\":\"wifi\"}");
172 } else if (pendingAuth) {
173 request->send(200,"application/json", "{\"result\":\"FAILED\",\"message\":\"Не применены настройки авторизации\", \"page\":\"system\"}");
175 request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Сохраняю настройки\"}");
176 millisScheduled = millis();
177 actionScheduled = "save";
179 } else if (strcmp(action,"time") == 0) {
180 if(request->hasParam("timestamp")) {
181 unsigned long timestamp = atoi(request->getParam("timestamp")->value().c_str());
183 timeval tv = { timestamp, 0 };
184 settimeofday(&tv, nullptr);
186 Serial.println(F("Время установлено вручную"));
187 RTC.adjust(DateTime(timestamp));
190 request->send(200,"application/json", "{\"result\":\"OK\",\"message\":\"Устанавливаю время\"}");
192 request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}");
195 request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Unsupported action\"}");
198 request->send(500, "text/plain", "{\"result\":\"FAILED\",\"message\":\"Not all parameters set\"}");
202 server.on("/wifi/scan", HTTP_GET, [](AsyncWebServerRequest* request) {
203 if (auth_user && auth_pwd && auth_user[0] && auth_pwd[0] && !request->authenticate(auth_user, auth_pwd)) {
204 return request-> requestAuthentication();
207 int n = WiFi.scanComplete();
209 WiFi.scanNetworks(true);
211 for (int i = 0; i < n; ++i) {
214 json += "\"rssi\":" + String(WiFi.RSSI(i));
215 json += ",\"ssid\":\"" + WiFi.SSID(i) + "\"";
216 json += ",\"bssid\":\"" + WiFi.BSSIDstr(i) + "\"";
217 json += ",\"channel\":" + String(WiFi.channel(i));
218 json += ",\"secure\":" + String(WiFi.encryptionType(i));
219 json += ",\"hidden\":" + String(WiFi.isHidden(i) ? "true" : "false");
223 if (WiFi.scanComplete() == -2) {
224 WiFi.scanNetworks(true);
228 request->send(200, "application/json", json);
232 server.on("/config/get", HTTP_GET, [](AsyncWebServerRequest* request) {
233 if (auth_user && auth_pwd && auth_user[0] && auth_pwd[0] && !request->authenticate(auth_user, auth_pwd)) {
234 return request-> requestAuthentication();
236 AsyncResponseStream* s = request->beginResponseStream("application/json");
238 for (int i = 0; i < cfg.getParametersCount(); i++) {
239 ConfigParameter* param = cfg.getParameter(i);
240 if (i) s->print(",");
242 s->print(param->getID());
244 switch (param->getType()) {
246 s->print(param->getBoolValue() ? "true" : "false");
249 s->print(param->getIntValue());
252 s->print(param->getFloatValue());
256 s->print(param->getCharValue());
265 server.on("/config/set", HTTP_GET, [](AsyncWebServerRequest* request) {
266 if (auth_user && auth_pwd && auth_user[0] && auth_pwd[0] && !request->authenticate(auth_user, auth_pwd)) {
267 return request-> requestAuthentication();
269 if(request->hasParam("name") && (request->hasParam("value"))) {
270 const char* name = request->getParam("name")->value().c_str();
271 const char* value = request->getParam("value")->value().c_str();
272 Serial.print(name); Serial.print(" = "); Serial.println(value);
274 ConfigParameter* param = cfg.getParam(name);
276 switch (param->getType()) {
278 cfg.setValue(name, strcmp(value, "true")==0);
279 sprintf(buf,"{\"%s\":%s}", name, param->getBoolValue()?"true":"false");
282 cfg.setValue(name, atoi(value));
283 sprintf(buf,"{\"%s\":%d}", name, param->getIntValue());
286 cfg.setValue(name, atof(value));
287 sprintf(buf,"{\"%s\":%f}", name, param->getFloatValue());
290 cfg.setValue(name, value);
291 sprintf(buf,"{\"%s\":\"%s\"}", name, param->getCharValue());
296 request->send(500, "text/plain", "Unknown parameter name");
299 request->send(200,"application/json",buf);
301 request->send(500, "text/plain", "Not all parameters set");
305 server.serveStatic("ui", LittleFS, "/ui.json").setAuthentication(auth_user,auth_pwd);
307 server.serveStatic("/", LittleFS, "/web/").setDefaultFile("index.html").setAuthentication(auth_user,auth_pwd);
309 server.onNotFound([](AsyncWebServerRequest *request){
310 request->send(404,"text/plain","Not found");
313 events.onConnect([](AsyncEventSourceClient *client){
317 events.setAuthentication(auth_user,auth_pwd);
319 server.addHandler(&events);
323 tKeepalive.attach(2, sendKeepalive);
327 #define CFG_AUTOSAVE 15
330 static char storedSSID[64];
331 static char storedPSK[64];
332 static bool connectInProgress = false;
333 static unsigned long connectMillis = 0;
334 if (actionScheduled && millis()>millisScheduled+300) {
335 Serial.print(F("Запланированная операция ")); Serial.println(actionScheduled);
337 if (strcmp(actionScheduled,"restart") == 0) {
340 } else if (strcmp(actionScheduled,"auth") == 0) {
341 Serial.println("Логин/пароль изменены");
342 strncpy(auth_user,cfg.getCharValue(F("auth_user")),31);
343 strncpy(auth_pwd,cfg.getCharValue(F("auth_pwd")),31);
345 } else if (strcmp(actionScheduled,"wifi") == 0) {
346 Serial.println("Применяю настройки сети");
347 strcpy(storedSSID,WiFi.SSID().c_str());
348 strcpy(storedPSK,WiFi.psk().c_str());
350 WiFi.begin(cfg.getCharValue("sta_ssid"),cfg.getCharValue("sta_psk"));
351 connectInProgress = true;
352 connectMillis = millis();
353 } else if (strcmp(actionScheduled,"save") == 0 && !pendingWiFi && !pendingAuth) {
356 actionScheduled = nullptr;
358 if (connectInProgress && (millis() > connectMillis + 1000)) {
359 if (WiFi.status() == WL_CONNECTED) {
361 sprintf(buf,"Подключен к %s, IP=%s", WiFi.SSID(), WiFi.localIP().toString().c_str());
365 connectInProgress = false;
366 } else if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL || ((WiFi.status() == WL_CONNECTION_LOST || WiFi.status() == WL_DISCONNECTED) && (millis()>connectMillis+1000*cfg.getIntValue("sta_wait")))) {
367 Serial.println(F("Подключение не удалось, возвращаю прежние настройки"));
368 message(F("Подключение не удалось, возвращаю прежние настройки"),1);
369 WiFi.begin(storedSSID, storedPSK);
370 connectInProgress = false;
374 if (!pendingWiFi && !pendingAuth && cfg.getTimestamp() && cfg.getTimestamp() < now - CFG_AUTOSAVE) {
376 reportMessage(F("Настройки сохранены"));
377 Serial.println(F("Настройки сохранены"));