diff --git a/vertushka_esp32s3/vertushka_esp32s3.ino b/vertushka_esp32s3/vertushka_esp32s3.ino index 22942d2..f7b89a5 100644 --- a/vertushka_esp32s3/vertushka_esp32s3.ino +++ b/vertushka_esp32s3/vertushka_esp32s3.ino @@ -98,6 +98,7 @@ static String wifi_ssid; // загружается из NVS static String wifi_pass; static bool wifi_started = false; static bool time_synced = false; +static bool ntp_requested = false; // сбрасывается при reconnect/TZ-смене static uint32_t last_wifi_check_ms = 0; static int8_t tz_offset_hours = 3; // UTC offset, default Москва +3 static bool tz_pick_active = false; // редактируется ли TZ энкодером @@ -109,6 +110,10 @@ static WebServer web_server(80); static DNSServer dns_server; static bool ap_active = false; static String ap_ssid_str; +static String ap_pin; // PIN-пароль AP — показывается на экране +static bool pending_apply_creds = false; +static String pending_ssid; +static String pending_pass; static inline uint16_t calibRpm(uint8_t idx) { return rpm_min + idx * RPM_STEP; } @@ -832,12 +837,13 @@ static void updateWifiUi() { if (!wifi_status_lbl) return; if (ap_active) { - // Режим настройки — показываем инструкцию - char buf[120]; - snprintf(buf, sizeof(buf), "Подключитесь к сети\n%s", ap_ssid_str.c_str()); + // Режим настройки — показываем сеть, PIN и адрес portal + char buf[160]; + snprintf(buf, sizeof(buf), "Сеть: %s\nПароль: %s", + ap_ssid_str.c_str(), ap_pin.c_str()); lv_label_set_text(wifi_status_lbl, buf); lv_obj_set_style_text_color(wifi_status_lbl, COLOR_ACCENT, 0); - lv_label_set_text(wifi_ip_lbl, "192.168.4.1"); + lv_label_set_text(wifi_ip_lbl, "http://192.168.4.1"); lv_label_set_text(wifi_btn_setup_lbl, "Отмена"); } else if (WiFi.status() == WL_CONNECTED) { char buf[80]; @@ -973,9 +979,10 @@ static void buildUi() { lv_obj_set_style_bg_color(scr, COLOR_BG, 0); lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0); - // Тап по экрану — toggle СТОП/РАБОТА (дублирует клик BOOT) + // ДЛИННОЕ нажатие по экрану — toggle СТОП/РАБОТА (защита от случайного касания + // корпуса при работающем моторе). Короткий тап игнорируется. lv_obj_add_flag(scr, LV_OBJ_FLAG_CLICKABLE); - lv_obj_add_event_cb(scr, onScreenTap, LV_EVENT_CLICKED, nullptr); + lv_obj_add_event_cb(scr, onScreenTap, LV_EVENT_LONG_PRESSED, nullptr); lv_obj_add_event_cb(scr, onMainGesture, LV_EVENT_GESTURE, nullptr); // ---- Дуга по окружности (фон + индикатор) ---- @@ -1061,6 +1068,8 @@ static void tzApply(int delta) { prefs.putChar("w_tz", tz_offset_hours); if (WiFi.status() == WL_CONNECTED) { configTime((long)tz_offset_hours * 3600L, NTP_DST_OFFSET_SEC, NTP_SERVER); + // Сбрасываем кэш-флаг чтобы wifiPoll переинициализировал NTP при следующем reconnect + ntp_requested = true; // configTime уже вызван — флаг согласован } ui_dirty = true; } @@ -1130,8 +1139,7 @@ static void onButtonClick(void *handle, void *user) { ui_dirty = true; } else if (onScheduleScreen()) { // Циклически переключаем поля: -1 → 0 → 1 → 2 → 3 → 4 → -1 - sch_field_pick = (sch_field_pick + 2) % 6 - 1; - if (sch_field_pick < -1) sch_field_pick = -1; + sch_field_pick = (sch_field_pick >= 4) ? -1 : (int8_t)(sch_field_pick + 1); ui_dirty = true; } else if (onCalibScreen()) { // Клик BOOT = ✓ NEXT/START (дублирует кнопку OK) @@ -1190,7 +1198,6 @@ static void onScheduleGesture(lv_event_t *e) { } } // Свайп пальцем ВНИЗ с WiFi-экрана → возврат на главный (control-center-стиль) -static void updateWifiUi(); static void onWifiGesture(lv_event_t *e) { lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act()); if (dir == LV_DIR_BOTTOM) { @@ -1314,7 +1321,12 @@ static void scheduleTick() { uint16_t start = s.start_hh * 60 + s.start_mm; uint16_t end = s.end_hh * 60 + s.end_mm; - bool inside = (now_min >= start && now_min < end); + // Поддержка окна через полночь: если end= start && now_min < end); + else if (start > end) inside = (now_min >= start || now_min < end); + else inside = false; if (inside && schedule_active_day != day) { // Запуск мотора по расписанию @@ -1380,11 +1392,11 @@ static void wifiPollSerialCommand() { String ssid = line.substring(sp1 + 1, sp2); String pass = line.substring(sp2 + 1); wifiSaveCreds(ssid, pass); - // Переподключаемся WiFi.disconnect(); delay(100); - wifi_started = false; - time_synced = false; + wifi_started = false; + time_synced = false; + ntp_requested = false; wifiBeginIfHasCreds(); } @@ -1395,7 +1407,6 @@ static void wifiPoll() { last_wifi_check_ms = millis(); if (wifi_started && WiFi.status() == WL_CONNECTED && !time_synced) { - static bool ntp_requested = false; if (!ntp_requested) { Serial.printf("WiFi connected, IP=%s\n", WiFi.localIP().toString().c_str()); ntpSetup(); @@ -1433,12 +1444,29 @@ static const char *PORTAL_HTML_TPL = "" ""; +// HTML-эскейп для безопасной вставки SSID в "; + ssids += ""; } if (ssids.length() == 0) ssids = ""; String page = String(PORTAL_HTML_TPL); @@ -1446,12 +1474,14 @@ static void apHandleRoot() { web_server.send(200, "text/html; charset=utf-8", page); } -static void apStart(); -static void apStop(); - static void apHandleSave() { String ssid = web_server.arg("ssid"); String pass = web_server.arg("pass"); + // Защита от DoS NVS — ограничение длин по WPA-стандарту + if (ssid.length() == 0 || ssid.length() > 32 || pass.length() > 63) { + web_server.send(400, "text/plain; charset=utf-8", "Bad SSID/PASS length"); + return; + } String resp = "" "