Closed GilDev closed 4 years ago
You didn't zero out wifi_config
To add to @negativekelvin's answer, the reason is mainly that C and C++ differs when using structure initialization. I don't know if it's still the case, but previously the code used field name assignment to initialize structure member. However, this is not valid in C++ and as such you must initialize the struct yourself. so, write:
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = {};
strcpy((char *) wifi_config.sta.ssid, EXAMPLE_ESP_WIFI_SSID);
strcpy((char *) wifi_config.sta.password, EXAMPLE_ESP_WIFI_PASS);
or
memset(&wifi_config, 0, sizeof(wifi_config));
This is valid GNU's C but invalid C++:
wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_ESP_WIFI_SSID, .password = EXAMPLE_ESP_WIFI_PASS } };
Thanks @negativekelvin @X-Ryl669! Indeed I saw the structure initialization was illegal in C++, that’s why I removed it, but I haven’t thought about zeroing it out again…
Thanks guys! We tried everything else to get it working but this finally worked :)
Environment
Problem Description
I’m trying to convert (kind of) the WiFi station example from C to C++, but the C++ version will never connect and the log is missing something as you can see below.
Expected Behavior
The ESP32 would connect like in the standard example.
Actual Behavior
It retries until it uses all the retries are used and then print:
Code to reproduce this issue
// If your code is longer than 30 lines, GIST is preferred.
Debug Logs
From the esp-idf example:
From my program in C++:
I have no idea of why it’s not initializing the WiFi properly, nothing changed aside from the class embedding both functions. Thanks for taking the time to look at this!