feat: access point name and password setting, ap_on in status

- ap_ssid and ap_password on /api/network: empty name uses led-XXXXXX, a password of 8-63 characters
  makes the access point WPA2, empty is open; the password is never sent back (ap_password_set)
- a running access point restarts when its name or password changes
- /api/status reports ap_on; the web page gets the fields

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-12 00:46:04 +13:00
1 parent af82cb4c4a
commit 59eb37cad5
7 files changed
+97 -6

No files matched your search

+37 -5
View File
@@ -47,21 +47,31 @@ static void apply_sta_config(const wifi_creds_t *c)
esp_wifi_set_config(WIFI_IF_STA, &cfg);
}
// Turn the access point on (with its led-XXXXXX name, open) or off. The client keeps running either way.
// 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)
{
if (on) {
char ssid[16];
net_cfg_t n;
settings_load_net(&n);
char ssid[33];
net_ap_ssid(ssid, sizeof(ssid));
wifi_config_t ap = {};
strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1);
ap.ap.ssid_len = strlen(ssid);
ap.ap.channel = AP_CHANNEL;
ap.ap.max_connection = AP_MAX_CLIENTS;
ap.ap.authmode = WIFI_AUTH_OPEN;
// A password of 8-63 characters is WPA2; anything else is an open network
const size_t plen = strlen(n.ap_pass);
if (plen >= 8) {
strncpy((char *)ap.ap.password, n.ap_pass, sizeof(ap.ap.password) - 1);
ap.ap.authmode = WIFI_AUTH_WPA2_PSK;
} else {
ap.ap.authmode = WIFI_AUTH_OPEN;
}
esp_wifi_set_config(WIFI_IF_AP, &ap);
esp_wifi_set_mode(WIFI_MODE_APSTA);
ESP_LOGI(TAG, "Access point %s on", ssid);
ESP_LOGI(TAG, "Access point %s on (%s)", ssid, plen >= 8 ? "WPA2" : "open");
} else {
esp_wifi_set_mode(WIFI_MODE_STA);
ESP_LOGI(TAG, "Access point off");
@@ -69,13 +79,19 @@ static void apply_ap(bool on)
s_ap_on = on;
}
// Switch the access point later, from a timer task (not inside a Wi-Fi event)
// Switch the access point later, from a timer task (not inside a Wi-Fi event).
// s_ap_reapply: the access point stays on but its name or password changed, so restart it with them.
static bool s_ap_reapply;
static void ap_timer_cb(void *arg)
{
(void)arg;
if (s_ap_want != s_ap_on) {
apply_ap(s_ap_want);
} else if (s_ap_want && s_ap_reapply) {
apply_ap(true);
}
s_ap_reapply = false;
}
static esp_err_t apply_client_ip(const net_cfg_t *c);
@@ -186,6 +202,12 @@ static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data
void net_ap_ssid(char *buf, size_t len)
{
net_cfg_t n;
settings_load_net(&n);
if (n.ap_ssid[0]) {
snprintf(buf, len, "%s", n.ap_ssid);
return;
}
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
snprintf(buf, len, "led-%02X%02X%02X", mac[3], mac[4], mac[5]);
@@ -255,6 +277,11 @@ esp_err_t net_start(void)
return ESP_OK;
}
bool net_ap_on(void)
{
return s_ap_on;
}
bool net_sta_status(char *ip, size_t ip_len)
{
if (ip_len) {
@@ -296,9 +323,14 @@ esp_err_t net_save_network(const net_cfg_t *c)
if (err != ESP_OK) {
return err;
}
const bool ap_cfg_changed = strcmp(old.ap_ssid, c->ap_ssid) != 0 || strcmp(old.ap_pass, c->ap_pass) != 0;
if (c->ap_enabled != s_ap_on) {
// After the reply has gone out, so the page that asked still gets it
ap_switch_later(c->ap_enabled, c->ap_enabled ? 1000000 : 1500000);
} else if (c->ap_enabled && ap_cfg_changed) {
// Still on, with a new name or password: restart it after the reply has gone out
s_ap_reapply = true;
ap_switch_later(true, 1500000);
}
// Reconnect with the new address settings if a client is configured (after the reply, see reconnect_cb)
wifi_creds_t w;
+3
View File
@@ -16,6 +16,9 @@ esp_err_t net_start(void);
/** @brief Fill buf with the AP SSID, e.g. "led-A1B2C3". */
void net_ap_ssid(char *buf, size_t len);
/** @brief True while the access point is running. */
bool net_ap_on(void);
/** @brief Client link state and its IPv4 address ("" if not connected). */
bool net_sta_status(char *ip, size_t ip_len);
+4
View File
@@ -134,6 +134,8 @@ esp_err_t settings_load_net(net_cfg_t *c)
char ap[8];
get_str(h, "ap", ap, sizeof(ap), "on");
c->ap_enabled = strcmp(ap, "off") != 0;
get_str(h, "ap_ssid", c->ap_ssid, sizeof(c->ap_ssid), "");
get_str(h, "ap_pass", c->ap_pass, sizeof(c->ap_pass), "");
char ddp[8];
get_str(h, "ddp", ddp, sizeof(ddp), "on");
c->ddp_enabled = strcmp(ddp, "off") != 0;
@@ -155,6 +157,8 @@ esp_err_t settings_save_net(const net_cfg_t *c)
if (err == ESP_OK) err = nvs_set_str(h, "gateway", c->gateway);
if (err == ESP_OK) err = nvs_set_str(h, "dns", c->dns);
if (err == ESP_OK) err = nvs_set_str(h, "ap", c->ap_enabled ? "on" : "off");
if (err == ESP_OK) err = nvs_set_str(h, "ap_ssid", c->ap_ssid);
if (err == ESP_OK) err = nvs_set_str(h, "ap_pass", c->ap_pass);
if (err == ESP_OK) err = nvs_set_str(h, "ddp", c->ddp_enabled ? "on" : "off");
if (err == ESP_OK) err = nvs_commit(h);
nvs_close(h);
+2
View File
@@ -20,6 +20,8 @@ struct net_cfg_t {
char gateway[16];
char dns[16]; // optional; empty uses the gateway
bool ap_enabled; // the access point (led-XXXXXX) is on; default on
char ap_ssid[33]; // access point name; empty uses led-XXXXXX from the MAC
char ap_pass[65]; // access point password, 8-63 characters (WPA2); empty is an open network
bool ddp_enabled; // DDP pixels and frame sync are received; default on
};
+24
View File
@@ -244,6 +244,7 @@ static esp_err_t status_get(api_req_t *req)
settings_load_net(&n);
cJSON *o = preset_to_json(&p);
cJSON_AddStringToObject(o, "ap_ssid", ap);
cJSON_AddBoolToObject(o, "ap_on", net_ap_on());
cJSON_AddStringToObject(o, "hostname", n.hostname);
cJSON_AddBoolToObject(o, "sta_connected", up);
cJSON_AddStringToObject(o, "sta_ip", ip);
@@ -389,6 +390,11 @@ static esp_err_t network_get(api_req_t *req)
cJSON_AddBoolToObject(o, "connected", up);
cJSON_AddStringToObject(o, "current_ip", ip);
cJSON_AddBoolToObject(o, "ap_enabled", n.ap_enabled);
char ap_name[33];
net_ap_ssid(ap_name, sizeof(ap_name));
cJSON_AddStringToObject(o, "ap_ssid", ap_name); // the name in use now
cJSON_AddBoolToObject(o, "ap_ssid_custom", n.ap_ssid[0] != '\0');
cJSON_AddBoolToObject(o, "ap_password_set", strlen(n.ap_pass) >= 8); // the password itself is never sent
cJSON_AddBoolToObject(o, "ddp_enabled", n.ddp_enabled);
return send_json(req, o);
}
@@ -448,6 +454,24 @@ static esp_err_t network_post(api_req_t *req)
if (cJSON_IsBool(ap)) {
n.ap_enabled = cJSON_IsTrue(ap);
}
// Access point name: empty goes back to led-XXXXXX. Password: empty is open, else 8-63 characters.
cJSON *apn = cJSON_GetObjectItem(in, "ap_ssid");
if (cJSON_IsString(apn)) {
if (strlen(apn->valuestring) > 32) {
cJSON_Delete(in);
return send_error(req, "400 Bad Request", "ap_ssid: up to 32 characters, or empty for the default name");
}
snprintf(n.ap_ssid, sizeof(n.ap_ssid), "%s", apn->valuestring);
}
cJSON *app = cJSON_GetObjectItem(in, "ap_password");
if (cJSON_IsString(app)) {
const size_t plen = strlen(app->valuestring);
if (plen != 0 && (plen < 8 || plen > 63)) {
cJSON_Delete(in);
return send_error(req, "400 Bad Request", "ap_password: empty (open) or 8-63 characters");
}
snprintf(n.ap_pass, sizeof(n.ap_pass), "%s", app->valuestring);
}
cJSON *ddp = cJSON_GetObjectItem(in, "ddp_enabled");
if (cJSON_IsBool(ddp)) {
n.ddp_enabled = cJSON_IsTrue(ddp);