feat: Wi-Fi access point with a setting, password and boot button

The controller runs an AP (SSID = hostname, 192.168.5.1) next to ESP-NOW,
on by default. Network settings gain an on/off switch (ap_enabled) and an
optional WPA2 password (ap_password, 8-63 chars; empty is open). A button
on D7 (GPIO44) held at boot forces the AP on.

Wi-Fi now starts before Ethernet, and a missing W5500 no longer aborts the
boot: the web server and mDNS start anyway so the AP stays usable.

Network tuning found while testing over the AP: HTTP backlog 10 and 12 open
sockets (the default 5 dropped the 6th SYN, a 1 s stall on every page load),
larger TCP and Wi-Fi buffers, and IPv6 link-local addresses so mDNS answers
AAAA queries (browsers waited about a second for them).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-10 01:46:05 +13:00
1 parent 9989dd3062
commit f85021d40d
11 files changed
+197 -22

No files matched your search

+60 -16
View File
@@ -44,6 +44,7 @@ static const char *TAG = "eth";
static httpd_handle_t s_server = NULL;
static esp_netif_t *s_eth_netif = NULL;
static bool s_ap_on = false; // Wi-Fi access point running
static const char *reset_reason(void)
{
@@ -75,19 +76,21 @@ static esp_err_t status_get_handler(httpd_req_t *req)
mic_get_level(&level);
espnow_tx_stats(&es);
rel_stats(&rs);
esp_netif_get_ip_info(s_eth_netif, &ip_info);
esp_netif_get_mac(s_eth_netif, mac);
if (s_eth_netif) { // NULL when the W5500 did not start
esp_netif_get_ip_info(s_eth_netif, &ip_info);
esp_netif_get_mac(s_eth_netif, mac);
}
snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip));
snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac));
snprintf(buf, sizeof(buf),
"{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu,"
"{\"ap\":%s,\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu,"
"\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f,"
"\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu,"
"\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu,"
"\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu,"
"\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu},"
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}",
ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(),
s_ap_on ? "true" : "false", ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(),
(unsigned long)heap_caps_get_free_size(MALLOC_CAP_INTERNAL), level.rms_dbfs, level.peak_dbfs, level.spl_db,
bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count,
(unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false",
@@ -107,6 +110,34 @@ static esp_err_t status_get_handler(httpd_req_t *req)
#define DEFAULT_NETMASK ESP_IP4TOADDR(255, 255, 255, 0)
#define DHCP_WAIT_S 15
// Boolean setting; def when missing or not a boolean
static bool settings_bool(const char *key, bool def)
{
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
return cJSON_IsBool(v) ? cJSON_IsTrue(v) : def;
}
// AP button held (to ground) for AP_HOLD_MS from boot
static bool ap_button_held(void)
{
#if CONFIG_AP_BUTTON_GPIO >= 0
gpio_config_t io = { .pin_bit_mask = 1ULL << CONFIG_AP_BUTTON_GPIO, .mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE };
if (gpio_config(&io) != ESP_OK) {
return false;
}
for (int t = 0; t < CONFIG_AP_HOLD_MS; t += 10) {
if (gpio_get_level(CONFIG_AP_BUTTON_GPIO)) {
return false;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
return true;
#else
return false;
#endif
}
static const char *settings_str(const char *key)
{
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
@@ -257,6 +288,8 @@ static void start_webserver(void)
config.max_uri_handlers = 80;
config.stack_size = 8192;
config.lru_purge_enable = true;
config.max_open_sockets = 12; // browsers open 6 connections at once
config.backlog_conn = 10; // the default 5 drops the 6th SYN, a 1 s retry
if (httpd_start(&s_server, &config) != ESP_OK) {
ESP_LOGE(TAG, "HTTP server start failed");
return;
@@ -287,6 +320,7 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
ESP_LOGI(TAG, "Ethernet Link Up, MAC " MACSTR, MAC2STR(mac));
esp_netif_create_ip6_linklocal(s_eth_netif); // lets mDNS answer AAAA queries (see espnow_tx.c)
xTaskCreate(link_up_task, "link_up", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "%s Mbps %s Duplex",
speed == ETH_SPEED_100M ? "100" : "10",
@@ -407,21 +441,31 @@ void app_main(void)
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_eth_handle_t eth_handle = NULL;
ESP_ERROR_CHECK(w5500_init(&eth_handle));
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
s_eth_netif = esp_netif_new(&netif_cfg);
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netif, esp_eth_new_netif_glue(eth_handle)));
// Wi-Fi (AP + ESP-NOW) comes up first so the AP is there even if Ethernet fails
const char *host = settings_str("hostname");
esp_netif_set_hostname(s_eth_netif, host ? host : DEFAULT_HOSTNAME);
bool held = ap_button_held();
s_ap_on = settings_bool("ap_enabled", true) || held;
ESP_LOGI(TAG, "AP %s%s", s_ap_on ? "on" : "off", held ? " (button held)" : "");
ESP_ERROR_CHECK(espnow_tx_init(s_ap_on ? (host ? host : DEFAULT_HOSTNAME) : NULL, settings_str("ap_password")));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
// Ethernet is optional: without it the web UI is still served over the AP
esp_eth_handle_t eth_handle = NULL;
if (w5500_init(&eth_handle) == ESP_OK) {
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
s_eth_netif = esp_netif_new(&netif_cfg);
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netif, esp_eth_new_netif_glue(eth_handle)));
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
ESP_ERROR_CHECK(espnow_tx_init());
esp_netif_set_hostname(s_eth_netif, host ? host : DEFAULT_HOSTNAME);
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
} else {
ESP_LOGE(TAG, "Ethernet unavailable, continuing without it%s", s_ap_on ? " (web UI on the AP)" : "");
start_webserver();
start_mdns();
}
ESP_ERROR_CHECK(rel_init());
ESP_ERROR_CHECK(engine_init());
ESP_ERROR_CHECK(mic_start());