四博智联ESP32模组:多种安全高效的蓝牙配网方案
在物联网(IoT)设备日益普及的今天,如何高效、安全地连接设备至网络成为了用户和开发者共同关注的焦点。四博智联基于乐鑫ESP32系列芯片推出的模组,不仅在性能和功耗方面表现卓越,更通过蓝牙配网(BluFi)技术和微信小程序的配网方案,为设备提供了强大而安全的网络配置功能。
模组:
详细说明
BluFi:通过蓝牙实现Wi-Fi网络配置
BluFi 是ESP32系列芯片独有的通过蓝牙配置Wi-Fi网络的功能。通过BluFi,用户可以通过蓝牙将Wi-Fi网络的配置参数(如SSID和密码)传递给ESP32设备。这样,设备能够在不需要手动输入的情况下,快速、安全地连接到Wi-Fi网络。这一技术的核心优势在于它既简化了设备的联网流程,又大大提升了用户体验。
微信小程序:灵活便捷的蓝牙配网
除了BluFi,微信小程序还提供了一种更加灵活、便捷的蓝牙配网方案。用户可以通过微信小程序与ESP32设备建立蓝牙连接,并将Wi-Fi配置信息直接发送给设备,实现快速配网。这一方式特别适合对用户体验要求较高的消费类物联网产品。
示例代码:BluFi 配网
下面是一个使用BluFi进行蓝牙配网的Arduino示例代码,该代码展示了如何在ESP32上使用BluFi实现Wi-Fi网络配置,并确保传输数据的安全性。
#include <WiFi.h>
#include "esp_blufi_api.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
// 设置WiFi SSID和密码的缓冲区
char ssid[32] = {0};
char password[64] = {0};
// 定义BluFi事件回调函数
static void blufi_event_handler(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param) {
switch (event) {
case ESP_BLUFI_EVENT_INIT_FINISH:
Serial.println("BluFi initialized.");
esp_blufi_adv_start();
break;
case ESP_BLUFI_EVENT_DEINIT_FINISH:
Serial.println("BluFi deinitialized.");
break;
case ESP_BLUFI_EVENT_RECV_STA_SSID:
memset(ssid, 0, sizeof(ssid));
memcpy(ssid, param->sta_ssid.ssid, param->sta_ssid.ssid_len);
Serial.printf("Received SSID: %s\n", ssid);
break;
case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
memset(password, 0, sizeof(password));
memcpy(password, param->sta_passwd.passwd, param->sta_passwd.passwd_len);
Serial.printf("Received Password: %s\n", password);
break;
case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE:
Serial.println("BluFi BLE disconnected.");
break;
case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
Serial.println("Received custom data.");
break;
default:
break;
}
}
// 定义BluFi回调结构体
esp_blufi_callbacks_t blufi_callbacks = {
.event_cb = blufi_event_handler,
// 定义其他回调函数...
};
void setup() {
Serial.begin(115200);
Serial.println("Initializing BluFi...");
// 初始化NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// 初始化蓝牙
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
// 初始化BluFi
esp_blufi_register_callbacks(&blufi_callbacks);
ESP_ERROR_CHECK(esp_blufi_profile_init());
// 初始化WiFi
WiFi.mode(WIFI_MODE_STA);
}
void loop() {
// 如果已经接收到SSID和密码,则尝试连接WiFi
if (strlen(ssid) > 0 && strlen(password) > 0) {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
}
示例代码:微信小程序配网
下面是使用微信小程序通过蓝牙进行ESP32配网的示例代码。该代码展示了如何在微信小程序中建立与ESP32的蓝牙连接,并将Wi-Fi配置信息发送给设备。
微信小程序端代码示例
Page({
data: {
deviceId: '',
serviceId: '',
characteristicId: '',
ssid: '',
password: ''
},
onLoad: function() {
wx.openBluetoothAdapter({
success: function(res) {
console.log('Bluetooth adapter initialized:', res);
},
fail: function(res) {
console.log('Bluetooth adapter initialization failed:', res);
}
});
},
connectDevice: function() {
const that = this;
wx.createBLEConnection({
deviceId: that.data.deviceId,
success: function(res) {
console.log('Connected to device:', res);
// 获取服务
wx.getBLEDeviceServices({
deviceId: that.data.deviceId,
success: function(res) {
that.setData({
serviceId: res.services[0].uuid // 假设使用第一个服务
});
// 获取特征值
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
success: function(res) {
that.setData({
characteristicId: res.characteristics[0].uuid // 假设使用第一个特征值
});
}
});
}
});
}
});
},
sendWifiCredentials: function() {
const that = this;
const buffer = new ArrayBuffer(that.data.ssid.length + that.data.password.length + 2);
const dataView = new DataView(buffer);
for (let i = 0; i < that.data.ssid.length; i++) {
dataView.setUint8(i, that.data.ssid.charCodeAt(i));
}
dataView.setUint8(that.data.ssid.length, 0); // SSID 和密码之间的分隔符
for (let i = 0; i < that.data.password.length; i++) {
dataView.setUint8(that.data.ssid.length + 1 + i, that.data.password.charCodeAt(i));
}
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.characteristicId,
value: buffer,
success: function(res) {
console.log('Wi-Fi credentials sent successfully:', res);
},
fail: function(res) {
console.log('Failed to send Wi-Fi credentials:', res);
}
});
}
});
ESP32端代码示例
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <WiFi.h>
BLECharacteristic *pCharacteristic;
std::string ssid;
std::string password;
class MyCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
int separatorIndex = value.find('\0');
ssid = value.substr(0, separatorIndex);
password = value.substr(separatorIndex + 1);
Serial.printf("Received SSID: %s, Password: %s\n", ssid.c_str(), password.c_str());
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32_BLE_WiFi");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
pCharacteristic = pService->createCharacteristic(
BLEUUID(CHARACTERISTIC_UUID),
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// 其他功能代码
}
结语
通过上述代码示例,我们展示了两种不同的蓝牙配网实现方式:BluFi和微信小程序。这些方案不仅提升了用户体验,还确保了