接到一个客户开发需求,要求开发一个基于大模型和ESP32S3的AI聊天机器人,要支持如下功能:
1.可以通过微信小程序或是APP配网。
2.可以通过微信小程序或是APP选择唤醒词,支持支持硬件aec降噪,支持选择ASR服务、选择大模型服务、选择TTS服务。
3.开发要快,最多一周要完成原型开发,拿出第一个可以演示版本,并且支持以后加屏幕支持 AI画图。
多亏以前有工作基础,评估了下大概三天能完成。在ChatGpt结伴编程的协助下,顺利收工,记录如下:
第一天:项目环境搭建与基础功能实现**
准备工作:
硬件环境准备:ESP32-S3开发板、麦克风阵列模块。
软件环境准备:ESP-IDF开发环境、微信小程序开发工具、ChatGPT API接口
功能实现:
微信小程序/APP配网功能:
使用蓝牙配网,用户通过小程序或APP将设备连接到Wi-Fi网络。
确保配网过程稳定、易用,并通过小程序提供简洁的用户界面。
唤醒词选择与硬件AEC降噪支持:
实现用户可以在小程序或APP中选择唤醒词。
集成ESP32-S3的硬件AEC降噪功能,确保在嘈杂环境下仍能有效识别唤醒词。
以下是蓝牙配网的协议设计和实现代码的详细说明,基于ESP32-S3和微信小程序或APP的蓝牙连接。我们将首先描述协议设计,然后提供实现代码。
1. 蓝牙配网协议设计
协议概述
该协议旨在通过蓝牙连接,将Wi-Fi配置信息(如SSID和密码)从微信小程序或APP传输到ESP32-S3设备。ESP32-S3设备使用接收到的配置信息连接到指定的Wi-Fi网络,并将连接结果返回给小程序或APP。
步骤流程
设备扫描与连接:
用户通过微信小程序或APP扫描ESP32-S3设备的蓝牙信号,并选择连接。
设备认证与握手:
ESP32-S3设备与小程序/APP之间进行简单的握手确认,以确保数据传输的安全性和可靠性。
传输Wi-Fi配置信息:
小程序/APP将Wi-Fi的SSID和密码通过蓝牙传输到ESP32-S3设备。
Wi-Fi连接:
ESP32-S3设备使用接收到的SSID和密码尝试连接到Wi-Fi网络。
返回连接结果:
ESP32-S3设备将Wi-Fi连接的结果(成功或失败)通过蓝牙发送回小程序/APP。
完成配网:
配网完成后,小程序/APP提示用户连接结果,ESP32-S3设备进入正常工作状态。
数据包格式
握手数据包:
方向:小程序/APP → ESP32-S3
内容:握手请求,可能包括设备ID或随机生成的认证码。
Wi-Fi信息数据包:
方向:小程序/APP → ESP32-S3
内容:SSID和密码,经过加密处理。
格式:
{
"ssid": "<WiFi SSID>",
"password": "<WiFi Password>"
}
Wi-Fi连接结果数据包:
- 方向:ESP32-S3 → 小程序/APP
- 内容:连接结果状态码(0表示成功,1表示失败)和可选的错误信息
2. ESP32-S3 蓝牙配网实现代码
以下代码基于ESP-IDF开发框架,展示如何实现蓝牙配网功能。
a. ESP32-S3 蓝牙服务器代码
#include <stdio.h>
#include <string.h>
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_device.h"
#include "esp_log.h"
#include "esp_gatt_common_api.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// BLE UUIDs
#define GATTS_SERVICE_UUID 0x00FF
#define GATTS_CHAR_UUID_WIFI 0xFF01
#define GATTS_CHAR_UUID_RESULT 0xFF02
#define DEVICE_NAME "ESP32S3_BLE_CONFIG"
#define CHAR_VAL_LEN_MAX 0x40
static uint8_t adv_config_done = 0;
#define adv_config_flag (1 << 0)
#define scan_rsp_config_flag (1 << 1)
static esp_gatt_char_prop_t a_property = 0;
static esp_attr_value_t gatts_char1_val = {
.attr_max_len = CHAR_VAL_LEN_MAX,
.attr_len = sizeof(char1_str),
.attr_value = char1_str,
};
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
static struct gatts_profile_inst {
esp_gatts_cb_t gatts_cb;
uint16_t gatts_if;
uint16_t app_id;
uint16_t conn_id;
uint16_t service_handle;
esp_gatt_srvc_id_t service_id;
uint16_t char_handle;
esp_bt_uuid_t char_uuid;
esp_gatt_perm_t perm;
esp_gatt_char_prop_t property;
uint16_t descr_handle;
esp_bt_uuid_t descr_uuid;
} gatts_profile = {
.gatts_cb = gatts_profile_event_handler,
.gatts_if = ESP_GATT_IF_NONE,
};
static char wifi_ssid[32];
static char wifi_password[64];
void wifi_init_sta(char *ssid, char *password) {
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = "",
.password = "",
},
};
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
strncpy((char *)wifi_config.sta.password, password, sizeof(wifi_config.sta.password));
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());
esp_wifi_connect();
}
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_REG_EVT:
esp_ble_gap_set_device_name(DEVICE_NAME);
esp_ble_gap_config_adv_data(&adv_data);
break;
case ESP_GATTS_WRITE_EVT:
if (param->write.handle == gatts_profile.char_handle) {
// 处理收到的Wi-Fi配置信息
memcpy(wifi_ssid, param->write.value, sizeof(wifi_ssid));
memcpy(wifi_password, param->write.value + sizeof(wifi_ssid), sizeof(wifi_password));
// 尝试连接Wi-Fi
wifi_init_sta(wifi_ssid, wifi_password);
// 返回连接结果
esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gatts_profile.char_handle, 1, (uint8_t *)"OK", false);
}
break;
default:
break;
}
}
void app_main(void) {
esp_err_t ret;
// 初始化NVS
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_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(TAG, "%s 初始化控制器失败: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(TAG, "%s 启用控制器失败: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(TAG, "%s 初始化蓝牙栈失败: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(TAG, "%s 启用蓝牙栈失败: %s\n", __func__, esp_err_to_name(ret));
return;
}
// 注册GATT服务
esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_app_register(PROFILE_A_APP_ID);
}
3. 微信小程序/APP 实现代码概述
微信小程序或APP的代码将负责扫描蓝牙设备、连接设备、传输Wi-Fi信息,并接收配置结果。这部分代码会基于微信小程序的蓝牙API进行开发。
// 扫描蓝牙设备
wx.startBluetoothDevicesDiscovery({
success(res) {
console.log('开始扫描蓝牙设备', res);
}
});
// 连接蓝牙设备
wx.createBLEConnection({
deviceId: 'DEVICE_ID',
success(res) {
console.log('连接蓝牙设备成功', res);
}
});
// 发送Wi-Fi配置信息
function sendWifiConfig(deviceId, serviceId, characteristicId, ssid, password) {
const buffer = new ArrayBuffer(ssid.length + password.length);
const dataView = new DataView(buffer);
for (let i = 0; i < ssid.length; i++) {
dataView.setUint8(i, ssid.charCodeAt(i));
}
for (let i = 0; i < password.length; i++) {
dataView.setUint8(ssid.length + i, password.charCodeAt(i));
}
wx.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: buffer,
success(res) {
console.log('Wi-Fi配置信息发送成功', res);
}
});
}
// 监听Wi-Fi连接结果
wx.onBLECharacteristicValueChange(function (characteristic) {
const result = String.fromCharCode.apply(null, new Uint8Array(characteristic.value));
console.log('Wi-Fi连接结果', result);
});
总结
以上是基于ESP32-S3的蓝牙配网协议设计和实现代码。这个方案通过微信小程序或APP来传输Wi-Fi配置信息,使得设备能够快速联网并进入正常工作状态。通过蓝牙连接,用户可以方便地通过移动设备配置设备网络,简化了传统的网络配置流程,提高了用户体验。
选择Swoole的大模型聚合平台进入接入,最后效果如视频:
基于ESP32-S3,支持离线语音唤醒,支持在线AI智能聊天,支持国内外三十多种大模型及私有部署的大模型,支持Cozylife APP蓝牙配网及模型选择
CozyLife AI大模型对话机器人方案