Programming in C++ and using esp_wifi
Programming in C++ and using esp_wifi
Hello Guy,
I'm trying to compile a C++ project in eclipse. Now, I was using the wifi feature and wanted to use the function esp_wifi_set_config(WF_IF_AP, &sta_config). Unfortunately, C++ doesn't support designeted initializers - but all of the configuration Makrus etc. are using constructs like
wifi_config_t sta_config={.sta={.ssid="foo",.password="bar, (...) }};
Of, cause - it is possible to change from C++ to C, but there are Libraries, which are written in C++ (e.g. WS2812b lib).
Do you have any suggestions, how to get this problem solved?
I'm trying to compile a C++ project in eclipse. Now, I was using the wifi feature and wanted to use the function esp_wifi_set_config(WF_IF_AP, &sta_config). Unfortunately, C++ doesn't support designeted initializers - but all of the configuration Makrus etc. are using constructs like
wifi_config_t sta_config={.sta={.ssid="foo",.password="bar, (...) }};
Of, cause - it is possible to change from C++ to C, but there are Libraries, which are written in C++ (e.g. WS2812b lib).
Do you have any suggestions, how to get this problem solved?
Re: Programming in C++ and using esp_wifi
Your timing is great. I too have been studying C++ programming in an ESP-IDF environment. Specifically not just C++ but the use of the Standard Template Library (something I have never looked at before). As I write sample project after sample project in C, I found myself copying and pasting C code over and over again. With the leverage of C++, I started compartmentalizing my code into classes and am really please with the improvements and comprehension of apps. To that end Ive started creating classes (primarily for my use) but I offer them up as fragments/samples here:
https://github.com/nkolban/esp32-snippe ... /cpp_utils
This is early days and I fully expect the number of classes I use to increase.
In addition, the Arduino libraries can be used in your C++ apps simply by linking. So that may also be a set of pre-made classes ready to go.
The question I am lieing awake at night wondering is ...
"If, as ESP32 programmers and ESP-IDF programmers, we had no pre-existing C++ classes but we did have the ESP-IDF specification, what would a set of C++ classes look like for ESP-IDF programming?" .... and that is what I am tinkering with. However, I suspect that should be a community project in its own right. So if we as a community want to create a "specification" and then "implementation" of C++ classes that provide value add to ESP-IDF, I'd be delighted to join such an effort.
https://github.com/nkolban/esp32-snippe ... /cpp_utils
This is early days and I fully expect the number of classes I use to increase.
In addition, the Arduino libraries can be used in your C++ apps simply by linking. So that may also be a set of pre-made classes ready to go.
The question I am lieing awake at night wondering is ...
"If, as ESP32 programmers and ESP-IDF programmers, we had no pre-existing C++ classes but we did have the ESP-IDF specification, what would a set of C++ classes look like for ESP-IDF programming?" .... and that is what I am tinkering with. However, I suspect that should be a community project in its own right. So if we as a community want to create a "specification" and then "implementation" of C++ classes that provide value add to ESP-IDF, I'd be delighted to join such an effort.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Programming in C++ and using esp_wifi
Hello Kolban,
First of all - you do awesome work! I'm using your book of the esp32 to getting started with the chip and it is great! Especially the chapter about the debugger saved a lot of time.
In chapter about the neopixels your writing about an example library - this lib worked great and I decided to start in c++, but maybe you should add, that c++ isn't supported well yet.
Thanks for these example snippets - they look promising. Right now, I'm at work and can't test them in my project.
Thanks
Florian
First of all - you do awesome work! I'm using your book of the esp32 to getting started with the chip and it is great! Especially the chapter about the debugger saved a lot of time.
In chapter about the neopixels your writing about an example library - this lib worked great and I decided to start in c++, but maybe you should add, that c++ isn't supported well yet.
Thanks for these example snippets - they look promising. Right now, I'm at work and can't test them in my project.
Thanks
Florian
Re: Programming in C++ and using esp_wifi
Howdy Florian,
Thanks for the kind words. I'm curious about your comment that C++ isn't supported well yet. I haven't seen any issues (so far). Could you post back what you consider to be lacking in C++ support? Do you think Espressif should be supplying C++ classes as part of the ESP-IDF? Is something (form a technical perspective) not working as it should?
Thanks for the kind words. I'm curious about your comment that C++ isn't supported well yet. I haven't seen any issues (so far). Could you post back what you consider to be lacking in C++ support? Do you think Espressif should be supplying C++ classes as part of the ESP-IDF? Is something (form a technical perspective) not working as it should?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Programming in C++ and using esp_wifi
At the moment the best suggestion I have (unfortunately) is to look at these macro definitions and set the same struct fields by hand in your C++ files.flo1211 wrote:Unfortunately, C++ doesn't support designeted initializers - but all of the configuration Makrus etc. are using constructs like
wifi_config_t sta_config={.sta={.ssid="foo",.password="bar, (...) }};
It seems like there are ways we can potentially refactor this initialization pattern to be C++-friendly. I hope to take a proper look over the next few days.
Angus
Re: Programming in C++ and using esp_wifi
I looked into this a bit more. GCC's g++ will allow designated initializers but only if the members of the structure are initialized in the same order as the structure definition. So, for example, WIFI_INIT_CONFIG_DEFAULT() works when used in C++. Otherwise, the error is "sorry, unimplemented: non-trivial designated initializers not supported".flo1211 wrote:Unfortunately, C++ doesn't support designeted initializers - but all of the configuration Makrus etc. are using constructs like
wifi_config_t sta_config={.sta={.ssid="foo",.password="bar, (...) }};
It appears you can leave off any number of members at the end, but the "trivial" initializer must include every other member starting from the first, in order.
For structures like wifi_config_t, the C code from the examples won't compile as C++ (as you've noticed.) I can't find a nice way to write these kind of nested designated initialisers for g++. The best you can do is:
Code: Select all
wifi_config_t sta_config = { }
sta_config.sta.ssid = "foo";
sta_config.sta.password = "bar";
// etc
(Note that the '= { }' is important, so unset fields in sta_config are initialized to zero. An easy gotcha as GCC won't warn on this, even for obvious transgressions of it.)
If you find an initializer macro in a header file (similar to WIFI_INIT_CONFIG_DEFAULT()) that doesn't work in C++ because the members are missing or out of order, please either let us know or send a Pull Request on github.
Angus
Re: Programming in C++ and using esp_wifi
Hi, has anyone gotten the C++ syntax working?
I'm trying to make a Wifi-connection from a .cpp file using this:
But for some reason the compilation fails with:
/home/frode/GIT/Ghost/ESP32/Ghost32Folkracer/main/./main.cpp:411:25: error: incompatible types in assignment of 'const char [6]' to 'uint8_t [32] {aka unsigned char [32]}'
sta_config.sta.ssid = "mynet";
^
/home/frode/GIT/Ghost/ESP32/Ghost32Folkracer/main/./main.cpp:412:29: error: incompatible types in assignment of 'const char [7]' to 'uint8_t [64] {aka unsigned char [64]}'
sta_config.sta.password = "s3cret";
^
Why does it mean that I'm trying to write to an integer?
I'm trying to make a Wifi-connection from a .cpp file using this:
Code: Select all
//Connect to Wi-Fi
tcpip_adapter_init();
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) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = { };
sta_config.sta.ssid = "mynet";
sta_config.sta.password = "s3cret";
sta_config.sta.bssid_set = false;
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
/home/frode/GIT/Ghost/ESP32/Ghost32Folkracer/main/./main.cpp:411:25: error: incompatible types in assignment of 'const char [6]' to 'uint8_t [32] {aka unsigned char [32]}'
sta_config.sta.ssid = "mynet";
^
/home/frode/GIT/Ghost/ESP32/Ghost32Folkracer/main/./main.cpp:412:29: error: incompatible types in assignment of 'const char [7]' to 'uint8_t [64] {aka unsigned char [64]}'
sta_config.sta.password = "s3cret";
^
Why does it mean that I'm trying to write to an integer?
Re: Programming in C++ and using esp_wifi
The ssid and password fields contained in sta_config.sta are not "pointers to char" but are instead arrays of bytes. The ssid is a 32 byte array and the password is a 64 byte array. What you need to do is copy your strings into those arrays. For example:
strcpy(sta_config.sta.ssid, "mynet");
strcpy(sta_config.sta.password, "s3cret");
strcpy(sta_config.sta.ssid, "mynet");
strcpy(sta_config.sta.password, "s3cret");
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Programming in C++ and using esp_wifi
You can init structs in C++ but all elements of the struct has to be included in the initialization in the order they are declared as you can not reference properties by name. Luckily, in C++, the struct can be initialized with {} and each property assigned separately.
Code: Select all
//Allocate storage for the struct
wifi_config_t sta_config = {};
//Assign ssid & password strings
strcpy((char*)sta_config.sta.ssid, "ssid");
strcpy((char*)sta_config.sta.password, "password");
sta_config.sta.bssid_set = false;
-
- Posts: 5
- Joined: Sat May 26, 2018 12:35 am
Re: Programming in C++ and using esp_wifi
I have been using Arduino IDE to program the ESP32, i wanted to program the SPI slave example using it and failed so i switched to the ESP-IDF and everything was going well but when i need to use the Wifi the esp_wifi.h is advanced for me and i wonder how can i use the WiFi.h library from arduino ESP32 support in the ESP-IDF?
My application is with ESPNOW so i need first to initialize wifi, check for slave and pair them.
My application is with ESPNOW so i need first to initialize wifi, check for slave and pair them.
Who is online
Users browsing this forum: No registered users and 77 guests