wifi_station example not working
Posted: Fri Jun 21, 2024 12:40 pm
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "esp_system.h"
- #include "esp_log.h"
- #include "esp_netif.h"
- #include "esp_event.h"
- #include "esp_wifi.h"
- #include "nvs.h"
- #include "nvs_flash.h"
- #include "lwip/err.h"
- #include "lwip/sys.h"
- /* The examples use WiFi configuration that you can set via project configuration menu
- If you'd rather not, just change the below entries to strings with
- the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
- */
- #define EXAMPLE_ESP_WIFI_SSID "Sm_Mobile"
- #define EXAMPLE_ESP_WIFI_PASS "P@s_1345"
- #define EXAMPLE_ESP_MAXIMUM_RETRY 5
- /* FreeRTOS event group to signal when we are connected*/
- static EventGroupHandle_t s_wifi_event_group;
- /* The event group allows multiple bits for each event, but we only care about two events:
- * - we are connected to the AP with an IP
- * - we failed to connect after the maximum amount of retries */
- #define WIFI_CONNECTED_BIT BIT0
- #define WIFI_FAIL_BIT BIT1
- static const char *TAG = "ESP8266_Station";
- static int s_retry_num = 0;
- static void event_handler(void* arg, esp_event_base_t event_base,
- int32_t event_id, void* event_data)
- {
- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
- esp_wifi_connect();
- } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
- if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
- esp_wifi_connect();
- s_retry_num++;
- ESP_LOGI(TAG, "retry to connect to the AP");
- } else {
- xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
- }
- ESP_LOGI(TAG,"connect to the AP fail");
- } 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:%s",
- ip4addr_ntoa(&event->ip_info.ip));
- s_retry_num = 0;
- xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
- }
- }
- void wifi_init_sta(void)
- {
- s_wifi_event_group = xEventGroupCreate();
- tcpip_adapter_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));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
- wifi_config_t wifi_config = {
- .sta = {
- .ssid = EXAMPLE_ESP_WIFI_SSID,
- .password = EXAMPLE_ESP_WIFI_PASS
- },
- };
- /* Setting a password implies station will connect to all security modes including WEP/WPA.
- * However these modes are deprecated and not advisable to be used. Incase your Access point
- * doesn't support WPA2, these mode can be enabled by commenting below line */
- if (strlen((char *)wifi_config.sta.password)) {
- wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
- }
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
- ESP_ERROR_CHECK(esp_wifi_start() );
- ESP_LOGI(TAG, "wifi_init_sta finished.");
- /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
- * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
- EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
- WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
- pdFALSE,
- pdFALSE,
- portMAX_DELAY);
- /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
- * happened. */
- if (bits & WIFI_CONNECTED_BIT) {
- ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
- EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
- } else if (bits & WIFI_FAIL_BIT) {
- ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
- EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
- } else {
- ESP_LOGE(TAG, "UNEXPECTED EVENT");
- }
- ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
- ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
- vEventGroupDelete(s_wifi_event_group);
- }
- void app_main()
- {
- ESP_ERROR_CHECK(nvs_flash_init());
- ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
- wifi_init_sta();
- }
I wanted to try wifi connection in ESP8266 uisng ESP8266_RTOS_SDK so i picked a station example code from the git and tried it I only changed the Ssid and Password of the AP which was correct and still it is not able to connect to AP
the Output of the Code was
- Toolchain path: /home/DELL/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc
- Toolchain version: esp-2020r3-49-gd5524c1
- Compiler version: 8.4.0
- Python requirements from D:/PRG_Install/esp32_win32_msys2_environment_and_toolchain-20181001/msys32/home/DELL/esp/ESP8266_RTOS_SDK/requirements.txt are satisfied.
- Project is not inside a git repository, or git repository has no commits
- will not use 'git describe' to determine PROJECT_VER.
- App "wifi_station" version: 1
- Flashing binaries to serial port COM12 (app at offset 0x10000)...
- esptool.py v2.4.0
- Connecting....
- Chip is ESP8266EX
- Features: WiFi
- MAC: bc:ff:4d:f9:2d:9e
- Uploading stub...
- Running stub...
- Stub running...
- Configuring flash size...
- Compressed 11088 bytes to 7440...
- Wrote 11088 bytes (7440 compressed) at 0x00000000 in 0.7 seconds (effective 133.5 kbit/s)...
- Hash of data verified.
- Compressed 436112 bytes to 279705...
- Wrote 436112 bytes (279705 compressed) at 0x00010000 in 24.7 seconds (effective 141.2 kbit/s)...
- Hash of data verified.
- Compressed 3072 bytes to 83...
- Wrote 3072 bytes (83 compressed) at 0x00008000 in 0.0 seconds (effective 1440.6 kbit/s)...
- Hash of data verified.
- Leaving...
- Hard resetting via RTS pin...
- MONITOR
- --- idf_monitor on COM12 74880 ---
- --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
- ets Jan 8 2013,rst cause:2, boot mode:(3,7)
- load 0x40100000, len 7544, room 16
- tail 8
- chksum 0xb9
- load 0x3ffe8408, len 24, room 0
- tail 8
- chksum 0x30
- load 0x3ffe8420, len 3476, room 0
- tail 4
- chksum 0x23
- csum 0x23
- I (46) boot: ESP-IDF v3.4-100-gd3a5f99e-dirty 2nd stage bootloader
- I (46) boot: compile time 15:23:47
- I (47) qio_mode: Enabling default flash chip QIO
- I (55) boot: SPI Speed : 40MHz
- I (61) boot: SPI Mode : QIO
- I (67) boot: SPI Flash Size : 2MB
- I (73) boot: Partition Table:
- I (79) boot: ## Label Usage Type ST Offset Length
- I (90) boot: 0 nvs WiFi data 01 02 00009000 00006000
- I (102) boot: 1 phy_init RF data 01 01 0000f000 00001000
- I (113) boot: 2 factory factory app 00 00 00010000 000f0000
- I (125) boot: End of partition table
- I (132) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x54ee0 (347872) map
- 0x40210010: _stext at ??:?
- I (263) esp_image: segment 1: paddr=0x00064ef8 vaddr=0x40264ef0 size=0x0fbec ( 64492) map
- I (286) esp_image: segment 2: paddr=0x00074aec vaddr=0x3ffe8000 size=0x005fc ( 1532) load
- I (287) esp_image: segment 3: paddr=0x000750f0 vaddr=0x40100000 size=0x00080 ( 128) load
- I (297) esp_image: segment 4: paddr=0x00075178 vaddr=0x40100080 size=0x055f4 ( 22004) load
- I (318) boot: Loaded app from partition at offset 0x10000
- I (341) ESP8266_Station: ESP_WIFI_MODE_STA
- I (344) system_api: Base MAC address is not set, read default base MAC address from EFUSE
- I (348) system_api: Base MAC address is not set, read default base MAC address from EFUSE
- phy_version: 1167.0, 14a6402, Feb 17 2022, 11:32:25, RTOS new
- I (420) phy_init: phy ver: 1167_0
- I (436) ESP8266_Station: wifi_init_sta finished.
- I (1407) wifi:state: 0 -> 2 (b0)
- I (1409) wifi:state: 2 -> 3 (0)
- I (1417) wifi:state: 3 -> 5 (10)
- I (4705) wifi:state: 5 -> 0 (fc0)
- I (4708) ESP8266_Station: retry to connect to the AP
- I (4710) ESP8266_Station: connect to the AP fail
- I (6766) ESP8266_Station: retry to connect to the AP
- I (6768) ESP8266_Station: connect to the AP fail
- I (7736) wifi:state: 0 -> 2 (b0)
- I (7738) wifi:state: 2 -> 3 (0)
- I (7740) wifi:state: 3 -> 5 (10)
- I (11459) wifi:state: 5 -> 0 (fc0)
- I (11462) ESP8266_Station: retry to connect to the AP
- I (11464) ESP8266_Station: connect to the AP fail
- I (13520) ESP8266_Station: retry to connect to the AP
- I (13522) ESP8266_Station: connect to the AP fail
- I (14491) wifi:state: 0 -> 2 (b0)
- I (14493) wifi:state: 2 -> 3 (0)
- I (14495) wifi:state: 3 -> 5 (10)
- I (17805) wifi:state: 5 -> 0 (fc0)
- I (17809) ESP8266_Station: retry to connect to the AP
- I (17811) ESP8266_Station: connect to the AP fail
- I (19865) ESP8266_Station: connect to the AP fail
- I (19867) ESP8266_Station: Failed to connect to SSID:Sm_Mobile, password:P@s_1345
Can anyone suggest me a way to overcome this problem