WPA2: Arduino framework works but ESP-IDF does not

kkbbesp
Posts: 3
Joined: Tue Apr 30, 2019 7:19 pm

WPA2: Arduino framework works but ESP-IDF does not

Postby kkbbesp » Wed May 01, 2019 8:56 pm

Hello All,

I am trying to connect to my wlan (WPA2 Enterprise, no certificates required, PEAP).

The following arduino core works flawlessly. Seconds after bootup, I get a DHCP address.

  1.  
  2. #include <Arduino.h>
  3. #include "esp_wpa2.h"
  4. #include <WiFi.h>
  5.  
  6. const char *ssid = "MYSSID";
  7. #define EAP_ID "MYSSID"
  8. #define EAP_USERNAME "test123"
  9. #define EAP_PASSWORD "test123"
  10.  
  11. void setup()
  12. {
  13.   // put your setup code here, to run once:
  14.   Serial.begin(115200);
  15.   delay(10);
  16.  
  17.   WiFi.disconnect(true);
  18.   WiFi.mode(WIFI_STA);
  19.   esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));
  20.   esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));
  21.   esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
  22.   esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
  23.   esp_wifi_sta_wpa2_ent_enable(&config);
  24.  
  25.   WiFi.begin(ssid);
  26.  
  27.   while (WiFi.status() != WL_CONNECTED)
  28.   {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.  
  33.   Serial.println("");
  34.   Serial.println("WiFi connected");
  35.   Serial.println("IP address: ");
  36.  
  37.   Serial.println(WiFi.macAddress());
  38.   Serial.println(WiFi.localIP());
  39. }
  40.  
  41. void loop()
  42. {
  43.   // put your main code here, to run repeatedly:
  44. }
  45.  

