WPA2: Arduino framework works but ESP-IDF does not
Posted: 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.
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)
Any ideas on what is wrong with the second code listing?
Thank you very much.
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.
- #include <Arduino.h>
- #include "esp_wpa2.h"
- #include <WiFi.h>
- const char *ssid = "MYSSID";
- #define EAP_ID "MYSSID"
- #define EAP_USERNAME "test123"
- #define EAP_PASSWORD "test123"
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- delay(10);
- WiFi.disconnect(true);
- WiFi.mode(WIFI_STA);
- esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));
- esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));
- esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
- esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
- esp_wifi_sta_wpa2_ent_enable(&config);
- WiFi.begin(ssid);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.macAddress());
- Serial.println(WiFi.localIP());
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- }
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)
- // includes omitted for brevity
- #define WIFI_SSID "MYWLAN"
- #define EAP_METHOD 0
- const char *ssid = "MYSSID";
- #define EAP_ID "MYSSID"
- #define USERNAME "test123"
- #define PASSWORD "test123"
- static EventGroupHandle_t wifi_event_group;
- const int CONNECTED_BIT = BIT0;
- #define EAP_PEAP 1
- #define EAP_TTLS 2
- static const char *TAG = "WLAN";
- static esp_err_t event_handler(void *ctx, system_event_t *event)
- {
- switch (event->event_id)
- {
- case SYSTEM_EVENT_STA_START:
- esp_wifi_connect();
- break;
- case SYSTEM_EVENT_STA_GOT_IP:
- xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
- break;
- case SYSTEM_EVENT_STA_DISCONNECTED:
- esp_wifi_connect();
- xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
- break;
- default:
- break;
- }
- return ESP_OK;
- }
- static void initialise_wifi(void)
- {
- tcpip_adapter_init();
- wifi_event_group = xEventGroupCreate();
- ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
- wifi_config_t wifi_config = {
- .sta = {
- .ssid = WIFI_SSID
- },
- };
- ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
- ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID)));
- ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_username((uint8_t *)USERNAME, strlen(USERNAME)));
- ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_set_password((uint8_t *)PASSWORD, strlen(PASSWORD)));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
- esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_sta_wpa2_ent_enable(&config));
- ESP_ERROR_CHECK(esp_wifi_start());
- }
- static void wpa2_enterprise_example_task(void *pvParameters)
- {
- tcpip_adapter_ip_info_t ip;
- memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
- vTaskDelay(2000 / portTICK_PERIOD_MS);
- while (1)
- {
- vTaskDelay(3000 / portTICK_PERIOD_MS);
- if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0)
- {
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- ESP_LOGI(TAG, "IP:" IPSTR, IP2STR(&ip.ip));
- ESP_LOGI(TAG, "MASK:" IPSTR, IP2STR(&ip.netmask));
- ESP_LOGI(TAG, "GW:" IPSTR, IP2STR(&ip.gw));
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- }
- }
- }
- void app_main()
- {
- ESP_ERROR_CHECK(nvs_flash_init());
- initialise_wifi();
- xTaskCreate(&wpa2_enterprise_example_task, "wpa2_enterprise_example_task", 4096, NULL, 5, NULL);
- }
- rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
- configsip: 188777542, SPIWP:0xee
- clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
- mode:DIO, clock div:2
- load:0x3fff0018,len:4
- load:0x3fff001c,len:6120
- load:0x40078000,len:8816
- ho 0 tail 12 room 4
- load:0x40080400,len:6392
- entry 0x40080744
- I (31) boot: ESP-IDF 3.30200.190418 2nd stage bootloader
- I (31) boot: compile time 10:43:14
- I (31) boot: Enabling RNG early entropy source...
- I (36) boot: SPI Speed : 40MHz
- I (40) boot: SPI Mode : DIO
- I (44) boot: SPI Flash Size : 4MB
- I (48) boot: Partition Table:
- I (52) boot: ## Label Usage Type ST Offset Length
- I (59) boot: 0 nvs WiFi data 01 02 00009000 00006000
- I (67) boot: 1 phy_init RF data 01 01 0000f000 00001000
- I (74) boot: 2 factory factory app 00 00 00010000 00100000
- I (82) boot: End of partition table
- I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x17928 ( 96552) map
- I (128) esp_image: segment 1: paddr=0x00027950 vaddr=0x3ffbdb60 size=0x02e94 ( 11924) load
- I (133) esp_image: segment 2: paddr=0x0002a7ec vaddr=0x40080000 size=0x00400 ( 1024) load
- I (135) esp_image: segment 3: paddr=0x0002abf4 vaddr=0x40080400 size=0x0541c ( 21532) load
- I (152) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x6babc (441020) map
- I (305) esp_image: segment 5: paddr=0x0009badc vaddr=0x4008581c size=0x0ae8c ( 44684) load
- I (333) boot: Loaded app from partition at offset 0x10000
- I (333) boot: Disabling RNG early entropy source...
- I (333) cpu_start: Pro cpu up.
- I (337) cpu_start: Starting app cpu, entry point is 0x400821ac
- I (0) cpu_start: App cpu up.
- I (347) heap_init: Initializing. RAM available for dynamic allocation:
- I (354) heap_init: At 3FFAE6E0 len 0000F480 (61 KiB): DRAM
- I (360) heap_init: At 3FFC6A30 len 000195D0 (101 KiB): DRAM
- I (367) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
- I (373) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
- I (379) heap_init: At 400906A8 len 0000F958 (62 KiB): IRAM
- I (386) cpu_start: Pro cpu start user code
- I (68) cpu_start: Starting scheduler on PRO CPU.
- I (0) cpu_start: Starting scheduler on APP CPU.
- I (176) wifi: wifi driver task: 3ffb7b20, prio:23, stack:3584, core=0
- I (176) wifi: wifi firmware version: 9415913
- I (176) wifi: config NVS flash: enabled
- I (186) wifi: config nano formating: disabled
- I (186) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
- I (196) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
- I (226) wifi: Init dynamic tx buffer num: 32
- I (226) wifi: Init data frame dynamic rx buffer num: 32
- I (226) wifi: Init management frame dynamic rx buffer num: 32
- I (226) wifi: Init static rx buffer size: 1600
- I (236) wifi: Init static rx buffer num: 10
- I (236) wifi: Init dynamic rx buffer num: 32
- I (246) WLAN: Setting WiFi configuration SSID WLAN...
- I (246) wpa: WPA2 ENTERPRISE VERSION: [v2.0] enable
- I (306) phy: phy_version: 4008, c9ae59f, Jan 25 2019, 16:54:06, 0, 0
- I (316) wifi: mode : sta (d8:a0:1d:61:12:b8)
- I (2846) wifi: n:6 0, o:1 0, ap:255 255, sta:6 0, prof:1
- I (3686) wifi: state: init -> auth (b0)
- I (3696) wifi: state: auth -> assoc (0)
- I (3696) wifi: state: assoc -> run (10)
- I (3706) wpa: wpa2_task prio:2, stack:6656
- I (3726) wpa: SSL: Need 2227 bytes more input data
- I (3746) wpa: SSL: Need 837 bytes more input data
- I (3926) wpa: EAP-TLV: TLV Result - Success - EAP-TLV/Phase2 Completed
- I (3936) wpa: >>>>>wpa2 FINISH
- I (3946) wifi: connected with WLAN, channel 6
- I (3946) wifi: pm start, type: 1
- I (5316) WLAN: ~~~~~~~~~~~
- I (5316) WLAN: IP:0.0.0.0
- I (5316) WLAN: MASK:0.0.0.0
- I (5316) WLAN: GW:0.0.0.0
- I (5316) WLAN: ~~~~~~~~~~~
- I (8316) WLAN: ~~~~~~~~~~~
- I (8316) WLAN: IP:0.0.0.0
- I (8316) WLAN: MASK:0.0.0.0
- I (8316) WLAN: GW:0.0.0.0
- I (8316) WLAN: ~~~~~~~~~~~
Any ideas on what is wrong with the second code listing?
Thank you very much.