Page 1 of 1

Can't dynamically choose SSID, "missing braces"

Posted: Sun Dec 29, 2019 12:39 am
by Dumbledore
Morning!

I have some problems setting up the SSID config for my WiFi.

It works flawlessly with the STA/AP example by using #define, e.g.:

Code: Select all

wifi_config_t wifi_config = {
	.sta = {
		.ssid = EXAMPLE_ESP_WIFI_SSID, 
		.password = EXAMPLE_ESP_WIFI_PASS
	},
};
but when I have some dynamically generated string and try to use it like

Code: Select all

wifi_config_t wifi_config = {
	.sta = {
		.ssid = generated_string, 
		.password = EXAMPLE_ESP_WIFI_PASS
	},
};
I get some error about missing brackets:

Code: Select all

../main/wifi_client.c:750:53: warning: initialization of 'unsigned char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
        wifi_config_t wifi_config = {.sta = {.ssid = partner_ssid, .password =WIFI_PASS},};
                                                     ^~~~~~~~~~~~
../main/wifi_client.c:750:53: note: (near initialization for 'wifi_config.sta.ssid[0]')
../main/wifi_client.c:750:36: error: missing braces around initializer [-Werror=missing-braces]
        wifi_config_t wifi_config = {.sta = {.ssid = partner_ssid, .password =WIFI_PASS},};
                                    ^
                                                     {           }
../main/wifi_client.c:750:36: error: missing braces around initializer [-Werror=missing-braces]
        wifi_config_t wifi_config = {.sta = {.ssid = partner_ssid, .password =WIFI_PASS},};
                                    ^
                                                     {           }
cc1.exe: some warnings being treated as errors
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
Can someone explain what went wrong? I tried to replace

Code: Select all

.ssid = generated_string
with

Code: Select all

.ssid = {generated_string}
and it compiled, but the WiFi seemed to have a faulty SSID (being shown by '?' squares).

PS: Just to avoid confusion, I've shown the STA part of my code and the error, the AP part is the same and is the reason for the faulty WiFi.

Thanks in advance!

Re: Can't dynamically choose SSID, "missing braces"

Posted: Sun Dec 29, 2019 2:24 am
by ESP_Sprite
Your main problem is this:

Code: Select all

warning: initialization of 'unsigned char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
The .ssid field you're using is a character array, and you're stashing a pointer to chars in there; that's not gonna work. You'll need to copy the data: remove the .ssid line entirely and do something like strncpy(wifi_config.ssid, generated_string, sizeof(wifi_config.ssid)) later on.