esp32blufi
时间: 2023-11-26 15:05:37 浏览: 301
ESP32 蓝牙配网Blufi ,BIN二进制文件,下载烧录直接可用。
ESP32 Blufi是一种基于蓝牙通道的Wi-Fi网络配置功能,适用于ESP32。它通过安全协议将Wi-Fi配置和证书传输到ESP32,然后ESP32可以基于这些信息连接到AP或建立SoftAP。使用ESP32 Blufi可以方便地进行Wi-Fi网络配置,而无需连接到串行端口。以下是使用ESP32 Blufi进行Wi-Fi网络配置的步骤:
1.下载并烧录ESP32 Blufi BIN文件到ESP32开发板中。
2.使用蓝牙调试应用程序连接ESP32开发板。
3.在蓝牙调试应用程序中输入Wi-Fi网络的SSID和密码。
4.ESP32将自动连接到Wi-Fi网络。
以下是ESP32 Blufi的示例代码:
```c
#include <WiFi.h>
#include <esp_wifi.h>
#include <esp_wpa2.h>
#include <esp_event_loop.h>
#include <esp_log.h>
#include <nvs_flash.h>
#include <blufi.h>
#define EXAMPLE_ESP_WIFI_SSID "your_ssid"
#define EXAMPLE_ESP_WIFI_PASS "your_password"
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
static EventGroupHandle_t s_wifi_event_group;
static int s_retry_num = 0;
static const char *TAG = "wifi station";
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail\n");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
{
switch (event) {
case ESP_BLUFI_EVENT_INIT_FINISH:
ESP_LOGI(TAG, "Blufi init finish\n");
esp_ble_gap_set_device_name("ESP32-BLUEFI");
esp_ble_gap_config_adv_data(&blufi_adv_data);
break;
case ESP_BLUFI_EVENT_DEINIT_FINISH:
break;
case ESP_BLUFI_EVENT_BLE_CONNECT:
ESP_LOGI(TAG, "Blufi ble connect\n");
break;
case ESP_BLUFI_EVENT_BLE_DISCONNECT:
ESP_LOGI(TAG, "Blufi ble disconnect\n");
break;
case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
ESP_LOGI(TAG, "Blufi set wifi opmode %d\n", param->wifi_mode.op_mode);
wifi_init_sta();
break;
case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP:
ESP_LOGI(TAG, "Blufi requset wifi connect to AP\n");
wifi_config_t sta_config;
memset(&sta_config, 0, sizeof(sta_config));
memcpy(sta_config.sta.ssid, param->connect_to_ap.ssid, param->connect_to_ap.ssid_len);
memcpy(sta_config.sta.password, param->connect_to_ap.password, param->connect_to_ap.password_len);
sta_config.sta.bssid_set = param->connect_to_ap.bssid_set;
if (sta_config.sta.bssid_set == true) {
memcpy(sta_config.sta.bssid, param->connect_to_ap.bssid, 6);
}
esp_wifi_disconnect();
esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);
esp_wifi_connect();
break;
case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP:
ESP_LOGI(TAG, "Blufi requset wifi disconnect from AP\n");
esp_wifi_disconnect();
break;
default:
break;
}
}
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bluedroid_init();
esp_bluedroid_enable();
esp_ble_gap_register_callback(blufi_event_callback);
esp_blufi_register_callbacks(&blufi_callbacks);
esp_blufi_profile_init();
}
```
阅读全文