However, a near identical example using the espressif-idf does not work. (does not work = stuck in getting dhcp address, as shown in log below code listing)
  1.  
  2. // includes omitted for brevity
  3. #define WIFI_SSID "MYWLAN"
  4. #define EAP_METHOD 0
  5.  
  6. const char *ssid = "MYSSID";
  7. #define EAP_ID "MYSSID"
  8. #define USERNAME "test123"
  9. #define PASSWORD "test123"
  10.  
  11.  
  12.  
  13. static EventGroupHandle_t wifi_event_group;
  14.  
  15. const int CONNECTED_BIT = BIT0;
  16.  
  17. #define EAP_PEAP 1
  18. #define EAP_TTLS 2
  19.  
  20. static const char *TAG = "WLAN";
  21.  
  22. static esp_err_t event_handler(void *ctx, system_event_t *event)
  23. {
  24.     switch (event->event_id)
  25.     {
  26.     case SYSTEM_EVENT_STA_START:
  27.         esp_wifi_connect();
  28.         break;
  29.     case SYSTEM_EVENT_STA_GOT_IP:
  30.         xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  31.         break;
  32.     case SYSTEM_EVENT_STA_DISCONNECTED:
  33.         esp_wifi_connect();
  34.         xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  35.         break;
  36.     default:
  37.         break;
  38.     }
  39.     return ESP_OK;
  40. }
  41.  
  42. static void initialise_wifi(void)
  43. {
  44.  
  45.     tcpip_adapter_init();
  46.  
  47.     wifi_event_group = xEventGroupCreate();
  48.     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  49.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  50.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  51.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  52.  
  53.     wifi_config_t wifi_config = {
  54.         .sta = {
  55.             .ssid = WIFI_SSID
  56.         },
  57.     };
  58.  
  59.     ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  60.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  61.  
  62.     ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID,   strlen(EAP_ID)));
  63.     ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_username((uint8_t *)USERNAME, strlen(USERNAME)));
  64.     ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_password((uint8_t *)PASSWORD, strlen(PASSWORD)));
  65.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  66.     esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
  67.     ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_enable(&config));
  68.  
  69.     ESP_ERROR_CHECK(esp_wifi_start());
  70.  
  71.  
  72. }
  73.  
  74. static void wpa2_enterprise_example_task(void *pvParameters)
  75. {
  76.     tcpip_adapter_ip_info_t ip;
  77.     memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
  78.     vTaskDelay(2000 / portTICK_PERIOD_MS);
  79.  
  80.     while (1)
  81.     {
  82.         vTaskDelay(3000 / portTICK_PERIOD_MS);
  83.  
  84.         if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0)
  85.         {
  86.             ESP_LOGI(TAG, "~~~~~~~~~~~");
  87.             ESP_LOGI(TAG, "IP:"    IPSTR, IP2STR(&ip.ip));
  88.             ESP_LOGI(TAG, "MASK:"  IPSTR, IP2STR(&ip.netmask));
  89.             ESP_LOGI(TAG, "GW:"    IPSTR, IP2STR(&ip.gw));
  90.             ESP_LOGI(TAG, "~~~~~~~~~~~");
  91.         }
  92.     }
  93. }
  94.  
  95. void app_main()
  96. {
  97.     ESP_ERROR_CHECK(nvs_flash_init());
  98.     initialise_wifi();
  99.     xTaskCreate(&wpa2_enterprise_example_task, "wpa2_enterprise_example_task", 4096, NULL, 5, NULL);
  100. }
  101.  
  1. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  2. configsip: 188777542, SPIWP:0xee
  3. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  4. mode:DIO, clock div:2
  5. load:0x3fff0018,len:4
  6. load:0x3fff001c,len:6120
  7. load:0x40078000,len:8816
  8. ho 0 tail 12 room 4
  9. load:0x40080400,len:6392
  10. entry 0x40080744
  11. I (31) boot: ESP-IDF 3.30200.190418 2nd stage bootloader
  12. I (31) boot: compile time 10:43:14
  13. I (31) boot: Enabling RNG early entropy source...
  14. I (36) boot: SPI Speed      : 40MHz
  15. I (40) boot: SPI Mode       : DIO
  16. I (44) boot: SPI Flash Size : 4MB
  17. I (48) boot: Partition Table:
  18. I (52) boot: ## Label            Usage          Type ST Offset   Length
  19. I (59) boot:  0 nvs              WiFi data        01 02 00009000 00006000
  20. I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
  21. I (74) boot:  2 factory          factory app      00 00 00010000 00100000
  22. I (82) boot: End of partition table
  23. I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x17928 ( 96552) map
  24. I (128) esp_image: segment 1: paddr=0x00027950 vaddr=0x3ffbdb60 size=0x02e94 ( 11924) load
  25. I (133) esp_image: segment 2: paddr=0x0002a7ec vaddr=0x40080000 size=0x00400 (  1024) load
  26. I (135) esp_image: segment 3: paddr=0x0002abf4 vaddr=0x40080400 size=0x0541c ( 21532) load
  27. I (152) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x6babc (441020) map
  28. I (305) esp_image: segment 5: paddr=0x0009badc vaddr=0x4008581c size=0x0ae8c ( 44684) load
  29. I (333) boot: Loaded app from partition at offset 0x10000
  30. I (333) boot: Disabling RNG early entropy source...
  31. I (333) cpu_start: Pro cpu up.
  32. I (337) cpu_start: Starting app cpu, entry point is 0x400821ac
  33. I (0) cpu_start: App cpu up.
  34. I (347) heap_init: Initializing. RAM available for dynamic allocation:
  35. I (354) heap_init: At 3FFAE6E0 len 0000F480 (61 KiB): DRAM
  36. I (360) heap_init: At 3FFC6A30 len 000195D0 (101 KiB): DRAM
  37. I (367) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
  38. I (373) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
  39. I (379) heap_init: At 400906A8 len 0000F958 (62 KiB): IRAM
  40. I (386) cpu_start: Pro cpu start user code
  41. I (68) cpu_start: Starting scheduler on PRO CPU.
  42. I (0) cpu_start: Starting scheduler on APP CPU.
  43. I (176) wifi: wifi driver task: 3ffb7b20, prio:23, stack:3584, core=0
  44. I (176) wifi: wifi firmware version: 9415913
  45. I (176) wifi: config NVS flash: enabled
  46. I (186) wifi: config nano formating: disabled
  47. I (186) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
  48. I (196) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
  49. I (226) wifi: Init dynamic tx buffer num: 32
  50. I (226) wifi: Init data frame dynamic rx buffer num: 32
  51. I (226) wifi: Init management frame dynamic rx buffer num: 32
  52. I (226) wifi: Init static rx buffer size: 1600
  53. I (236) wifi: Init static rx buffer num: 10
  54. I (236) wifi: Init dynamic rx buffer num: 32
  55. I (246) WLAN: Setting WiFi configuration SSID WLAN...
  56. I (246) wpa: WPA2 ENTERPRISE VERSION: [v2.0] enable
  57.  
  58. I (306) phy: phy_version: 4008, c9ae59f, Jan 25 2019, 16:54:06, 0, 0
  59. I (316) wifi: mode : sta (d8:a0:1d:61:12:b8)
  60. I (2846) wifi: n:6 0, o:1 0, ap:255 255, sta:6 0, prof:1
  61. I (3686) wifi: state: init -> auth (b0)
  62. I (3696) wifi: state: auth -> assoc (0)
  63. I (3696) wifi: state: assoc -> run (10)
  64. I (3706) wpa: wpa2_task prio:2, stack:6656
  65.  
  66. I (3726) wpa: SSL: Need 2227 bytes more input data
  67. I (3746) wpa: SSL: Need 837 bytes more input data
  68. I (3926) wpa: EAP-TLV: TLV Result - Success - EAP-TLV/Phase2 Completed
  69. I (3936) wpa: >>>>>wpa2 FINISH
  70.  
  71. I (3946) wifi: connected with WLAN, channel 6
  72. I (3946) wifi: pm start, type: 1
  73.  
  74. I (5316) WLAN: ~~~~~~~~~~~
  75. I (5316) WLAN: IP:0.0.0.0
  76. I (5316) WLAN: MASK:0.0.0.0
  77. I (5316) WLAN: GW:0.0.0.0
  78. I (5316) WLAN: ~~~~~~~~~~~
  79. I (8316) WLAN: ~~~~~~~~~~~
  80. I (8316) WLAN: IP:0.0.0.0
  81. I (8316) WLAN: MASK:0.0.0.0
  82. I (8316) WLAN: GW:0.0.0.0
  83. I (8316) WLAN: ~~~~~~~~~~~
  84.  

Any ideas on what is wrong with the second code listing?
Thank you very much.

Who is online

Users browsing this forum: Bing [Bot] and 120 guests