M1 step 2: Wi-Fi Service, Saved Networks, NTP, Settings page

- lib/wifi (host-tested): SavedNetworks (up to 8, validated, hidden
  flag, persisted) and WifiController (joins the strongest Saved
  Network, tries hidden ones in turn, backoff 10/30/60 s, connect
  timeout, Monitoring override, radio off without Saved Networks)
- WifiService carries out the controller's actions, sets the EU
  country code, and syncs the Clock over SNTP without touching the TZ
- Wi-Fi On/Off setting; Settings > Wi-Fi page: status, add from a scan
  or a hidden network, forget
- Status Bar: W + signal bars, W? while searching, MON while monitoring
- Serial: wifi add / wifi status (replaces the step 1 heap probe)

Verified on the device: joins the test network ~5 s after boot, clock
set over NTP, ~129 KB free heap while connected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-02 14:46:07 +02:00
co-authored by Claude Opus 5.5
parent d8fe676c65
commit 1a0baf4ac7
22 changed files with 1108 additions and 48 deletions
+30 -40
View File
@@ -1,7 +1,5 @@
// roro9stack firmware entry point: wires Services, Apps and the UI loop together.
#include <M5Cardputer.h>
#include <NetworkClientSecure.h>
#include <WiFi.h>
#include "app_manager.h"
#include "apps/demo_app.h"
@@ -16,6 +14,7 @@
#include "services/clock_service.h"
#include "services/power_service.h"
#include "services/storage_service.h"
#include "services/wifi_service.h"
#include "settings.h"
#include "ui/notifier.h"
#include "ui/screen.h"
@@ -35,6 +34,8 @@ static BatteryService* battery;
static StorageService* storage;
static PowerService* power;
static ClockService* clockService;
static SavedNetworks* savedNetworks;
static WifiService* wifi;
static Notifier* notifier;
static LauncherApp launcher;
static AppManager* apps;
@@ -64,6 +65,18 @@ static StatusInfo currentStatus() {
s.sdPresent = storageState.present;
s.sdLevel = storageState.level;
s.compose = keyMapper.pendingCompose();
using WifiState = WifiController::State;
switch (wifi->state()) {
case WifiState::Connected: {
s.wifi = StatusInfo::Wifi::Connected;
int rssi = wifi->rssi();
s.wifiBars = rssi >= -60 ? 3 : rssi >= -70 ? 2 : rssi >= -80 ? 1 : 0;
break;
}
case WifiState::Monitoring: s.wifi = StatusInfo::Wifi::Monitoring; break;
case WifiState::Off: s.wifi = StatusInfo::Wifi::None; break;
default: s.wifi = StatusInfo::Wifi::Searching; break;
}
return s;
}
@@ -80,17 +93,21 @@ void setup() {
storage = new StorageService(bus);
power = new PowerService(settings);
clockService = new ClockService(settings, bus);
savedNetworks = new SavedNetworks(nvs);
savedNetworks->load();
wifi = new WifiService(settings, *savedNetworks, *clockService);
notifier = new Notifier(bus, settings);
notifier->onShow = [](uint32_t now, uint32_t until) { power->onNotification(now, until); };
services.add(*power);
services.add(*clockService);
services.add(*battery);
services.add(*storage);
services.add(*wifi);
apps = new AppManager(launcher);
launcher.setManager(*apps);
apps->registerApp({"settings", "Settings", false,
new SettingsApp({settings, bus, *apps, *battery, *storage, *clockService})});
new SettingsApp({settings, bus, *apps, *battery, *storage, *clockService, *wifi, *savedNetworks})});
apps->registerApp({"demo", "Widget demo", true, new DemoApp(bus)});
apps->registerApp({"setup", "Setup", true, new SetupApp(settings, *apps)});
@@ -103,41 +120,6 @@ void setup() {
Serial.printf("%s %s ready, free heap %u\n", kProductName, versionString(), ESP.getFreeHeap());
}
// Dev aid (M1 step 1): heap cost of Wi-Fi and one TLS connection. Credentials come over serial only.
static void memoryProbe(const String& ssid, const String& pass) {
auto heap = [](const char* stage) {
Serial.printf("probe %-18s heap %6u min %6u largest block %6u\n", stage, ESP.getFreeHeap(),
ESP.getMinFreeHeap(), heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
};
heap("before wifi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), pass.c_str());
uint32_t start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) delay(100);
if (WiFi.status() != WL_CONNECTED) {
Serial.printf("probe wifi failed, status %d\n", WiFi.status());
heap("wifi failed");
WiFi.disconnect(true);
return;
}
Serial.printf("probe wifi connected, rssi %d, ip %s\n", WiFi.RSSI(), WiFi.localIP().toString().c_str());
heap("wifi connected");
{
NetworkClientSecure tls;
tls.setInsecure(); // measurement only; M1 verifies certificates
if (tls.connect("irc.libera.chat", 6697)) {
heap("tls connected");
tls.stop();
} else {
Serial.println("probe tls connect failed");
}
}
heap("tls closed");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
heap("wifi off");
}
// Dev aid: serial commands to drive the UI without the keyboard.
static void serialCommands() {
static String line;
@@ -157,10 +139,18 @@ static void serialCommands() {
: k == "right" ? Key::Right : k == "back" ? Key::Back : k == "home" ? Key::Home : Key::Select;
if (!power->onKey(millis())) apps->handleKey(KeyEvent::of(key));
}
if (line.startsWith("probe ")) { // probe <ssid>\t<password>
if (line.startsWith("wifi add ")) { // wifi add <ssid>\t<password>: credentials never touch the repo
int tab = line.indexOf('\t');
if (tab > 0) memoryProbe(line.substring(6, tab), line.substring(tab + 1));
std::string ssid = (tab > 0 ? line.substring(9, tab) : line.substring(9)).c_str();
std::string pass = tab > 0 ? line.substring(tab + 1).c_str() : "";
std::string error = savedNetworks->add(ssid, pass);
Serial.printf("wifi add: %s\n", error.empty() ? "ok" : error.c_str());
wifi->savedNetworksChanged();
}
if (line == "wifi status")
Serial.printf("wifi: state %d ssid '%s' rssi %d clock %s heap %u min %u\n", (int)wifi->state(),
wifi->ssid().c_str(), wifi->rssi(), clockService->displayTime().c_str(), ESP.getFreeHeap(),
ESP.getMinFreeHeap());
if (line == "sound off") settings.setBool(Setting::Sound, false);
if (line == "sound on") settings.setBool(Setting::Sound, true);
if (line == "short") {