Page 1 of 1

wifi_station example not working

Posted: Fri Jun 21, 2024 12:40 pm
by vincenzo05
  1. #include <string.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/event_groups.h"
  5. #include "esp_system.h"
  6. #include "esp_log.h"
  7. #include "esp_netif.h"
  8. #include "esp_event.h"
  9. #include "esp_wifi.h"
  10. #include "nvs.h"
  11. #include "nvs_flash.h"
  12.  
  13. #include "lwip/err.h"
  14. #include "lwip/sys.h"
  15.  
  16. /* The examples use WiFi configuration that you can set via project configuration menu
  17.  
  18.    If you'd rather not, just change the below entries to strings with
  19.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  20. */
  21. #define EXAMPLE_ESP_WIFI_SSID      "Sm_Mobile"
  22. #define EXAMPLE_ESP_WIFI_PASS      "P@s_1345"
  23. #define EXAMPLE_ESP_MAXIMUM_RETRY   5
  24.  
  25. /* FreeRTOS event group to signal when we are connected*/
  26. static EventGroupHandle_t s_wifi_event_group;
  27.  
  28. /* The event group allows multiple bits for each event, but we only care about two events:
  29.  * - we are connected to the AP with an IP
  30.  * - we failed to connect after the maximum amount of retries */
  31. #define WIFI_CONNECTED_BIT BIT0
  32. #define WIFI_FAIL_BIT      BIT1
  33.  
  34. static const char *TAG = "ESP8266_Station";
  35.  
  36. static int s_retry_num = 0;
  37.  
  38.  
  39.  
  40. static void event_handler(void* arg, esp_event_base_t event_base,
  41.                                 int32_t event_id, void* event_data)
  42. {
  43.     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  44.         esp_wifi_connect();
  45.     } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  46.         if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  47.             esp_wifi_connect();
  48.             s_retry_num++;
  49.             ESP_LOGI(TAG, "retry to connect to the AP");
  50.         } else {
  51.             xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  52.         }
  53.         ESP_LOGI(TAG,"connect to the AP fail");
  54.     } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  55.         ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  56.         ESP_LOGI(TAG, "got ip:%s",
  57.                  ip4addr_ntoa(&event->ip_info.ip));
  58.         s_retry_num = 0;
  59.         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  60.     }
  61. }
  62.  
  63. void wifi_init_sta(void)
  64. {
  65.     s_wifi_event_group = xEventGroupCreate();
  66.  
  67.     tcpip_adapter_init();
  68.  
  69.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  70.    
  71.  
  72.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  73.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  74.  
  75.  
  76.     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  77.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  78.  
  79.     wifi_config_t wifi_config = {
  80.         .sta = {
  81.             .ssid = EXAMPLE_ESP_WIFI_SSID,
  82.             .password = EXAMPLE_ESP_WIFI_PASS
  83.         },
  84.     };
  85.  
  86.     /* Setting a password implies station will connect to all security modes including WEP/WPA.
  87.         * However these modes are deprecated and not advisable to be used. Incase your Access point
  88.         * doesn't support WPA2, these mode can be enabled by commenting below line */
  89.  
  90.     if (strlen((char *)wifi_config.sta.password)) {
  91.         wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  92.     }
  93.  
  94.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  95.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  96.     ESP_ERROR_CHECK(esp_wifi_start() );
  97.  
  98.     ESP_LOGI(TAG, "wifi_init_sta finished.");
  99.  
  100.  
  101.  
  102.     /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  103.      * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
  104.     EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  105.             WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  106.             pdFALSE,
  107.             pdFALSE,
  108.             portMAX_DELAY);
  109.  
  110.     /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  111.      * happened. */
  112.     if (bits & WIFI_CONNECTED_BIT) {
  113.         ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  114.                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  115.     } else if (bits & WIFI_FAIL_BIT) {
  116.         ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  117.                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  118.     } else {
  119.         ESP_LOGE(TAG, "UNEXPECTED EVENT");
  120.     }
  121.  
  122.     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
  123.     ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
  124.     vEventGroupDelete(s_wifi_event_group);
  125. }
  126.  
  127. void app_main()
  128. {
  129.     ESP_ERROR_CHECK(nvs_flash_init());
  130.  
  131.     ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  132.     wifi_init_sta();
  133. }
  134.  

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

  1. Toolchain path: /home/DELL/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc
  2. Toolchain version: esp-2020r3-49-gd5524c1
  3. Compiler version: 8.4.0
  4. Python requirements from D:/PRG_Install/esp32_win32_msys2_environment_and_toolchain-20181001/msys32/home/DELL/esp/ESP8266_RTOS_SDK/requirements.txt are satisfied.
  5. Project is not inside a git repository, or git repository has no commits
  6. will not use 'git describe' to determine PROJECT_VER.
  7. App "wifi_station" version: 1
  8. Flashing binaries to serial port COM12 (app at offset 0x10000)...
  9. esptool.py v2.4.0
  10. Connecting....
  11. Chip is ESP8266EX
  12. Features: WiFi
  13. MAC: bc:ff:4d:f9:2d:9e
  14. Uploading stub...
  15. Running stub...
  16. Stub running...
  17. Configuring flash size...
  18. Compressed 11088 bytes to 7440...
  19. Wrote 11088 bytes (7440 compressed) at 0x00000000 in 0.7 seconds (effective 133.5 kbit/s)...
  20. Hash of data verified.
  21. Compressed 436112 bytes to 279705...
  22. Wrote 436112 bytes (279705 compressed) at 0x00010000 in 24.7 seconds (effective 141.2 kbit/s)...
  23. Hash of data verified.
  24. Compressed 3072 bytes to 83...
  25. Wrote 3072 bytes (83 compressed) at 0x00008000 in 0.0 seconds (effective 1440.6 kbit/s)...
  26. Hash of data verified.
  27.  
  28. Leaving...
  29. Hard resetting via RTS pin...
  30. MONITOR
  31. --- idf_monitor on COM12 74880 ---
  32. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  33.  
  34.  ets Jan  8 2013,rst cause:2, boot mode:(3,7)
  35.  
  36. load 0x40100000, len 7544, room 16
  37. tail 8
  38. chksum 0xb9
  39. load 0x3ffe8408, len 24, room 0
  40. tail 8
  41. chksum 0x30
  42. load 0x3ffe8420, len 3476, room 0
  43. tail 4
  44. chksum 0x23
  45. csum 0x23
  46. I (46) boot: ESP-IDF v3.4-100-gd3a5f99e-dirty 2nd stage bootloader
  47. I (46) boot: compile time 15:23:47
  48. I (47) qio_mode: Enabling default flash chip QIO
  49. I (55) boot: SPI Speed      : 40MHz
  50. I (61) boot: SPI Mode       : QIO
  51. I (67) boot: SPI Flash Size : 2MB
  52. I (73) boot: Partition Table:
  53. I (79) boot: ## Label            Usage          Type ST Offset   Length
  54. I (90) boot:  0 nvs              WiFi data        01 02 00009000 00006000
  55. I (102) boot:  1 phy_init         RF data          01 01 0000f000 00001000
  56. I (113) boot:  2 factory          factory app      00 00 00010000 000f0000
  57. I (125) boot: End of partition table
  58. I (132) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x54ee0 (347872) map
  59. 0x40210010: _stext at ??:?
  60.  
  61. I (263) esp_image: segment 1: paddr=0x00064ef8 vaddr=0x40264ef0 size=0x0fbec ( 64492) map
  62. I (286) esp_image: segment 2: paddr=0x00074aec vaddr=0x3ffe8000 size=0x005fc (  1532) load
  63. I (287) esp_image: segment 3: paddr=0x000750f0 vaddr=0x40100000 size=0x00080 (   128) load
  64. I (297) esp_image: segment 4: paddr=0x00075178 vaddr=0x40100080 size=0x055f4 ( 22004) load
  65. I (318) boot: Loaded app from partition at offset 0x10000
  66. I (341) ESP8266_Station: ESP_WIFI_MODE_STA
  67. I (344) system_api: Base MAC address is not set, read default base MAC address from EFUSE
  68. I (348) system_api: Base MAC address is not set, read default base MAC address from EFUSE
  69. phy_version: 1167.0, 14a6402, Feb 17 2022, 11:32:25, RTOS new
  70. I (420) phy_init: phy ver: 1167_0
  71. I (436) ESP8266_Station: wifi_init_sta finished.
  72. I (1407) wifi:state: 0 -> 2 (b0)
  73. I (1409) wifi:state: 2 -> 3 (0)
  74. I (1417) wifi:state: 3 -> 5 (10)
  75. I (4705) wifi:state: 5 -> 0 (fc0)
  76. I (4708) ESP8266_Station: retry to connect to the AP
  77. I (4710) ESP8266_Station: connect to the AP fail
  78. I (6766) ESP8266_Station: retry to connect to the AP
  79. I (6768) ESP8266_Station: connect to the AP fail
  80. I (7736) wifi:state: 0 -> 2 (b0)
  81. I (7738) wifi:state: 2 -> 3 (0)
  82. I (7740) wifi:state: 3 -> 5 (10)
  83. I (11459) wifi:state: 5 -> 0 (fc0)
  84. I (11462) ESP8266_Station: retry to connect to the AP
  85. I (11464) ESP8266_Station: connect to the AP fail
  86. I (13520) ESP8266_Station: retry to connect to the AP
  87. I (13522) ESP8266_Station: connect to the AP fail
  88. I (14491) wifi:state: 0 -> 2 (b0)
  89. I (14493) wifi:state: 2 -> 3 (0)
  90. I (14495) wifi:state: 3 -> 5 (10)
  91. I (17805) wifi:state: 5 -> 0 (fc0)
  92. I (17809) ESP8266_Station: retry to connect to the AP
  93. I (17811) ESP8266_Station: connect to the AP fail
  94. I (19865) ESP8266_Station: connect to the AP fail
  95. I (19867) ESP8266_Station: Failed to connect to SSID:Sm_Mobile, password:P@s_1345
  96.  

Can anyone suggest me a way to overcome this problem