# HG changeset patch # User Michiel Broek # Date 1696346646 -7200 # Node ID 15dc572a7fcb7e30aa112ed8024de81599e98609 # Parent 0432d914768207b49aa569b65e277c245840c09f Version 0.3.0. Backported network code from experimental roaming project. Will now connect after reset to the strongest AP. Id the signal level drops below -67, extra scans are done to see for a better AP. Nothing is done yet. Removed config.conf file, all info is taken from the project menu and live tests. Better log the board type and send it via json mqtt. Send bssid and current channel too. diff -r 0432d9147682 -r 15dc572a7fcb CMakeLists.txt --- a/CMakeLists.txt Thu Sep 28 11:29:23 2023 +0200 +++ b/CMakeLists.txt Tue Oct 03 17:24:06 2023 +0200 @@ -2,7 +2,7 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -set(PROJECT_VER "0.2.7") +set(PROJECT_VER "0.3.0") set(PROJECT_NAME "co2meter") set(EXTRA_COMPONENT_DIRS components/esp32-ds18b20 components/esp32-owb components/esp32-rotary-encoder components/u8g2) diff -r 0432d9147682 -r 15dc572a7fcb main/Kconfig.projbuild --- a/main/Kconfig.projbuild Thu Sep 28 11:29:23 2023 +0200 +++ b/main/Kconfig.projbuild Tue Oct 03 17:24:06 2023 +0200 @@ -1,5 +1,74 @@ menu "CO2 meter configuration" + menu "WiFi settings" + + config ESP_WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for to connect to. + + config ESP_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi WPA2/WPA3 password to use. + + choice ESP_WIFI_SCAN_METHOD + prompt "scan method" + default WIFI_FAST_SCAN + help + scan method for the esp32 to use + config WIFI_FAST_SCAN + bool "fast" + config WIFI_ALL_CHANNEL_SCAN + bool "all" + endchoice + + config ESP_FAST_SCAN_MINIMUM_SIGNAL + int "WiFi fast scan minimum RSSI" + range -127 0 + default -96 + help + RSSI minimum reliable level to connect to an AP + + config ESP_WIFI_ROAMING_LEVEL + int "WiFi RSSI level for alternative AP" + range -96 -50 + default -67 + help + RSSI level to start looking for a better AP + + endmenu + + menu "MQTT server" + + config MQTT_SERVER + string "MQTT server" + default "mqtt.eclipseprojects.io" + help + The MQTT server to connect to + + config MQTT_PORT + int "MQTT port number" + default 1883 + help + The MQTT server port + + config MQTT_USER + string "MQTT optional username" + default "" + help + The optional MQTT username. + + config MQTT_PASS + string "MQTT password" + default "letmein" + help + The optional MQTT password for the username. + + endmenu + config ONE_WIRE_BUS int "OneWire Bus GPIO number" range 0 34 diff -r 0432d9147682 -r 15dc572a7fcb main/co2meter.c --- a/main/co2meter.c Thu Sep 28 11:29:23 2023 +0200 +++ b/main/co2meter.c Tue Oct 03 17:24:06 2023 +0200 @@ -26,10 +26,15 @@ extern SemaphoreHandle_t xSemaphoreWiFi; extern WIFI_State *wifi_state; ///< WiFi state extern EventGroupHandle_t xEventGroupUser; +extern bool _wifi_ScanDone; +extern int8_t _wifi_RSSI; extern int count_pub; ///< Published MQTT messages in transit extern uint32_t AlarmTimer; ///< Alarm timer extern uint32_t err_connect; ///< Connect error counter +char hostname[32]; +char uuid[37]; + /** * @brief Main program entry @@ -39,7 +44,7 @@ esp_err_t ret; Main_Loop1 = ML1_INIT; - app_desc = esp_ota_get_app_description(); + app_desc = esp_app_get_description(); /* * event handler and event group for the user interface */ @@ -50,7 +55,7 @@ * Initialize NVS */ ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } @@ -113,10 +118,17 @@ /* * Read or create configuration */ - read_config(); +// unlink("/spiffs/config.conf"); +// read_config(); read_units(); vTaskDelay(500 / portTICK_PERIOD_MS); + uint8_t mac_addr[8] = {0}; + // Set the configured hostname for the dhcp client. + esp_efuse_mac_get_default(mac_addr); + sprintf(hostname, "co2meter-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); + sprintf(uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + /* * Create FreeRTOS tasks */ @@ -127,13 +139,14 @@ xTaskCreate(&task_user, "task_user", 4096, NULL,10, &xTaskUser); xTaskCreate(&task_ds18b20, "task_ds18b20", 2560, NULL, 8, &xTaskDS18B20); xTaskCreate(&task_adc, "task_adc", 2560, NULL, 8, &xTaskADC); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); +// esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); esp_log_level_set("wifi", ESP_LOG_ERROR); xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); - vTaskDelay(10 / portTICK_PERIOD_MS); + int wait = 150; while (wait) { + vTaskDelay(100 / portTICK_PERIOD_MS); if (ready_WiFi()) { ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0); wait = 0; @@ -142,7 +155,6 @@ if (wait < 1) ESP_LOGE(TAG, "Timeout network connection"); } - vTaskDelay(100 / portTICK_PERIOD_MS); } /* @@ -156,16 +168,16 @@ while (1) { switch (Main_Loop1) { case ML1_INIT: - status_WiFi(); Main_Loop1 = ML1_CONNECT; request_ds18b20(); request_adc(); - AlarmTimer = 30; + AlarmTimer = 30 * 100; if (! ready_WiFi()) { /* If WiFi was lost, try a new connection */ ESP_LOGI(TAG, "Try WiFi restore"); request_WiFi(); err_connect++; } + ESP_LOGI(TAG, "Loop => ML1_CONNECT"); break; case ML1_CONNECT: @@ -175,6 +187,7 @@ if (! ready_mqtt()) connect_mqtt(true); } + ESP_LOGI(TAG, "Loop => ML1_MQTT_CONNECT"); break; case ML1_MQTT_CONNECT: @@ -250,12 +263,15 @@ ESP_LOGE(TAG, "ML1_MQTT_CONNECT write_units lock error"); } user_refresh(); + ESP_LOGI(TAG, "Loop => ML1_MQTT_WAITCON"); } break; case ML1_WAITCON: - if (ready_mqtt()) + if (ready_mqtt()) { Main_Loop1 = ML1_SEND; + ESP_LOGI(TAG, "Loop => ML1_SEND"); + } break; case ML1_SEND: @@ -263,22 +279,51 @@ publishUnits(); publishLogs(); Main_Loop1 = ML1_WAITACK; + ESP_LOGI(TAG, "Loop => ML1_WAITACK"); break; case ML1_WAITACK: if (count_pub == 0) { // Wait until all published messages are sent. + status_WiFi(); ESP_LOGD(TAG, "Main loop: Done, user busy: %s", user_busy() ? "true":"false"); - Main_Loop1 = ML1_DONE; + Main_Loop1 = ML1_STATUS; user_refresh(); + ESP_LOGI(TAG, "Loop => ML1_STATUS"); } break; + case ML1_STATUS: + /* + * If rssi is too low, scan for beter AP. + */ + if (_wifi_RSSI < CONFIG_ESP_WIFI_ROAMING_LEVEL) { + scan_WiFi(); + Main_Loop1 = ML1_SCAN; + ESP_LOGI(TAG, "Loop => ML1_SCAN"); + } else { + Main_Loop1 = ML1_DONE; + ESP_LOGI(TAG, "Loop => ML1_DONE"); + } + break; + + case ML1_SCAN: + if (_wifi_ScanDone == true) { + Main_Loop1 = ML1_DONE; + ESP_LOGI(TAG, "Loop => ML1_DONE"); + } + break; + case ML1_DONE: /* Wait here until the timer resets the loop */ - AlarmTimer = 0; + if (AlarmTimer == 0) { + Main_Loop1 = ML1_INIT; + ESP_LOGI(TAG, "Loop => ML1_INIT"); + } break; } vTaskDelay(10 / portTICK_PERIOD_MS); + if (AlarmTimer > 0) + AlarmTimer--; } Main_Loop1 = ML1_INIT; diff -r 0432d9147682 -r 15dc572a7fcb main/config.c --- a/main/config.c Thu Sep 28 11:29:23 2023 +0200 +++ b/main/config.c Tue Oct 03 17:24:06 2023 +0200 @@ -11,53 +11,6 @@ unit_t units[3]; ///< Pressure test units SemaphoreHandle_t xSemaphoreUnits = NULL; ///< Semaphore Units records wifiStation_t wifiStation; -strConfig_t config; - - -void write_config() { - uint8_t *dst = (uint8_t *)&config; - FILE *f = fopen("/spiffs/config.conf", "w+"); - - if (f == NULL) { - ESP_LOGE(TAG, "write /spiffs/config.conf failed"); - return; - } - - size_t bytes = fwrite(dst, 1, sizeof(config), f); - fclose(f); - if (bytes != sizeof(config)) { - ESP_LOGE(TAG, "/spiffs/config.conf written %d/%d bytes", bytes, sizeof(config)); - } else { - ESP_LOGD(TAG, "/spiffs/config.conf written %d bytes", bytes); - } -} - - - -void read_config() { - uint8_t *dst; - uint8_t mac_addr[8] = {0}; - FILE *f = fopen("/spiffs/config.conf", "r"); - - if (f == NULL) { - // No configuration yet, create it. - esp_efuse_mac_get_default(mac_addr); - config.Version = 1; - sprintf(config.hostname, "co2meter-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); - config.mqtt_server[0] = '\0'; - config.mqtt_port = 1883; - config.mqtt_user[0] = '\0'; - config.mqtt_pwd[0] = '\0'; - sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", - mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); - write_config(); - } else { - dst = (uint8_t*)&config; - size_t bytes = fread(dst, 1, sizeof(config), f); - fclose(f); - ESP_LOGD(TAG, "/spiffs/config.conf read %d bytes", bytes); - } -} diff -r 0432d9147682 -r 15dc572a7fcb main/config.h --- a/main/config.h Thu Sep 28 11:29:23 2023 +0200 +++ b/main/config.h Tue Oct 03 17:24:06 2023 +0200 @@ -77,6 +77,8 @@ ML1_WAITCON, ///< Wait for connection and measurements ML1_SEND, ///< Send MQTT node and units messages ML1_WAITACK, ///< MQTT messages received + ML1_STATUS, ///< Update status + ML1_SCAN, ///< Scan AP's for better one ML1_DONE ///< All done } ML1; @@ -106,11 +108,6 @@ ML2_SEL_SENSOR1, ///< Unit 1 select sensor ML2_SEL_SENSOR2, ///< Unit 2 select sensor ML2_SEL_SENSOR3, ///< Unit 3 select sensor - ML2_SETUP_NETWORK, ///< Network setup - ML2_SETUP_MQTT, ///< MQTT setup - ML2_SETUP_WIFI, ///< WiFi setup - ML2_LIST_APS, ///< WiFi list APs - ML2_EDIT_AP, ///< WiFi edit AP ML2_INACTIVE, ///< Inactive reached, cleanup ML2_DONE ///< All done } ML2; @@ -118,35 +115,6 @@ /** - * @brief Global configuration. File /spiffs/config.conf - */ -typedef struct { - uint8_t Version; ///< Record version number for updates. - char ntp_server[32]; ///< Preffered NTP server. - char hostname[32]; ///< Our hostname. - char uuid[37]; ///< Sort of uuid code. - char xlastSSID[32]; ///< Not used anymore. - char mqtt_server[32]; ///< MQTT server. - uint16_t mqtt_port; ///< MQTT TCP port. - char mqtt_user[32]; ///< MQTT user name if needed. - char mqtt_pwd[62]; ///< MQTT password. -} strConfig_t; ///< Config record. - - -/** - * @brief Write configuration to disk. - */ -void write_config(void); - -/** - * @brief Read configuration file. If it doesn't exist create and - * write a new configuration file with sane defaults. - */ -void read_config(void); - - - -/** * @brief Records with WiFi stations we have succesfully connected. */ typedef struct { diff -r 0432d9147682 -r 15dc572a7fcb main/task_mqtt.c --- a/main/task_mqtt.c Thu Sep 28 11:29:23 2023 +0200 +++ b/main/task_mqtt.c Tue Oct 03 17:24:06 2023 +0200 @@ -17,6 +17,7 @@ const int TASK_MQTT_CONNECT = BIT0; ///< Request MQTT connection const int TASK_MQTT_DISCONNECT = BIT1; ///< Request MQTT disconnect const int TASK_MQTT_CONNECTED = BIT2; ///< MQTT is connected +const int TASK_MQTT_STARTED = BIT3; ///< MQTT is started const char *sensState[] = { "OK", "ERROR" }; ///< Sensor state strings const char *unitMode[] = { "OFF", "ON" }; ///< Units state strings @@ -30,8 +31,8 @@ extern unit_t units[3]; extern SemaphoreHandle_t xSemaphoreUnits; extern const esp_app_desc_t *app_desc; -extern strConfig_t config; - +extern char hostname[]; +extern char uuid[]; void connect_mqtt(bool state) { @@ -78,7 +79,7 @@ tmp = xstrcpy((char *)"mbv1.0/co2meters/"); tmp = xstrcat(tmp, msgtype); tmp = xstrcat(tmp, (char *)"/"); - tmp = xstrcat(tmp, config.hostname); + tmp = xstrcat(tmp, hostname); return tmp; } @@ -200,12 +201,30 @@ esp_chip_info(&chip_info); payload = payload_header(); payload = xstrcat(payload, (char *)"{\"uuid\":\""); - payload = xstrcat(payload, config.uuid); + payload = xstrcat(payload, uuid); payload = xstrcat(payload, (char *)"\",\"interval\":"); sprintf(buf, "%d", MAINLOOP_TIMER); payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)",\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\""); - sprintf(buf, "ESP32 %d cores rev %d, WiFi bgn", chip_info.cores, chip_info.revision); + if (chip_info.model == CHIP_ESP32) + payload = xstrcat(payload, (char *)"ESP32"); + else if (chip_info.model == CHIP_ESP32S2) + payload = xstrcat(payload, (char *)"ESP32-S2"); + else if (chip_info.model == CHIP_ESP32S3) + payload = xstrcat(payload, (char *)"ESP32-S3"); + else if (chip_info.model == CHIP_ESP32C3) + payload = xstrcat(payload, (char *)"ESP32-C3"); + else if (chip_info.model == CHIP_ESP32C2) + payload = xstrcat(payload, (char *)"ESP32-C2"); + else if (chip_info.model == CHIP_ESP32C6) + payload = xstrcat(payload, (char *)"ESP32-C6"); + else if (chip_info.model == CHIP_ESP32H2) + payload = xstrcat(payload, (char *)"ESP32-H2"); + else if (chip_info.model == CHIP_POSIX_LINUX) + payload = xstrcat(payload, (char *)"Posix Linux"); + else + payload = xstrcat(payload, (char *)"Unknown"); + sprintf(buf, " %d cores rev %d, WiFi bgn", chip_info.cores, chip_info.revision); payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)"\",\"os\":\"esp-idf\",\"os_version\":\""); payload = xstrcat(payload, (char *)esp_get_idf_version()); @@ -231,6 +250,11 @@ payload = xstrcat(payload, (char *)"\",\"rssi\":"); sprintf(buf, "%d", wifi_state->STA_rssi); payload = xstrcat(payload, buf); + payload = xstrcat(payload, (char *)",\"bssid\":\""); + payload = xstrcat(payload, wifi_state->STA_bssid); + payload = xstrcat(payload, (char *)"\",\"channel\":"); + sprintf(buf, "%d", wifi_state->STA_channel); + payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)"}"); xSemaphoreGive(xSemaphoreWiFi); } else { @@ -289,17 +313,18 @@ switch (event->event_id) { case MQTT_EVENT_CONNECTED: - ESP_LOGD(TAG, "MQTT_EVENT_CONNECTED"); + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); + xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_STARTED); break; case MQTT_EVENT_DISCONNECTED: - ESP_LOGD(TAG, "MQTT_EVENT_DISCONNECTED"); + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGD(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); break; case MQTT_EVENT_UNSUBSCRIBED: @@ -307,7 +332,7 @@ break; case MQTT_EVENT_PUBLISHED: - ESP_LOGD(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); if (xSemaphoreTake(xSemaphorePcounter, 10) == pdTRUE) { if (count_pub) { count_pub--; @@ -378,40 +403,55 @@ uxBits = xEventGroupWaitBits(xEventGroupMQTT, TASK_MQTT_CONNECT | TASK_MQTT_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY ); if (uxBits & TASK_MQTT_CONNECT) { - if (strlen(config.mqtt_server)) { - uri = xstrcpy((char *)"mqtt://"); - if (strlen(config.mqtt_user) && strlen(config.mqtt_pwd)) { - uri = xstrcat(uri, config.mqtt_user); - uri = xstrcat(uri, (char *)":"); - uri = xstrcat(uri, config.mqtt_pwd); - uri = xstrcat(uri, (char *)"@"); + if (! (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED)) { + if (strlen(CONFIG_MQTT_SERVER)) { + uri = xstrcpy((char *)"mqtt://"); + if (strlen(CONFIG_MQTT_USER) && strlen(CONFIG_MQTT_PASS)) { + uri = xstrcat(uri, CONFIG_MQTT_USER); + uri = xstrcat(uri, (char *)":"); + uri = xstrcat(uri, CONFIG_MQTT_PASS); + uri = xstrcat(uri, (char *)"@"); + } + uri = xstrcat(uri, CONFIG_MQTT_SERVER); + if (CONFIG_MQTT_PORT != 1883) { + uri = xstrcat(uri, (char *)":"); + sprintf(port, "%d", CONFIG_MQTT_PORT); + uri = xstrcat(uri, port); + } + } else { + uri = xstrcpy((char *)"mqtt://iot.eclipse.org:1883"); } - uri = xstrcat(uri, config.mqtt_server); - if (config.mqtt_port != 1883) { - uri = xstrcat(uri, (char *)":"); - sprintf(port, "%d", config.mqtt_port); - uri = xstrcat(uri, port); + + ESP_LOGI(TAG, "Request MQTT connect %s", uri); + err = esp_mqtt_client_set_uri(client, uri); + if (err != ESP_OK) + ESP_LOGE(TAG, "Set uri %s", esp_err_to_name(err)); + + if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_STARTED) { + /* Existing session */ + err = esp_mqtt_client_reconnect(client); + if (err != ESP_OK) + ESP_LOGE(TAG, "Reconnect result %s", esp_err_to_name(err)); + } else { + /* New session */ + err = esp_mqtt_client_start(client); + if (err != ESP_OK) + ESP_LOGE(TAG, "Start result %s", esp_err_to_name(err)); } + if (uri) + free(uri); + uri = NULL; } else { - uri = xstrcpy((char *)"mqtt://iot.eclipse.org:1883"); + ESP_LOGI(TAG, "Request MQTT connect but already connected."); } - ESP_LOGI(TAG, "Request MQTT connect %s", uri); - err = esp_mqtt_client_set_uri(client, uri); - if (err != ESP_OK) - ESP_LOGE(TAG, "Set uri %s", esp_err_to_name(err)); - err = esp_mqtt_client_start(client); - if (err != ESP_OK) - ESP_LOGE(TAG, "Result %s", esp_err_to_name(err)); - if (uri) - free(uri); - uri = NULL; xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECT); } else if (uxBits & TASK_MQTT_DISCONNECT) { ESP_LOGI(TAG, "Request MQTT disconnect"); esp_mqtt_client_stop(client); xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); - xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); +// xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); + ESP_LOGI(TAG, "Request MQTT disconnect done"); } } } diff -r 0432d9147682 -r 15dc572a7fcb main/task_user.c --- a/main/task_user.c Thu Sep 28 11:29:23 2023 +0200 +++ b/main/task_user.c Tue Oct 03 17:24:06 2023 +0200 @@ -45,8 +45,9 @@ extern int Main_Loop1; ///< Main measure loop extern int update_running; ///< If update is running extern int num_sensors; ///< Detected DS18B20 sensors -extern strConfig_t config; extern wifiStation_t wifiStation; +extern char hostname[]; + const int TASK_USER_COLD = BIT0; ///< System cold start const int TASK_USER_WAKEUP = BIT1; ///< System wakeup from deepsleep @@ -533,14 +534,14 @@ * @brief The WiFi setup menu. * @param sub The submenu entry to hilite. */ -void screen_wifi_setup(int sub) +/*void screen_wifi_setup(int sub) { screen_top("WiFi AP setup"); menu_line(sub == 0, 1, 25, "Lijst AP"); menu_line(sub == 1, 1, 37, "Nieuw AP"); menu_line(sub == 2, 1, 49, "Terug"); u8g2_SendBuffer(&u8g2); -} +}*/ @@ -549,7 +550,7 @@ * @param sub The sub entry line. * @param offset The offset in the list. */ -void screen_list_aps(int sub, int offset) +/*void screen_list_aps(int sub, int offset) { int i; wifiStation_t ap; @@ -586,7 +587,7 @@ menu_line(sub == i, 1, 25 + (i * 12), "Terug"); } u8g2_SendBuffer(&u8g2); -} +}*/ @@ -594,7 +595,7 @@ * @brief Edit WiFi AP menu. * @param sub The menu entry to hilite. */ -void screen_edit_ap(int sub) +/*void screen_edit_ap(int sub) { screen_top("WiFi wijzig AP"); menu_line(sub == 0, 1, 25, "SSID %s", editAP.SSID); @@ -604,7 +605,7 @@ menu_line(sub == 3, 1, 61, "Verwijder"); } u8g2_SendBuffer(&u8g2); -} +}*/ @@ -628,22 +629,7 @@ menu_line(0, 1, 25, "IP %s", ip); menu_line(0, 1, 37, "Mask %s", nm); menu_line(0, 1, 49, "GW %s", gw); - menu_line(0, 1, 61, "Naam %s", config.hostname); - u8g2_SendBuffer(&u8g2); -} - - - -/** - * @brief The network setup menu. - * @param sub The menu entry to hilite. - */ -void screen_network_setup(int sub) -{ - screen_top("Netwerk setup"); - menu_line(sub == 0, 1, 25, "Naam %s", config.hostname); - menu_line(sub == 1, 1, 37, "NTP %s", config.ntp_server); - menu_line(sub == 2, 1, 49, "Terug"); + menu_line(0, 1, 61, "Naam %s", hostname); u8g2_SendBuffer(&u8g2); } @@ -655,25 +641,9 @@ void screen_mqtt() { screen_top("MQTT Status"); - menu_line(0, 1, 25, "serv %s", config.mqtt_server); - menu_line(0, 1, 37, "port %d", config.mqtt_port); - menu_line(0, 1, 49, "user %s", config.mqtt_user); - u8g2_SendBuffer(&u8g2); -} - - - -/** - * @brief MQTT setup menu. - * @param sub The submenu entry to hilite. - */ -void screen_mqtt_setup(int sub) -{ - screen_top("MQTT Setup"); - menu_line(sub == 0, 1, 25, "serv %s", config.mqtt_server); - menu_line(sub == 1, 1, 37, "port %d", config.mqtt_port); - menu_line(sub == 2, 1, 49, "user %s", config.mqtt_user); - menu_line(sub == 3, 1, 61, "Terug"); + menu_line(0, 1, 25, "serv %s", CONFIG_MQTT_SERVER); + menu_line(0, 1, 37, "port %d", CONFIG_MQTT_PORT); + menu_line(0, 1, 49, "user %s", CONFIG_MQTT_USER); u8g2_SendBuffer(&u8g2); } @@ -889,35 +859,14 @@ screen_wifi(); break; - case ML2_SETUP_WIFI: - screen_wifi_setup(SubMenu); - break; - - case ML2_LIST_APS: - SubMenu = SubOffset = 0; - screen_list_aps(SubMenu, SubOffset); - break; - - case ML2_EDIT_AP: - screen_edit_ap(0); - break; - case ML2_NETWORK: screen_network(); break; - case ML2_SETUP_NETWORK: - screen_network_setup(SubMenu); - break; - case ML2_MQTT: screen_mqtt(); break; - case ML2_SETUP_MQTT: - screen_mqtt_setup(SubMenu); - break; - case ML2_UPDATE: screen_update(); break; @@ -991,26 +940,6 @@ case ML2_SEL_SENSOR3: if (rotate_to_sub(event.state.position, num_sensors, &SubMenu, &SubOffset)) screen_list_sensors(Main_Loop2 - ML2_SEL_SENSOR1, SubMenu, SubOffset); break; - case ML2_SETUP_WIFI: if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset)) - screen_wifi_setup(SubMenu); - break; - case ML2_LIST_APS: if (rotate_to_sub(event.state.position, num_ssids, &SubMenu, &SubOffset)) - screen_list_aps(SubMenu, SubOffset); - break; - case ML2_EDIT_AP: if (edit_ssid < 0) { - if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset)) - screen_edit_ap(SubMenu); - } else { - if (rotate_to_sub(event.state.position, 3, &SubMenu, &SubOffset)) - screen_edit_ap(SubMenu); - } - break; - case ML2_SETUP_MQTT: if (rotate_to_sub(event.state.position, 3, &SubMenu, &SubOffset)) - screen_mqtt_setup(SubMenu); - break; - case ML2_SETUP_NETWORK: if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset)) - screen_network_setup(SubMenu); - break; default: ESP_LOGI(TAG, "Event: position %ld, direction %s", event.state.position, event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW":"CCW"):"NOT_SET"); @@ -1036,7 +965,6 @@ void menu_loop(void) { int idx = 0; - char txt[32]; switch (Main_Loop2) { case ML2_UNIT1: @@ -1121,105 +1049,12 @@ break; case ML2_WIFI: - New_Loop2 = ML2_SETUP_WIFI; - break; - - case ML2_SETUP_WIFI: - if (SubMenu == 0) { - New_Loop2 = ML2_LIST_APS; - } else if (SubMenu == 1) { - edit_ssid = -1; - editAP.SSID[0] = '\0'; - editAP.Password[0] = '\0'; - SubMenu = SubOffset = 0; - New_Loop2 = ML2_EDIT_AP; - } else if (SubMenu == 2) { - New_Loop2 = ML2_WIFI; - } - break; - - case ML2_LIST_APS: - if ((SubMenu + SubOffset) < num_ssids) { - edit_ssid = SubMenu + SubOffset; - memcpy(editAP.SSID, APs[edit_ssid].SSID, 32); - memcpy(editAP.Password, APs[edit_ssid].Password, 64); - SubMenu = SubOffset = 0; - New_Loop2 = ML2_EDIT_AP; - } else { - New_Loop2 = ML2_SETUP_WIFI; - SubMenu = SubOffset = 0; - } - break; - - case ML2_EDIT_AP: - ESP_LOGI(TAG, " ML2_EDIT_AP: SubMenu %d edit_ssid %d", SubMenu, edit_ssid); - if (SubMenu == 0) { - rotary_editer("SSID", editAP.SSID, "", 16, EDIT_TYPE_TEXT); - screen_edit_ap(SubMenu); - } else if (SubMenu == 1) { - rotary_editer("PSK", editAP.Password, "", 16, EDIT_TYPE_TEXT); - screen_edit_ap(SubMenu); - } else if (SubMenu == 2 || SubMenu == 3) { - update_running = 1; // Block measurements - if (edit_ssid < 0) - New_Loop2 = ML2_SETUP_WIFI; - else - New_Loop2 = ML2_LIST_APS; - - if (edit_ssid >= 0) { - ESP_LOGI(TAG, "Remove %s", APs[edit_ssid].SSID); - remove_station((uint8_t *)APs[edit_ssid].SSID); - } - if (SubMenu == 2) { - ESP_LOGI(TAG, "Add %s %s", editAP.SSID, editAP.Password); - add_station((uint8_t *)editAP.SSID, (uint8_t *)editAP.Password); - } - } - update_running = 0; break; case ML2_NETWORK: - New_Loop2 = ML2_SETUP_NETWORK; break; - case ML2_SETUP_NETWORK: - if (SubMenu == 0) { - rotary_editer("Hostnaam", config.hostname, "", 16, EDIT_TYPE_TEXT); - screen_network_setup(SubMenu); - } else if (SubMenu == 1) { - rotary_editer("NTP server", config.ntp_server, "", 16, EDIT_TYPE_TEXT); - screen_network_setup(SubMenu); - } else if (SubMenu == 2) { - ESP_LOGI(TAG, "Hostname `%s`", config.hostname); - ESP_LOGI(TAG, "NTP server `%s`", config.ntp_server); - write_config(); - New_Loop2 = ML2_NETWORK; - } - break; - case ML2_MQTT: - New_Loop2 = ML2_SETUP_MQTT; - break; - - case ML2_SETUP_MQTT: - if (SubMenu == 0) { - rotary_editer("MQTT server", config.mqtt_server, "", 16, EDIT_TYPE_TEXT); - screen_mqtt_setup(SubMenu); - } else if (SubMenu == 1) { - sprintf(txt, "%d", config.mqtt_port); - rotary_editer("MQTT server poort", txt, "", 6, EDIT_TYPE_INT); - config.mqtt_port = atoi(txt); - screen_mqtt_setup(SubMenu); - } else if (SubMenu == 2) { - rotary_editer("MQTT user", config.mqtt_user, "", 16, EDIT_TYPE_TEXT); - rotary_editer("MQTT password", config.mqtt_pwd, "", 16, EDIT_TYPE_TEXT); - screen_mqtt_setup(SubMenu); - } else if (SubMenu == 3) { - ESP_LOGI(TAG, "mqtt_server %s:%d", config.mqtt_server, config.mqtt_port); - ESP_LOGI(TAG, "mqtt_user/pass `%s/%s`", config.mqtt_user, config.mqtt_pwd); - write_config(); - New_Loop2 = ML2_MQTT; - } break; case ML2_UPDATE: diff -r 0432d9147682 -r 15dc572a7fcb main/task_wifi.c --- a/main/task_wifi.c Thu Sep 28 11:29:23 2023 +0200 +++ b/main/task_wifi.c Tue Oct 03 17:24:06 2023 +0200 @@ -6,6 +6,7 @@ #include "config.h" +#define MAX_AP_NUM 10 static const char *TAG = "task_wifi"; @@ -18,32 +19,32 @@ WIFI_State *wifi_state = NULL; ///< Public state for other tasks. esp_netif_t *sta_netif = NULL; ///< Station interface -extern wifiStation_t wifiStation; -extern strConfig_t config; +wifi_scan_config_t scan_config = { ///< WiFi scanner configuration. + .ssid = (uint8_t *)CONFIG_ESP_WIFI_SSID, + .bssid = 0, + .channel = 0, + .show_hidden = false +}; -wifi_scan_config_t scan_config = { - .ssid = 0, - .bssid = 0, - .channel = 0, - .show_hidden = false, - .scan_time.passive = 130 ///< Beacons are usually sent every 100 mSec. -}; ///< WiFi scanner configuration. +bool _wifi_ScanDone = false; ///< Scan ready +bool _wifi_BetterAP = false; ///< If better AP available. +int8_t _wifi_RSSI = -127; ///< Latest RSSI level. +uint16_t _wifi_Scanned = 0; ///< Total scanned APs. -uint8_t _wifi_ssid[33]; ///< Current SSID -bool _wifi_ScanAPs = false; ///< Scanning -bool _wifi_ScanDone = false; ///< Scan ready -uint16_t _wifi_Scanned = 0; ///< Total scanned APs. +extern char hostname[]; ///< Generated hostname + const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT0; ///< When set, means a client requested to disconnect from currently connected AP. const int TASK_WIFI_REQUEST_STA_CONNECT = BIT1; ///< When set, means a client requested to connect to an access point. -const int TASK_WIFI_REQUEST_STA_STATUS = BIT2; ///< When set, means a client requested to update the connection status. +const int TASK_WIFI_REQUEST_STA_SCAN = BIT2; ///< When set, means a client requested a AP scan. +const int TASK_WIFI_REQUEST_STA_STATUS = BIT3; ///< When set, means a client requested to update the connection status. -const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address -const int TASK_WIFI_STA_FAILED = BIT4; ///< Indicate that we could not get a connection to AP as station. -const int TASK_WIFI_STA_DISCONNECTED = BIT5; ///< Indicate that we are disconnected from an ap station. -const int TASK_WIFI_STA_CONNECTED = BIT6; ///< Indicate that we are connected to AP as station, flip of BIT5. +const int TASK_WIFI_HAS_IP = BIT4; ///< Indicate that we have an IP address +const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. +const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. +const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT5. @@ -78,24 +79,53 @@ } +void scan_WiFi(void) +{ + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_SCAN); +} + + +void disconnect_WiFi(void) +{ + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); + xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); +} + + static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { switch (event_id) { case WIFI_EVENT_SCAN_DONE: - // Get the results so the memory used is freed. - ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); - _wifi_Scanned = ap_num; - _wifi_ScanDone = true; - break; + { + /* Get the results so the memory used is freed. */ + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); + ESP_LOGI(TAG, "Event wifi Scane done, %d records", ap_num); + for (int i = 0; i < ap_num; i++) { + wifi_ap_record_t ap = accessp_records[i]; + ESP_LOGI(TAG, "AP:%d bssid:%02x:%02x:%02x:%02x:%02x:%02x ssid:%s ch:%d rssi:%d", + i, ap.bssid[0], ap.bssid[1], ap.bssid[2], ap.bssid[3], ap.bssid[4], ap.bssid[5], + ap.ssid, ap.primary, ap.rssi); + if (ap.rssi > CONFIG_ESP_WIFI_ROAMING_LEVEL && ap.rssi > (_wifi_RSSI + 3)) { + _wifi_BetterAP = true; + ESP_LOGI(TAG, "AP:%d is a better AP", i); + } + } + _wifi_Scanned = ap_num; + _wifi_ScanDone = true; + break; + } case WIFI_EVENT_STA_START: + { ESP_LOGI(TAG, "Event wifi START"); // Set the configured hostname for the dhcp client. - ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname)); + ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, hostname)); esp_wifi_connect(); + _wifi_BetterAP = false; break; + } case WIFI_EVENT_STA_CONNECTED: { @@ -107,11 +137,15 @@ if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { wifi_state->STA_connected = true; wifi_state->STA_rssi = ap_info.rssi; + wifi_state->STA_channel = ap_info.primary; sprintf(wifi_state->STA_ssid, "%s", ap_info.ssid); + snprintf(wifi_state->STA_bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x", + ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]); xSemaphoreGive(xSemaphoreWiFi); } else { ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); } + _wifi_BetterAP = false; xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); break; @@ -131,7 +165,7 @@ } else { ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); } - connect_mqtt(false); + _wifi_BetterAP = false; xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); break; @@ -176,7 +210,6 @@ } else { ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); } - connect_mqtt(false); break; case IP_EVENT_AP_STAIPASSIGNED: @@ -200,6 +233,10 @@ /* initialize the tcp stack */ ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + sta_netif = esp_netif_create_default_wifi_sta(); assert(sta_netif); @@ -207,9 +244,6 @@ * memory allocation of objects used by the task */ accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); - task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); - memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); - xSemaphoreWiFi = xSemaphoreCreateMutex(); wifi_state = malloc(sizeof(WIFI_State)); memset(wifi_state, 0x00, sizeof(WIFI_State)); @@ -217,18 +251,32 @@ /* * init wifi as station */ - wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); - esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_ESP_WIFI_SSID, + .password = CONFIG_ESP_WIFI_PASSWORD, +#if CONFIG_WIFI_ALL_CHANNEL_SCAN + .scan_method = WIFI_ALL_CHANNEL_SCAN, +#elif CONFIG_WIFI_FAST_SCAN + .scan_method = WIFI_FAST_SCAN, +#endif + .failure_retry_cnt = 3, + .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, + .threshold.rssi = CONFIG_ESP_FAST_SCAN_MINIMUM_SIGNAL, + .threshold.authmode = WIFI_AUTH_WPA2_PSK, + }, + }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); +// ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); @@ -238,88 +286,72 @@ /* actions that can trigger: request a connection, a scan, or a disconnection */ uxBits = xEventGroupWaitBits(xEventGroupWifi, - TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_STATUS, - pdFALSE, pdFALSE, portMAX_DELAY ); + TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT | TASK_WIFI_REQUEST_STA_SCAN | TASK_WIFI_REQUEST_STA_STATUS, + pdFALSE, pdFALSE, portMAX_DELAY ); if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { /* * user requested a disconnect, this will in effect disconnect the wifi */ - connect_mqtt(false); - ESP_LOGD(TAG, "Request STA disconnect"); + ESP_LOGI(TAG, "Request STA disconnect"); ESP_ERROR_CHECK(esp_wifi_disconnect()); xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); /* finally: release the scan request bit */ xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); + ESP_LOGI(TAG, "Request STA disconnect is done"); } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { - ESP_LOGD(TAG, "Request STA connect"); + ESP_LOGI(TAG, "Request STA connect"); xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); - _wifi_ScanAPs = true; - _wifi_ScanDone = false; - ap_num = MAX_AP_NUM; - ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); - while (_wifi_ScanDone == false) { - vTaskDelay(10 / portTICK_PERIOD_MS); - } - ESP_LOGI(TAG, "Scan done %d APs", _wifi_Scanned); - bool found = false; + xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); - // Available Access Points. - for (int i = 0; i < _wifi_Scanned; i++) { - wifi_ap_record_t ap = accessp_records[i]; - // Check if we know this AP in the database. - ESP_LOGD(TAG, "%d %-20s ch: %2d rssi: %d %s", i, ap.ssid, ap.primary, ap.rssi, apsec[ap.authmode]); - if ((read_station(ap.ssid) >= 0)) { - /* ssid */ - size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); - memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); - /* password */ - sz = sizeof(task_wifi_ConfigSTA->sta.password); - memcpy(task_wifi_ConfigSTA->sta.password, wifiStation.Password, sz); - found = true; - break; - } + ESP_LOGI(TAG, "Connecting to `%s'", CONFIG_ESP_WIFI_SSID); + esp_err_t wifierror = esp_wifi_connect(); + if (wifierror != ESP_OK) { + ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", CONFIG_ESP_WIFI_SSID, (int)wifierror); + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); } - if (found) { - /* - * Now connect to the known SSID - */ - ESP_LOGD(TAG, "Connecting to `%s'", task_wifi_ConfigSTA->sta.ssid); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); - esp_err_t wifierror = esp_wifi_connect(); - if (wifierror != ESP_OK) { - ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", task_wifi_ConfigSTA->sta.ssid, (int)wifierror); - xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); - } - uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS); - if (uxBits & TASK_WIFI_STA_CONNECTED) - xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Only clear when connected. - } else { - ESP_LOGI(TAG, "No known AP found, scan again"); - vTaskDelay(3000 / portTICK_PERIOD_MS); - } + uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS); +// if (uxBits & TASK_WIFI_STA_CONNECTED) +// xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Only clear when connected. + } else if (uxBits & TASK_WIFI_REQUEST_STA_STATUS) { /* * Request WiFi update status, refresh the rssi. */ + ESP_LOGD(TAG, "Request STA status"); xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_STATUS); wifi_ap_record_t ap_info; esp_wifi_sta_get_ap_info(&ap_info); - ESP_LOGD(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi); + ESP_LOGI(TAG, "Event STA status, ssid:%s, bssid:" MACSTR ", rssi: %d", ap_info.ssid, MAC2STR(ap_info.bssid), ap_info.rssi); + _wifi_RSSI = ap_info.rssi; + _wifi_BetterAP = false; if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { wifi_state->STA_rssi = ap_info.rssi; + wifi_state->STA_channel = ap_info.primary; + snprintf(wifi_state->STA_bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x", + ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]); xSemaphoreGive(xSemaphoreWiFi); } else { ESP_LOGE(TAG, "lock error TASK_WIFI_REQUEST_STA_STATUS"); } user_refresh(); + + } else if (uxBits & TASK_WIFI_REQUEST_STA_SCAN) { + + ESP_LOGI(TAG, "Request STA scan"); + xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_SCAN); + /* safe guard against overflow */ + ap_num = MAX_AP_NUM; + _wifi_ScanDone = false; + _wifi_BetterAP = false; + ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); } } /* for(;;) */ - vTaskDelay(10 / portTICK_PERIOD_MS); +// vTaskDelay(10 / portTICK_PERIOD_MS); } diff -r 0432d9147682 -r 15dc572a7fcb main/task_wifi.h --- a/main/task_wifi.h Thu Sep 28 11:29:23 2023 +0200 +++ b/main/task_wifi.h Tue Oct 03 17:24:06 2023 +0200 @@ -8,19 +8,15 @@ /** - * @brief Defines the maximum number of access points that can be scanned. - */ -#define MAX_AP_NUM 5 - - -/** * @brief Structure containing the information of the driver task. */ typedef struct { bool STA_connected; ///< Connected to AP as station. bool STA_online; ///< Connected and online. char STA_ssid[33]; ///< Connected to this SSID. + char STA_bssid[18]; ///< Connected to this BSSID. int8_t STA_rssi; ///< Signal strength. + uint8_t STA_channel; ///< Connected channel. char STA_ip[17]; ///< IP address. char STA_nm[17]; ///< IP netmask. char STA_gw[17]; ///< IP gateway. @@ -48,6 +44,18 @@ /** + * @brief Request a WiFi scan. + */ +void scan_WiFi(void); + + +/** + * @brief Request WiFi disconnect. + */ +void disconnect_WiFi(void); + + +/** * @brief Main task for the wifi_manager * @param pvParameters Parameters for the task. */ diff -r 0432d9147682 -r 15dc572a7fcb sdkconfig --- a/sdkconfig Thu Sep 28 11:29:23 2023 +0200 +++ b/sdkconfig Tue Oct 03 17:24:06 2023 +0200 @@ -337,6 +337,27 @@ # # CO2 meter configuration # + +# +# WiFi settings +# +CONFIG_ESP_WIFI_SSID="MBSE_WLP" +CONFIG_ESP_WIFI_PASSWORD="abcjkltuv" +CONFIG_WIFI_FAST_SCAN=y +# CONFIG_WIFI_ALL_CHANNEL_SCAN is not set +CONFIG_ESP_FAST_SCAN_MINIMUM_SIGNAL=-96 +CONFIG_ESP_WIFI_ROAMING_LEVEL=-67 +# end of WiFi settings + +# +# MQTT server +# +CONFIG_MQTT_SERVER="lx02.mbse.ym" +CONFIG_MQTT_PORT=1883 +CONFIG_MQTT_USER="" +CONFIG_MQTT_PASS="letmein" +# end of MQTT server + CONFIG_ONE_WIRE_BUS=25 #