Progress Log: WiFi Manager Module(v1.8.0)¶
Task Description¶
Implemented a comprehensive WiFi management module for OSECHI that provides:
- AP Mode(Access Point) - ESP32 acts as a WiFi router
- Default behavior: Automatically starts in AP mode on boot
- SSID:
OSECHI-DETECTOR, Password:osechi2025 - IP address: 192.168.4.1(ESP32標準)
-
Allows direct device connection without external network
-
STA Mode(Station/Client) - ESP32 connects to existing WiFi network
- Non-blocking connection with timeout handling
- Auto-reconnection logic with 5-second retry interval
-
Credential storage and state management
-
State Machine - Polling-based(non-blocking)
- DISCONNECTED → CONNECTING → CONNECTED
- Timeout handling and failure recovery
- Independent from cosmic ray detection loop
Outcome¶
✅ Successfully completed
Created Files¶
- include/wifi_manager.h - Public API
- Mode control:
wifi_start_ap(),wifi_start_sta() - Connection management:
wifi_connect(),wifi_disconnect() - State queries:
wifi_get_state(),wifi_is_connected() -
AP info:
wifi_get_ap_ssid(),wifi_get_ap_ip() -
src/wifi_manager.cpp - Full implementation
- Non-blocking state machine(<10ms per update)
- 10-second connection timeout
- 5-second retry interval
- Memory-safe credential handling
- Safe to call even if WiFi hardware unavailable
Architecture¶
State Machine Flow:
[DISCONNECTED]
↓ (credentials stored)
[CONNECTING] → timeout → [FAILED]
↓ (success) ↓ (wait retry)
[CONNECTED] [RETRY_INTERVAL]
↓ (loss) ↓
[DISCONNECTED] [DISCONNECTED]
Integration Points:
setup():wifi_init()after 3-second stability delayloop():wifi_update()for state machine polling- Non-blocking: <10ms execution time, doesn't block detection
Key Features¶
- Polling-based non-blocking - No FreeRTOS threading complexity
- AP mode default - Immediate device connectivity without external network
- Graceful degradation - Safely handles missing WiFi hardware
- Credential storage - Stores SSID/password for auto-reconnection
- State visibility - Multiple query functions for debugging
Configuration¶
- AP SSID: "OSECHI-DETECTOR"(wifi_manager.cpp L44で変更可能)
- AP Password: "osechi2025"(wifi_manager.cpp L45で変更可能)
- Connection timeout: 10秒
- Retry interval: 5秒
- Compile-time control:
-DENABLE_WIFI=1で有効化
Status Reporting¶
WiFi状態はテキストコマンド経由でクエリ可能:
STATUS → WiFi接続状態を表示
GET_VERSION → ファームウェアバージョン(WiFi情報含む)
Learnings¶
- Polling vs Events - FreeRTOSのイベント駆動は複雑。シンプルなポーリングが効果的
- AP mode benefits - 外部ネットワーク不要で即座にデバイス接続可能
- Non-blocking design - detect.detected()の検出ロジックとの独立性が重要
- WiFi.h API - ESP32標準ライブラリで十分な機能を提供
- Default IP - 192.168.4.1はESP32標準のAP IPで変更不要
Design Decision: Custom wifi_state_t vs WiFi.h's wl_status_t¶
Why Custom wifi_state_t?¶
The implementation uses a custom wifi_state_t enumeration instead of directly using WiFi.h's wl_status_t:
// Custom high-level state (4 states)
typedef enum {
WIFI_STATE_DISCONNECTED = 0,
WIFI_STATE_CONNECTING = 1,
WIFI_STATE_CONNECTED = 2,
WIFI_STATE_FAILED = 3
} wifi_state_t;
// vs WiFi.h's low-level hardware state (11 states)
// WL_CONNECTED (3), WL_IDLE_STATUS (0), WL_NO_SSID_AVAIL (1),
// WL_SCAN_COMPLETED (2), WL_CONNECT_FAILED (4), WL_CONNECTION_LOST (5),
// WL_DISCONNECTED (6), WL_NO_SHIELD (255), etc.
Rationale¶
- Application Layer Abstraction
- Hardware states (
wl_status_t) are low-level and numerous (11+ values) - Application code needs simple, high-level states
-
wifi_state_tmaps multiple hardware states to meaningful application states -
Timeout Management
- Hardware alone cannot track connection timeouts (10 seconds max)
- Application layer maintains
g_connect_start_timefor timeout detection -
Impossible to implement with
wl_status_talone -
Auto-Retry Logic
- Custom 5-second retry interval after failed connection
- Requires application-level state tracking
-
g_last_retry_timeenables non-blocking retry behavior -
State Machine Design
- DISCONNECTED → CONNECTING → {CONNECTED, FAILED}
- FAILED → (wait 5s) → DISCONNECTED → (auto-reconnect)
- This high-level flow cannot be directly represented in
wl_status_t
Implementation Details¶
Internally, wifi_update() still uses hardware states:
void wifi_update(void) {
wl_status_t hw_status = WiFi.status(); // Hardware state (low-level)
switch (g_wifi_state) { // Application state (high-level)
case WIFI_STATE_CONNECTING:
if (hw_status == WL_CONNECTED) {
g_wifi_state = WIFI_STATE_CONNECTED; // Transition to app state
}
else if ((now - g_connect_start_time) > CONNECT_TIMEOUT_MS) {
g_wifi_state = WIFI_STATE_FAILED; // Timeout (impossible with hw_status alone)
}
break;
// ... other cases
}
}
Summary¶
- Hardware layer:
wl_status_tfrom WiFi.h (used internally inwifi_update()) - Application layer:
wifi_state_t(exposed in public API) - Mapping: Multiple hardware states → single application state
- Benefits: Simplified API, timeout handling, auto-retry logic, debuggability
This is a standard abstraction pattern in embedded systems (hardware abstraction layer above low-level drivers).
Current Issues¶
STA Mode接続トラブル(調査中)¶
症状: WPA2/WPA3セキュリティが有効なルーターへのSTA接続がタイムアウト
- エラー:
[WiFi] Connection timeout (hw_status=1)(WL_NO_SSID_AVAIL) - 発生条件:
WIFI_ENABLE <SSID> <PASSWORD>コマンド実行時 - 状態: 接続状態遷移は正常、ただし認証段階で失敗
推定原因:
- WPA2/WPA3セキュリティプロトコルの互換性問題
- SSIDまたはパスワードの入力エラー
- ESP32WiFi.hライブラリのセキュリティプロトコル対応範囲
回避策(テスト済み):
- ルーターをWPA2のみに設定(WPA3を無効化)
- シンプルなSSID(英数字のみ)とパスワード(英数字+特殊文字)でテスト
- 2.4GHz帯域を使用(5GHzでなく)
次の調査ステップ:
- WiFi接続詳細ログの追加実装
- セキュリティプロトコル明示指定の検討(WiFi.begin()への引数追加)
- タイムアウト値の調整(現在10秒)
- 複数のテストWiFiネットワークでの検証
Next Steps¶
Optional enhancements for future versions:
- STA mode接続トラブル解決(WPA2/WPA3互換性確認)
- WiFi詳細ログ出力機能(デバッグ用)
- STA mode接続画面(Web UI経由でWiFi設定変更)
- Web設定ポータル(SSID/パスワード入力フォーム)
- WPA3セキュリティ対応
- WiFi信号強度(RSSI)のダッシュボード表示
- OTA(Over-The-Air)ファームウェア更新対応
- mDNS対応(
osechi.localでの接続)