问题
在esp32c3开发中,使用字符数组保存了sta模式需要用到的ssid、password
char sta_ssid[32]=“MST_XIAOMI”
char sta_password[64]=“123456789”
而在进行sta模式配置时如下:
//wifi配置
wifi_config_t wifi_config = {
.sta = {
.ssid = sta_addr,
.password = sta_password,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
该结构体中 变量 .ssid 和 .password 类型如下:
直接将数组名赋值过来,在编译时报错如下:
(从’char *'初始化’unsigned char’将从指针生成整型,而不需要强制转换)
解析
参考station项目的配置内容如下:
有宏定义:
#define EXAMPLE_ESP_WIFI_SSID “MST_XIAOMI”
#define EXAMPLE_ESP_WIFI_PASS “123456789”
//wifi配置
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
即相当于赋值如下:
.ssid = “MST_XIAOMI”
.password= “123456789”
而我赋值如下:
char sta_ssid[32]=“MST_XIAOMI”
char sta_password[64]=“123456789”
.ssid=sta_ssid
.password=sta_password
直接用字符数组名赋值只是将数组首地址给出,并不给出数组中的字符串,所以编译出错,不能以这种形式赋值
于是改变为:
//wifi配置
wifi_config_t wifi_config = {
.sta = {
// .ssid = sta_addr, //STA_ESP_WIFI_SSID,
// .password = sta_password, //STA_ESP_WIFI_PASS,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
strcpy((char *)wifi_config.sta.ssid,sta_addr);
strcpy((char *)wifi_config.sta.password,sta_password);
采用strcpy 来赋值字符串,且第一个参数要强转为char *,否则报错
(“strcpy”的传递参数1中的指针目标的符号不同)