Page 1 of 1

蓝牙BLE 自动配网报错

Posted: Tue Oct 08, 2024 4:56 pm
by wiltonliu
我希望实现启动自动配网的功能,下面是我的代码。

我第一版代码没有增加判断,只是通过蓝牙BLE进行了配网,完成后可以看到IP地址出现,证明网络已经连接成功。
但是我希望可以让设备启动后自动联网,就增加了


static void wifi_init_sta(void)
{
/* Start Wi-Fi in station mode */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}

还有man函数内加了。
bool provisioned = false;
if (!provisioned)
{
ESP_LOGI(TAG, "Starting provisioning");

/* What is the Device Service Name that we want
* This translates to :
* - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
* - device name when scheme is wifi_prov_scheme_ble
*/
char service_name[60];
get_device_service_name(service_name, sizeof(wifi_sta_config_t));


}
else{

/* We don't need the manager as device is already provisioned,
* so let's release it's resources */
wifi_prov_mgr_deinit();

/* Start Wi-Fi station */
wifi_init_sta();
}



问题:现在启动后 error: implicit declaration of function 'get_device_service_name' [-Werror=implicit-function-declaration]
96 | get_device_service_name(service_name, sizeof(wifi_sta_config_t));


如果我去掉。get_device_service_name(service_name, sizeof(wifi_sta_config_t)); 第二次上电又无法连接WiFi。





我的完整代码如下:



#include <stdio.h>
#include <nvs_flash.h>
#include <esp_wifi.h>
#include <wifi_provisioning/manager.h>
#include <wifi_provisioning/scheme_ble.h>
#include <esp_log.h>
#include <FreeRTOS/FreeRTOS.h>
#include <FreeRTOS/task.h>


#define TAG "prov-demo"

void sh_viod_event_cb(void* event_handler_arg,esp_event_base_t event_base,int32_t event_id,void* event_data)
{
if(WIFI_EVENT)
{


}else if (WIFI_PROV_EVENT)
{
switch (event_id)
{
case WIFI_PROV_START:
ESP_LOGI(TAG,"Provisioning start \n");
break;
case WIFI_PROV_CRED_RECV:
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
ESP_LOGI(TAG,"Received Wi-Fi credentials"
"\n\tSSID :%s\b\tPassword : %s \n",
(const char *)wifi_sta_cfg->ssid,
(const char *)wifi_sta_cfg->password);
break;
case WIFI_PROV_CRED_SUCCESS:
ESP_LOGI(TAG,"Provisioning successful \n");
break;
case WIFI_PROV_END:
wifi_prov_mgr_deinit();
break;
default:
break;
}
}else if (IP_EVENT)
{
/* code */
}

}

static void wifi_init_sta(void)
{
/* Start Wi-Fi in station mode */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}

void app_main(void)
{

//配网初始化
nvs_flash_init();
ESP_ERROR_CHECK(nvs_flash_erase()); //将上次储存在flash中的ssid等信息清除
ESP_ERROR_CHECK(nvs_flash_init()); //nvs_flsh_erase后需要重新初始化
esp_netif_init();
/*创建默认事件处理队列*/
esp_event_loop_create_default();
/*初始化网络协议族*/
esp_netif_init();
esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&config);
//配网注册事件处理
esp_event_handler_register(WIFI_EVENT,ESP_EVENT_ANY_ID,sh_viod_event_cb,NULL);
esp_event_handler_register(WIFI_PROV_EVENT,ESP_EVENT_ANY_ID,sh_viod_event_cb,NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, sh_viod_event_cb,NULL);

wifi_prov_mgr_config_t prov_mgr_config = {
.scheme = wifi_prov_scheme_ble,
.scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE,
};

//配网蓝牙参数设置
wifi_prov_mgr_init(prov_mgr_config);
wifi_prov_mgr_start_provisioning(WIFI_PROV_SECURITY_1, "123456","PROV_SH_Void_NET",NULL);
bool provisioned = false;
if (!provisioned)
{
ESP_LOGI(TAG, "Starting provisioning");

/* What is the Device Service Name that we want
* This translates to :
* - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
* - device name when scheme is wifi_prov_scheme_ble
*/
char service_name[60];
get_device_service_name(service_name, sizeof(wifi_sta_config_t));


}
else{

/* We don't need the manager as device is already provisioned,
* so let's release it's resources */
wifi_prov_mgr_deinit();

/* Start Wi-Fi station */
wifi_init_sta();
}
while (1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}


}