From 618a6974bf03ce57cedbeaeab04e7286bec4faf4f025ab545417f2ca95d4e00a Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 9 Jan 2026 23:26:46 +0100 Subject: [PATCH 1/2] enable ap lock --- main/Kconfig.projbuild | 14 ++++++++++++++ main/wifi.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index c855f47..5b17d67 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -18,5 +18,19 @@ menu "Smart Oil Heating Control System" config SNTP_SERVER_IP_ADDR string "SNTP IPv4 server address" default "192.168.0.1" + config ENV_WIFI_BSSID_LOCK + bool "Lock to specific Access Point (BSSID)" + default n + help + When enabled, the device will only connect to the access point + with the specified MAC address (BSSID). Useful when multiple APs + share the same SSID. + config ENV_WIFI_BSSID + string "Access Point MAC Address (BSSID)" + default "00:00:00:00:00:00" + depends on ENV_WIFI_BSSID_LOCK + help + MAC address of the access point to connect to. + Format: XX:XX:XX:XX:XX:XX (uppercase or lowercase) endmenu diff --git a/main/wifi.c b/main/wifi.c index d1fd0c1..d97e472 100644 --- a/main/wifi.c +++ b/main/wifi.c @@ -19,6 +19,7 @@ static const char *TAG = "smart-oil-heater-control-system-wifi"; static EventGroupHandle_t s_wifi_event_group; static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); +static bool parse_bssid(const char *bssid_str, uint8_t *bssid); void initWifi(void) { @@ -56,6 +57,21 @@ void initWifi(void) .threshold.authmode = WIFI_AUTH_WPA2_PSK, }, }; + +#if CONFIG_ENV_WIFI_BSSID_LOCK + /* Lock to specific AP by BSSID */ + if (parse_bssid(CONFIG_ENV_WIFI_BSSID, wifi_config.sta.bssid)) + { + wifi_config.sta.bssid_set = true; + ESP_LOGI(TAG, "BSSID lock enabled: %s", CONFIG_ENV_WIFI_BSSID); + } + else + { + ESP_LOGE(TAG, "Invalid BSSID format: %s", CONFIG_ENV_WIFI_BSSID); + wifi_config.sta.bssid_set = false; + } +#endif + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); @@ -105,4 +121,28 @@ static void event_handler(void *arg, esp_event_base_t event_base, ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip)); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } +} + +/** + * @brief Parse BSSID string to byte array + * + * @param bssid_str BSSID string in format "XX:XX:XX:XX:XX:XX" + * @param bssid Output byte array (6 bytes) + * @return true on success, false on parse error + */ +static bool parse_bssid(const char *bssid_str, uint8_t *bssid) +{ + unsigned int tmp[6]; + int parsed = sscanf(bssid_str, "%x:%x:%x:%x:%x:%x", + &tmp[0], &tmp[1], &tmp[2], + &tmp[3], &tmp[4], &tmp[5]); + if (parsed != 6) + { + return false; + } + for (int i = 0; i < 6; i++) + { + bssid[i] = (uint8_t)tmp[i]; + } + return true; } \ No newline at end of file -- 2.50.1 From b77cba87ede27177feed62c69d92f9aa3222663f392ccf56d68cde8e86326f20 Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 9 Jan 2026 23:44:53 +0100 Subject: [PATCH 2/2] fix wifi event handler --- main/wifi.c | 88 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/main/wifi.c b/main/wifi.c index d97e472..6a4b806 100644 --- a/main/wifi.c +++ b/main/wifi.c @@ -13,13 +13,17 @@ #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 +#define MAX_RETRY_COUNT 10 +#define RETRY_DELAY_MS 1000 static const char *TAG = "smart-oil-heater-control-system-wifi"; static EventGroupHandle_t s_wifi_event_group; +static int s_retry_num = 0; +static bool s_initial_connect = true; + static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); -static bool parse_bssid(const char *bssid_str, uint8_t *bssid); void initWifi(void) { @@ -57,21 +61,6 @@ void initWifi(void) .threshold.authmode = WIFI_AUTH_WPA2_PSK, }, }; - -#if CONFIG_ENV_WIFI_BSSID_LOCK - /* Lock to specific AP by BSSID */ - if (parse_bssid(CONFIG_ENV_WIFI_BSSID, wifi_config.sta.bssid)) - { - wifi_config.sta.bssid_set = true; - ESP_LOGI(TAG, "BSSID lock enabled: %s", CONFIG_ENV_WIFI_BSSID); - } - else - { - ESP_LOGE(TAG, "Invalid BSSID format: %s", CONFIG_ENV_WIFI_BSSID); - wifi_config.sta.bssid_set = false; - } -#endif - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); @@ -100,7 +89,9 @@ void initWifi(void) { ESP_LOGE(TAG, "Unexpected event"); } - vEventGroupDelete(s_wifi_event_group); + + // Mark initial connection phase complete - do NOT delete the event group + s_initial_connect = false; } static void event_handler(void *arg, esp_event_base_t event_base, @@ -112,37 +103,46 @@ static void event_handler(void *arg, esp_event_base_t event_base, } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - esp_wifi_connect(); - ESP_LOGI(TAG, "Retry to connect to the AP"); + wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; + ESP_LOGW(TAG, "Disconnected from AP (reason: %d)", event->reason); + + if (s_initial_connect) + { + // During initial connection phase, use retry limit + if (s_retry_num < MAX_RETRY_COUNT) + { + vTaskDelay(pdMS_TO_TICKS(RETRY_DELAY_MS)); + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "Retry to connect to the AP (%d/%d)", s_retry_num, MAX_RETRY_COUNT); + } + else + { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + ESP_LOGE(TAG, "Failed to connect after %d attempts", MAX_RETRY_COUNT); + } + } + else + { + // After initial connection, always try to reconnect with delay + vTaskDelay(pdMS_TO_TICKS(RETRY_DELAY_MS)); + esp_wifi_connect(); + ESP_LOGI(TAG, "Attempting to reconnect to the AP..."); + } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + s_retry_num = 0; + + if (s_initial_connect) + { + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } + else + { + ESP_LOGI(TAG, "Successfully reconnected to AP"); + } } } - -/** - * @brief Parse BSSID string to byte array - * - * @param bssid_str BSSID string in format "XX:XX:XX:XX:XX:XX" - * @param bssid Output byte array (6 bytes) - * @return true on success, false on parse error - */ -static bool parse_bssid(const char *bssid_str, uint8_t *bssid) -{ - unsigned int tmp[6]; - int parsed = sscanf(bssid_str, "%x:%x:%x:%x:%x:%x", - &tmp[0], &tmp[1], &tmp[2], - &tmp[3], &tmp[4], &tmp[5]); - if (parsed != 6) - { - return false; - } - for (int i = 0; i < 6; i++) - { - bssid[i] = (uint8_t)tmp[i]; - } - return true; -} \ No newline at end of file -- 2.50.1