led-driver: channel default 11, bare ESP-NOW build, settings and USB config updates

Default ESP-NOW channel is now 11. Adds the bare ESP-NOW-only build option
(CONFIG_LED_ESPNOW_ONLY) with its sdkconfig fragments, a default hostname from
the Wi-Fi MAC, and the related net, link, web and settings changes.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-21 01:18:55 +13:00
1 parent 12a8e225ad
commit c9838d1cbe
18 files changed
+268 -31

No files matched your search

+73 -5
View File
@@ -13,6 +13,8 @@
#include "esp_wifi.h"
#include "mdns.h"
#include "net.h"
#include "sdkconfig.h"
#include "web.h"
static const char *TAG = "net";
@@ -31,6 +33,8 @@ static int s_retries;
// then stays down for about a minute (AP_FALLBACK_RETRIES reconnect attempts), the access point comes back.
#define AP_FALLBACK_RETRIES 6
static volatile bool s_ap_on;
static bool s_net_up; // net_start has finished
static bool s_mdns_on; // mDNS was started (not in ESP-NOW-only mode)
static bool s_ap_want; // the state ap_timer switches to
static esp_timer_handle_t s_ap_timer;
@@ -48,6 +52,22 @@ static void apply_sta_config(const wifi_creds_t *c)
esp_wifi_set_config(WIFI_IF_STA, &cfg);
}
// ESP-NOW uses the radio's channel, which the access point (on the ESP-NOW channel) or a joined network sets.
// With ESP-NOW on the client stays off, so with the access point off too nothing sets it: set it here.
// Not while a client is joined (it would break the association). Before esp_wifi_start this fails
// harmlessly; net_start calls it again after the start.
static void pin_espnow_channel(void)
{
net_cfg_t n;
settings_load_net(&n);
if (n.espnow_enabled && !s_sta_up) {
esp_err_t err = esp_wifi_set_channel(n.espnow_channel, WIFI_SECOND_CHAN_NONE);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Radio on ESP-NOW channel %u", n.espnow_channel);
}
}
}
// Turn the access point on (its saved name and password, or led-XXXXXX and open) or off.
// The client keeps running either way.
static void apply_ap(bool on)
@@ -73,8 +93,14 @@ static void apply_ap(bool on)
esp_wifi_set_config(WIFI_IF_AP, &ap);
esp_wifi_set_mode(WIFI_MODE_APSTA);
ESP_LOGI(TAG, "Access point %s on (%s)", ssid, plen >= 8 ? "WPA2" : "open");
#if !CONFIG_LED_ESPNOW_ONLY
if (s_net_up) {
web_start(); // turned on later (USB, or the client fallback): its page needs the web interface
}
#endif
} else {
esp_wifi_set_mode(WIFI_MODE_STA);
pin_espnow_channel();
ESP_LOGI(TAG, "Access point off");
}
s_ap_on = on;
@@ -232,6 +258,27 @@ void net_ap_ssid(char *buf, size_t len)
snprintf(buf, len, "led-%02X%02X%02X", mac[3], mac[4], mac[5]);
}
#if CONFIG_LED_ESPNOW_ONLY
// The bare ESP-NOW driver: the radio only, a station that never joins a network, held on the saved ESP-NOW
// channel. No network interfaces, access point, client, mDNS or timers.
esp_err_t net_start(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&init_cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // nothing of the client's is used or saved
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); // the radio stays awake for every frame
net_cfg_t n;
settings_load_net(&n);
ESP_ERROR_CHECK(esp_wifi_set_channel(n.espnow_channel, WIFI_SECOND_CHAN_NONE));
s_net_up = true;
ESP_LOGI(TAG, "Bare ESP-NOW: radio on channel %u, no Wi-Fi client or access point", n.espnow_channel);
return ESP_OK;
}
#else
esp_err_t net_start(void)
{
ESP_ERROR_CHECK(esp_netif_init());
@@ -272,21 +319,29 @@ esp_err_t net_start(void)
ESP_ERROR_CHECK(esp_timer_create(&reconnect_args, &s_reconnect_timer));
// The access point is on unless turned off. With no client Wi-Fi saved it stays on, so the device
// can always be reached and set up.
// can always be reached and set up, except with ESP-NOW on: then the controller and USB reach it.
net_cfg_t n;
settings_load_net(&n);
wifi_creds_t c;
const bool has_creds = settings_load_wifi(&c) == ESP_OK && c.ssid[0];
apply_ap(n.ap_enabled || !has_creds);
apply_ap(n.ap_enabled || (!has_creds && !n.espnow_enabled));
if (has_creds) {
apply_sta_config(&c);
}
ESP_ERROR_CHECK(esp_wifi_start());
if (!s_ap_on) {
pin_espnow_channel(); // no access point to set the channel (see pin_espnow_channel)
}
// No modem sleep: the radio stays awake so the AP never holds DDP unicast until a beacon
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
s_net_up = true;
if (net_espnow_only()) {
ESP_LOGI(TAG, "ESP-NOW only: no web interface, mDNS or DDP (USB and ESP-NOW still work)");
return ESP_OK;
}
// mDNS: <hostname>.local, with an HTTP service on port 80
ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(mdns_hostname_set(n.hostname));
@@ -294,15 +349,27 @@ esp_err_t net_start(void)
// app=led-strip lets other drivers' discovery (GET /api/devices) pick out LED drivers from other HTTP devices
static mdns_txt_item_t txt[] = { { "app", "led-strip" } };
ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, txt, 1));
s_mdns_on = true;
ESP_LOGI(TAG, "mDNS: %s.local", n.hostname);
return ESP_OK;
}
#endif
bool net_ap_on(void)
{
return s_ap_on;
}
bool net_espnow_only(void)
{
#if CONFIG_LED_ESPNOW_ONLY
return true;
#endif
net_cfg_t n;
settings_load_net(&n);
return n.espnow_enabled && !s_ap_on;
}
bool net_sta_status(char *ip, size_t ip_len)
{
if (ip_len) {
@@ -360,17 +427,18 @@ esp_err_t net_sta_connect(const wifi_creds_t *c)
esp_err_t net_save_network(const net_cfg_t *c)
{
// Turning the access point off needs a connected client, or the device could not be reached again
// Turning the access point off needs a connected client or ESP-NOW (with USB, the controller reaches it),
// or the device could not be reached again
net_cfg_t old;
settings_load_net(&old);
if (!c->ap_enabled && old.ap_enabled && !s_sta_up) {
if (!c->ap_enabled && old.ap_enabled && !s_sta_up && !c->espnow_enabled) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t err = settings_save_net(c);
if (err != ESP_OK) {
return err;
}
err = mdns_hostname_set(c->hostname);
err = s_mdns_on ? mdns_hostname_set(c->hostname) : ESP_OK; // ESP-NOW only: no mDNS, the name is just saved
if (err != ESP_OK) {
return err;
}