diff --git a/vertushka_esp32s3/vertushka_esp32s3.ino b/vertushka_esp32s3/vertushka_esp32s3.ino index bcc48be..6559d55 100644 --- a/vertushka_esp32s3/vertushka_esp32s3.ino +++ b/vertushka_esp32s3/vertushka_esp32s3.ino @@ -15,6 +15,8 @@ #include #include +#include +#include #include #include #include @@ -90,6 +92,16 @@ static bool schedule_global_on = false; static int8_t schedule_active_day = -1; // -1 = нет; 0..6 = индекс дня static int8_t last_checked_day = -1; +// ==== WiFi + NTP ==== +static String wifi_ssid; // загружается из NVS +static String wifi_pass; +static bool wifi_started = false; +static bool time_synced = false; +static uint32_t last_wifi_check_ms = 0; +#define NTP_TZ_OFFSET_SEC (3 * 3600) // Москва UTC+3 +#define NTP_DST_OFFSET_SEC 0 +#define NTP_SERVER "pool.ntp.org" + static inline uint16_t calibRpm(uint8_t idx) { return rpm_min + idx * RPM_STEP; } #define INIT_VER 2200 // PWM "стоп" — ESC в нейтрали @@ -944,6 +956,80 @@ static void scheduleSave() { Serial.println("schedule saved"); } +// ==== WiFi: загрузка/сохранение creds, попытка подключения, NTP ==== +static void wifiLoadCreds() { + wifi_ssid = prefs.getString("w_ssid", ""); + wifi_pass = prefs.getString("w_pass", ""); +} +static void wifiSaveCreds(const String &ssid, const String &pass) { + prefs.putString("w_ssid", ssid); + prefs.putString("w_pass", pass); + wifi_ssid = ssid; + wifi_pass = pass; + Serial.printf("WiFi creds saved: SSID=%s\n", ssid.c_str()); +} +static void wifiBeginIfHasCreds() { + if (wifi_ssid.length() == 0) { + Serial.println("WiFi: no creds, skipping. Type 'wifi ' in Serial."); + return; + } + Serial.printf("WiFi: connecting to %s\n", wifi_ssid.c_str()); + WiFi.mode(WIFI_STA); + WiFi.begin(wifi_ssid.c_str(), wifi_pass.c_str()); + wifi_started = true; +} +static void ntpSetup() { + configTime(NTP_TZ_OFFSET_SEC, NTP_DST_OFFSET_SEC, NTP_SERVER); + Serial.println("NTP requested (pool.ntp.org, UTC+3)"); +} + +// Обрабатывает Serial-команду "wifi SSID PASS" +static void wifiPollSerialCommand() { + if (!Serial.available()) return; + String line = Serial.readStringUntil('\n'); + line.trim(); + if (!line.startsWith("wifi ")) return; + // wifi SSID PASS — берём после первого пробела SSID, остальное — пароль + int sp1 = line.indexOf(' '); + int sp2 = line.indexOf(' ', sp1 + 1); + if (sp1 < 0 || sp2 < 0) { + Serial.println("usage: wifi "); + return; + } + 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; + wifiBeginIfHasCreds(); +} + +// Поллинг состояния WiFi/NTP в loop. После connect — запрашиваем NTP +// и периодически проверяем что время уже синхронизировано. +static void wifiPoll() { + if (millis() - last_wifi_check_ms < 1000) return; + 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(); + ntp_requested = true; + } + struct tm tinfo; + if (getLocalTime(&tinfo, 0)) { + time_synced = true; + Serial.printf("NTP synced: %04d-%02d-%02d %02d:%02d:%02d\n", + tinfo.tm_year + 1900, tinfo.tm_mon + 1, tinfo.tm_mday, + tinfo.tm_hour, tinfo.tm_min, tinfo.tm_sec); + } + } +} + // ==== Setup ==== void setup() { Serial.begin(115200); @@ -955,6 +1041,8 @@ void setup() { step_idx = prefs.getUChar("idx", 0); calibLoad(); scheduleLoad(); + wifiLoadCreds(); + wifiBeginIfHasCreds(); if (step_idx >= calib_count) step_idx = 0; Serial.printf("Loaded step_idx=%d, calib_count=%u, rpm_min=%u\n", step_idx, calib_count, rpm_min); @@ -1014,6 +1102,10 @@ void loop() { rampTick(); } + // WiFi: поллинг подключения + NTP, обработка Serial-команды + wifiPoll(); + wifiPollSerialCommand(); + // Перерисовка из callback'ов (нельзя дёргать LVGL из IRQ-контекста callback'а) if (ui_dirty) { ui_dirty = false;