feat: И5a WiFi STA + NTP
- Загрузка SSID/PASS из NVS (ключи w_ssid, w_pass) - При наличии креденшлс — auto STA-connect при старте - После connect — NTP sync (UTC+3 Москва, pool.ntp.org) - Serial-команда 'wifi <SSID> <PASS>' для интерактивной настройки - wifiPoll() в loop: каждую секунду проверяет статус, по connected — get NTP В Serial monitor видно: WiFi: connecting to <ssid> WiFi connected, IP=192.168.X.X NTP synced: 2026-04-28 18:42:15 Размер прошивки: 1019 КБ → 1604 КБ (+585 КБ на WiFi-стек)
This commit is contained in:
@@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <WiFi.h>
|
||||||
#include <esp_display_panel.hpp>
|
#include <esp_display_panel.hpp>
|
||||||
#include <lvgl.h>
|
#include <lvgl.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
@@ -90,6 +92,16 @@ static bool schedule_global_on = false;
|
|||||||
static int8_t schedule_active_day = -1; // -1 = нет; 0..6 = индекс дня
|
static int8_t schedule_active_day = -1; // -1 = нет; 0..6 = индекс дня
|
||||||
static int8_t last_checked_day = -1;
|
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; }
|
static inline uint16_t calibRpm(uint8_t idx) { return rpm_min + idx * RPM_STEP; }
|
||||||
|
|
||||||
#define INIT_VER 2200 // PWM "стоп" — ESC в нейтрали
|
#define INIT_VER 2200 // PWM "стоп" — ESC в нейтрали
|
||||||
@@ -944,6 +956,80 @@ static void scheduleSave() {
|
|||||||
Serial.println("schedule saved");
|
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 <SSID> <PASS>' 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 <SSID> <PASS>");
|
||||||
|
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 ====
|
// ==== Setup ====
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -955,6 +1041,8 @@ void setup() {
|
|||||||
step_idx = prefs.getUChar("idx", 0);
|
step_idx = prefs.getUChar("idx", 0);
|
||||||
calibLoad();
|
calibLoad();
|
||||||
scheduleLoad();
|
scheduleLoad();
|
||||||
|
wifiLoadCreds();
|
||||||
|
wifiBeginIfHasCreds();
|
||||||
if (step_idx >= calib_count) step_idx = 0;
|
if (step_idx >= calib_count) step_idx = 0;
|
||||||
Serial.printf("Loaded step_idx=%d, calib_count=%u, rpm_min=%u\n",
|
Serial.printf("Loaded step_idx=%d, calib_count=%u, rpm_min=%u\n",
|
||||||
step_idx, calib_count, rpm_min);
|
step_idx, calib_count, rpm_min);
|
||||||
@@ -1014,6 +1102,10 @@ void loop() {
|
|||||||
rampTick();
|
rampTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WiFi: поллинг подключения + NTP, обработка Serial-команды
|
||||||
|
wifiPoll();
|
||||||
|
wifiPollSerialCommand();
|
||||||
|
|
||||||
// Перерисовка из callback'ов (нельзя дёргать LVGL из IRQ-контекста callback'а)
|
// Перерисовка из callback'ов (нельзя дёргать LVGL из IRQ-контекста callback'а)
|
||||||
if (ui_dirty) {
|
if (ui_dirty) {
|
||||||
ui_dirty = false;
|
ui_dirty = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user