1. 前言
圍繞ESP32S3 Sense接入語音識別+MiniMax模型對話展開,首先串口輸入“1”字符,隨后麥克風(fēng)采集2s聲音數(shù)據(jù),對接百度在線語音識別,將返回文本結(jié)果丟入MiniMax模型,進(jìn)而返回第二次結(jié)果文本,實(shí)現(xiàn)語言對話文本效果。以上一共有兩次調(diào)用,后期只需加入tts模塊就可完整對話。
講解視頻:
ESP32S3 Sense接入語音識別+MiniMax模型對話
目前這是我使用的ESP32S3官方硬件??????(小小的身材有大大的力量)只需要35元加攝像頭麥克風(fēng)79元,后期我會整理相關(guān)專欄進(jìn)行Arduino系統(tǒng)學(xué)習(xí)??????。有需要可以購買xiao開發(fā)板??????,SeeedXIAO ESP32S3 Sense硬件購買地址:https://s.click.taobao.com/lekazrt
1.1 語音接入
百度在線語音接入教程:
【ESP32S3 Sense接入百度在線語音識別】
使用Seeed XIAO ESP32S3 Sense開發(fā)板接入百度智能云實(shí)現(xiàn)在線語音識別。自帶麥克風(fēng)模塊用做語音輸入,通過串口發(fā)送字符“1”來控制數(shù)據(jù)的采集和上傳。
1.2 大模型接入
國產(chǎn)大模型接入分享如下:
【ESP32接入國產(chǎn)大模型之MiniMax】
【ESP32接入語言大模型之智譜清言】
【ESP32接入國產(chǎn)大模型之文心一言】
【ESP32接入語言大模型之通義千問】
下面是不標(biāo)準(zhǔn)測評,推薦使用MiniMax大模型,參考而已
MM智能助理是一款由MiniMax自研的,沒有調(diào)用其他產(chǎn)品的接口的大型語言模型。MiniMax是一家中國科技公司,一直致力于進(jìn)行大模型相關(guān)的研究。
模型 | 響應(yīng)時(shí)間 | 內(nèi)容質(zhì)量 | 免費(fèi)token次數(shù) | 地址 |
---|---|---|---|---|
MiniMax | 3s | 8分 | 500萬 | https://www.minimaxi.com/ |
智譜清言 | 7s | 8分 | 300萬 | https://open.bigmodel.cn/ |
文心一言 | 10s | 9分 | 500萬 | https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu |
通義千問 | 8s | 8分 | 800萬 | https://tongyi.aliyun.com/qianwen/ |
這一次還是采用Arduino編程就會輕松許多開發(fā)。這樣就可以把stt+chat大模型裝進(jìn)口袋啦??????,接下來就只差tts播報(bào)啦!
2. 先決條件
在繼續(xù)此項(xiàng)目之前,請確保檢查以下先決條件。
我們將使用 Arduino IDE 對 ESP32/ESP8266 開發(fā)板進(jìn)行編程,因此在繼續(xù)本教程之前,請確保已在 Arduino IDE 中安裝這些開發(fā)板。
2.1 環(huán)境配置
- Arduino IDE:下載并安裝 Arduino IDE;
- ESP32 開發(fā)板庫:在 Arduino IDE 中添加 ESP32 支持;
參考博客:【esp32c3配置arduino IDE教程】
為安裝過程留出一些時(shí)間,具體時(shí)間可能因您的互聯(lián)網(wǎng)連接而異。
2.2 所需零件
要學(xué)習(xí)本教程,您需要1個 ESP32 開發(fā)板+馬克風(fēng)或者ESP32 Sense,建議使用后者,筆者發(fā)現(xiàn)同樣的代碼后者可以輕松調(diào)用,ESP32不行(可能板子壞了)。
3. 核心代碼
提供兩種硬件測試
- ESP32S3 Sense
- ESP32 + max9814麥克風(fēng)模塊用做語音輸入,一個按鍵來控制數(shù)據(jù)的采集和上傳
3.1 ESP32S3 Sense
ESP32S3 Sense自帶麥克風(fēng)直接燒錄
Arduino代碼如下
#include <Arduino.h>
#include "base64.h"
#include <WiFi.h>
#include "HTTPClient.h"
#include "cJSON.h"
#include <I2S.h>
#include <ArduinoJson.h>
#define data_len 16000
// #define key 4 //端口0
// #define ADC 2 //端口39
// #define led 15 //端口2
HTTPClient http_client;
// 1. Replace with your network credentials
const char* ssid = "J09 502";
const char* password = "qwertyuiop111";
// 2. Check your Aduio port
const int buttonPin = 1; // the number of the pushbutton pin
const int ledPin = 21; // the number of the LED pin
hw_timer_t* timer = NULL;
uint16_t adc_data[data_len]; //16000個數(shù)據(jù),8K采樣率,即2秒,錄音時(shí)間為2秒,想要實(shí)現(xiàn)更長時(shí)間的語音識別,就要改這個數(shù)組大小
//和下面data_json數(shù)組的大小,改大一些。
uint8_t adc_start_flag = 0; //開始標(biāo)志
uint8_t adc_complete_flag = 0; //完成標(biāo)志
char data_json[45000]; //用于儲存json格式的數(shù)據(jù),大一點(diǎn),JSON編碼后數(shù)據(jù)字節(jié)數(shù)變成原來的4/3,所以得計(jì)算好,避免出現(xiàn)越界
// 3. Replace with your MiniMax API key
const char* apiKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJHcm91cE5hbWUiOiIyMzQ1dm9yIiwiVXNlck5hbWUiOiIyMzQ1dm9yIiwiQWNjb3VudCI6IiIsIlN1YmjE3NTk0ODIxODAxMDAxNzAyMDgiLCJQI6IjE3NTk0ODIxODAwOTU5NzU5MDQiLCJQYWdlTmFtZSI6IiIsIk1haWwiOiIiLCJDcmVhdGVUaW1lIjoiMjAyNC0wMy0xNiAxMzoyNDoxOCIsImlzcyI6Im1pbmltYXgifQ.WlEj8Nk0j_WOMXZE9SbIC8sHpwJ6R6Pi8Spl5mahJsW3-Jsz7Ev53sGGz3v__Bd5dDkt7o9-Y8BOW0WZq2ImaN7Rof7YNtYnYnvPNDyGx23_xRqq5co9P5UkC3ciYEcIch2SUZ5QPkXR-sMUPzhdowSYvfdu1N25kdKJ8GE_63NfCnsdDVt8mv0wQSSweJK0yf_C8a8ADdB1uF4vg_WKMDjHlvzERsoNZgX6FYtr-bee85rIyu4U-OrbUvEpR1FLPXa7lTlx65QvhVIYGbIKde7ERIT_7QLOQoVFvPz0gX-H6V7UlmSRgRy4LK_R9mvV5TqCy3v90WK_AFuwEhPXcg";
HTTPClient http;
String token_key = String("Bearer ") + apiKey;
// Send request to MiniMax API
String inputText = "你好,minimax!";
String apiUrl = "https://api.minimax.chat/v1/text/chatcompletion_v2";
int httpResponseCode;
String response,question,answer;
DynamicJsonDocument jsonDoc(1024);
uint32_t num = 0;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
if (adc_start_flag == 1) {
//Serial.println("");
// adc_data[num] = analogRead(ADC);
adc_data[num] = I2S.read();
num++;
if (num >= data_len) {
adc_complete_flag = 1;
adc_start_flag = 0;
num = 0;
//Serial.println(Complete_flag);
}
}
portEXIT_CRITICAL_ISR(&timerMux);
}
String getGPTAnswer(String inputText) {
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", token_key);
String payload = "{\"model\":\"abab5.5s-chat\",\"messages\":[{\"role\": \"system\",\"content\": \"你是鵬鵬的生活助手機(jī)器人,要求下面的回答嚴(yán)格控制在256字符以內(nèi)。\"},{\"role\": \"user\",\"content\": \"" + inputText + "\"}]}";
httpResponseCode = http.POST(payload);
if (httpResponseCode == 200) {
response = http.getString();
http.end();
Serial.println(response);
// Parse JSON response
deserializeJson(jsonDoc, response);
String outputText = jsonDoc["choices"][0]["message"]["content"];
return outputText;
// Serial.println(outputText);
} else {
http.end();
Serial.printf("Error %i \n", httpResponseCode);
return "<error>";
}
}
void setup() {
//Serial.begin(921600);
Serial.begin(115200);
// pinMode(ADC, ANALOG);
// pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// start I2S at 16 kHz with 16-bits per sample
I2S.setAllPins(-1, 42, 41, -1, -1);
if (!I2S.begin(PDM_MONO_MODE, 16000, 16)) {
Serial.println("Failed to initialize I2S!");
while (1)
; // do nothing
}
uint8_t count = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
count++;
if (count >= 75) {
Serial.printf("\r\n-- wifi connect fail! --");
break;
}
vTaskDelay(200);
}
Serial.printf("\r\n-- wifi connect success! --\r\n");
Serial.println(WiFi.localIP());
http.setTimeout(10000);
// gain_token();
timer = timerBegin(0, 80, true); // 80M的時(shí)鐘 80分頻 1M
timerAlarmWrite(timer, 125, true); // 1M 計(jì)125個數(shù)進(jìn)中斷 8K
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmEnable(timer);
timerStop(timer); //先暫停
}
uint32_t time1, time2;
void loop() {
if (Serial.available() > 0) //按鍵按下
{
if (Serial.read() == '1') {
Serial.printf("Start recognition\r\n\r\n");
digitalWrite(ledPin, HIGH);
adc_start_flag = 1;
timerStart(timer);
// time1=micros();
while (!adc_complete_flag) //等待采集完成
{
ets_delay_us(10);
}
// time2=micros()-time1;
timerStop(timer);
adc_complete_flag = 0; //清標(biāo)志
digitalWrite(ledPin, LOW);
memset(data_json, '\0', strlen(data_json)); //將數(shù)組清空
strcat(data_json, "{");
strcat(data_json, "\"format\":\"pcm\",");
strcat(data_json, "\"rate\":16000,"); //采樣率 如果采樣率改變了,記得修改該值,只有16000、8000兩個固定采樣率
strcat(data_json, "\"dev_pid\":1537,"); //中文普通話
strcat(data_json, "\"channel\":1,"); //單聲道
strcat(data_json, "\"cuid\":\"666666\","); //識別碼 隨便打幾個字符,但最好唯一
strcat(data_json, "\"token\":\"24.8f6143793af76e02f5e191.2592000.1713789066.282335-57722200\","); //token 這里需要修改成自己申請到的token
strcat(data_json, "\"len\":32000,"); //數(shù)據(jù)長度 如果傳輸?shù)臄?shù)據(jù)長度改變了,記得修改該值,該值是ADC采集的數(shù)據(jù)字節(jié)數(shù),不是base64編碼后的長度
strcat(data_json, "\"speech\":\"");
strcat(data_json, base64::encode((uint8_t*)adc_data, sizeof(adc_data)).c_str()); //base64編碼數(shù)據(jù)
strcat(data_json, "\"");
strcat(data_json, "}");
// Serial.println(data_json);
int httpCode;
http_client.setTimeout(5000);
http_client.begin("http://vop.baidu.com/server_api"); //https://vop.baidu.com/pro_api
http_client.addHeader("Content-Type", "application/json");
httpCode = http_client.POST(data_json);
if (httpCode == 200) {
if (httpCode == HTTP_CODE_OK) {
response = http_client.getString();
http_client.end();
Serial.println(response);
// Parse JSON response
// DynamicJsonDocument jsonDoc(512);
deserializeJson(jsonDoc, response);
String question = jsonDoc["result"][0];
// 訪問"result"數(shù)組,并獲取其第一個元
// 輸出結(jié)果
Serial.println("\n Input:"+question);
answer = getGPTAnswer(question);
Serial.println("Answer: " + answer);
Serial.println("Enter a prompt:");
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http_client.errorToString(httpCode).c_str());
}
}
// while (!digitalRead(buttonPin))
// ;
Serial.printf("Recognition complete\r\n");
}
}
vTaskDelay(1);
}
用于實(shí)現(xiàn)一個通過 I2S 接口采集音頻信號并將其發(fā)送到百度語音識別 API 進(jìn)行語音識別,然后將識別出的文本通過 MiniMax API 獲取 AI 回答的功能。以下是代碼的主要結(jié)構(gòu)和功能說明:
-
引入必要的庫文件,包括 Arduino.h、base64.h、WiFi.h、HTTPClient.h、cJSON.h、I2S.h 和 ArduinoJson.h,這些庫分別提供了基本的 Arduino 功能、Base64 編解碼、Wi-Fi 連接、HTTP 客戶端操作、JSON 數(shù)據(jù)處理和 I2S 音頻接口驅(qū)動。
-
定義了一些全局變量,如 Wi-Fi 的 SSID 和密碼,以及與音頻采集和處理相關(guān)的變量,如 ADC 數(shù)據(jù)緩沖區(qū)、錄音標(biāo)志位、完成標(biāo)志位、JSON 格式數(shù)據(jù)緩沖區(qū),還有 MiniMax API 的密鑰(apiKey)。
修改Wi-Fi 的 SSID 和密碼
// 1. Replace with your network credentials
const char* ssid = "J09 502";
const char* password = "qwertyuiop111";
修改MiniMax API 的密鑰(apiKey)
// 3. Replace with your MiniMax API key
const char* apiKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJHcm91cE5hbWUiOiIyMzQ1dm9yIiwiVXNlck5hbWUiOiIyMzQ1dm9yIiwiQWNjb3VudCI6IiIsIlN1YmjE3NTk0ODIxODAxMDAxNzAyMDgiLCJQI6IjE3NTk0ODIxODAwOTU5NzU5MDQiLCJQYWdlTmFtZSI6IiIsIk1haWwiOiIiLCJDcmVhdGVUaW1lIjoiMjAyNC0wMy0xNiAxMzoyNDoxOCIsImlzcyI6Im1pbmltYXgifQ.WlEj8Nk0j_WOMXZE9SbIC8sHpwJ6R6Pi8Spl5mahJsW3-Jsz7Ev53sGGz3v__Bd5dDkt7o9-Y8BOW0WZq2ImaN7Rof7YNtYnYnvPNDyGx23_xRqq5co9P5UkC3ciYEcIch2SUZ5QPkXR-sMUPzhdowSYvfdu1N25kdKJ8GE_63NfCnsdDVt8mv0wQSSweJK0yf_C8a8ADdB1uF4vg_WKMDjHlvzERsoNZgX6FYtr-bee85rIyu4U-OrbUvEpR1FLPXa7lTlx65QvhVIYGbIKde7ERIT_7QLOQoVFvPz0gX-H6V7UlmSRgRy4LK_R9mvV5TqCy3v90WK_AFuwEhPXcg";
修改百度api
strcat(data_json, "\"token\":\"24.8f6143793af76e02f5e191.2592000.1713789066.282335-57722200\","); //token 這里需要修改成自己申請到的token
-
定義了一個 HTTPClient 實(shí)例 http_client,用于向 API 發(fā)送請求。
-
函數(shù) getGPTAnswer() 負(fù)責(zé)調(diào)用 MiniMax API,并傳入用戶輸入文本以獲取 AI 的回答。
-
onTimer() 函數(shù)是一個中斷服務(wù)程序,每當(dāng)定時(shí)器觸發(fā)時(shí)會讀取 I2S 接口上的音頻數(shù)據(jù)并存入緩沖區(qū) adc_data 中。當(dāng)緩沖區(qū)滿或者錄音結(jié)束時(shí),會設(shè)置完成標(biāo)志。
-
setup() 函數(shù)負(fù)責(zé)初始化串口通信、配置 I2S 接口、連接 Wi-Fi,并設(shè)置定時(shí)器和中斷。
-
loop() 函數(shù)是 Arduino 主循環(huán)函數(shù),在循環(huán)中檢查是否有按鍵輸入(這里未實(shí)際使用)。如果有特定輸入,開始音頻采集過程,并在采集完成后將音頻數(shù)據(jù)轉(zhuǎn)換為 Base64 編碼,構(gòu)建一個 JSON 請求體,然后通過 HTTP POST 方式發(fā)送到百度語音識別 API。收到識別結(jié)果后,調(diào)用 getGPTAnswer() 函數(shù)獲取 AI 回答,并打印在控制臺上。
3.2 ESP32 + max9814
ESP32 + max9814麥克風(fēng)模塊用做語音輸入,一個按鍵來控制數(shù)據(jù)的采集和上傳
注意端口:
#define key 0 //按鍵端口0
#define ADC 32 //max9814麥克風(fēng)模塊端口39
#define led 2 //LED端口2
stt_chat_max.ino
#include <Arduino.h>
#include "base64.h"
#include <WiFi.h> //#include "WiFi.h"
#include "HTTPClient.h"
#include "cJSON.h"
// #include <I2S.h>
#include <ArduinoJson.h>
#define data_len 16000
#define key 0 //端口0
#define ADC 32 //端口39
#define led 2 //端口2
HTTPClient http_client;
// 1. Replace with your network credentials
const char* ssid = "J09 502";
const char* password = "qwertyuiop111";
// 2. Check your Aduio port
// const int buttonPin = 1; // the number of the pushbutton pin
// const int ledPin = 21; // the number of the LED pin
hw_timer_t* timer = NULL;
uint16_t adc_data[data_len]; //16000個數(shù)據(jù),8K采樣率,即2秒,錄音時(shí)間為2秒,想要實(shí)現(xiàn)更長時(shí)間的語音識別,就要改這個數(shù)組大小
//和下面data_json數(shù)組的大小,改大一些。
uint8_t adc_start_flag = 0; //開始標(biāo)志
uint8_t adc_complete_flag = 0; //完成標(biāo)志
char data_json[45000]; //用于儲存json格式的數(shù)據(jù),大一點(diǎn),JSON編碼后數(shù)據(jù)字節(jié)數(shù)變成原來的4/3,所以得計(jì)算好,避免出現(xiàn)越界
// 3. Replace with your MiniMax API key
const char* apiKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJHcm91iOiIyMzQ1dm9yIiwiQWNjb3VudCI6IiIsIlN1YmplY3RJRCI6IjE3NTk0ODIxODAxMDAxNzAyMDgiLCJQaG9uZSI6IjE1MDcyNjQxNTYxIiwiR3JvdXBJRCI6IjE3NTk0ODIxODAwOTU5NzU5MDQiLCJQYWdlTmFtZSI6IiIsIk1haWwiOiIiLCJDcmVhdGVUaW1lIjoiMjAyNC0wMy0xNiAxMzoyNDoxOCIsImlzcyI6Im1pbmltYXgifQ.WlEj8Nk0j_WOMXZE9SbIC8sHpwJ6R6Pi8Spl5mahJsW3-Jsz7Ev53sGGz3v__Bd5dDkt7o9-Y8BOW0WZq2ImaN7Rof7YNtYnYnvPNDyGx23_xRqq5co9P5UkC3ciYEcIch2SUZ5QPkXR-sMUPzhdowSYvfdu1N25kdKJ8GE_63NfCnsdDVt8mv0wQSSweJK0yf_C8a8ADdB1uF4vg_WKMDjHlvzERsoNZgX6FYtr-bee85rIyu4U-OrbUvEpR1FLPXa7lTlx65QvhVIYGbIKde7ERIT_7QLOQoVFvPz0gX-H6V7UlmSRgRy4LK_R9mvV5TqCy3v90WK_AFuwEhPXcg";
HTTPClient http;
String token_key = String("Bearer ") + apiKey;
// Send request to MiniMax API
String inputText = "你好,minimax!";
String apiUrl = "https://api.minimax.chat/v1/text/chatcompletion_v2";
int httpResponseCode;
String response, question, answer;
DynamicJsonDocument jsonDoc(1024);
uint32_t num = 0;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
if (adc_start_flag == 1) {
//Serial.println("");
adc_data[num] = analogRead(ADC);
// adc_data[num] = I2S.read();
num++;
if (num >= data_len) {
adc_complete_flag = 1;
adc_start_flag = 0;
num = 0;
//Serial.println(Complete_flag);
}
}
portEXIT_CRITICAL_ISR(&timerMux);
}
String getGPTAnswer(String inputText) {
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", token_key);
String payload = "{\"model\":\"abab5.5s-chat\",\"messages\":[{\"role\": \"system\",\"content\": \"你是鵬鵬的生活助手機(jī)器人,要求下面的回答嚴(yán)格控制在256字符以內(nèi)。\"},{\"role\": \"user\",\"content\": \"" + inputText + "\"}]}";
httpResponseCode = http.POST(payload);
if (httpResponseCode == 200) {
response = http.getString();
http.end();
Serial.println(response);
// Parse JSON response
deserializeJson(jsonDoc, response);
String outputText = jsonDoc["choices"][0]["message"]["content"];
return outputText;
// Serial.println(outputText);
} else {
http.end();
Serial.printf("Error %i \n", httpResponseCode);
return "<error>";
}
}
void setup() {
//Serial.begin(921600);
Serial.begin(115200);
pinMode(ADC, ANALOG);
pinMode(key, INPUT_PULLUP);
pinMode(led, OUTPUT);
// pinMode(ADC, ANALOG);
// pinMode(buttonPin, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// start I2S at 16 kHz with 16-bits per sample
// I2S.setAllPins(-1, 42, 41, -1, -1);
// if (!I2S.begin(PDM_MONO_MODE, 16000, 16)) {
// Serial.println("Failed to initialize I2S!");
// while (1)
// ; // do nothing
// }
uint8_t count = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
count++;
if (count >= 75) {
Serial.printf("\r\n-- wifi connect fail! --");
break;
}
vTaskDelay(200);
}
Serial.printf("\r\n-- wifi connect success! --\r\n");
Serial.println(WiFi.localIP());
http.setTimeout(10000);
// gain_token();
timer = timerBegin(0, 80, true); // 80M的時(shí)鐘 80分頻 1M
timerAlarmWrite(timer, 125, true); // 1M 計(jì)125個數(shù)進(jìn)中斷 8K
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmEnable(timer);
timerStop(timer); //先暫停
}
uint32_t time1, time2;
void loop() {
if (digitalRead(key)==0) {
Serial.printf("Start recognition\r\n\r\n");
digitalWrite(led,HIGH);
adc_start_flag = 1;
timerStart(timer);
// time1=micros();
while (!adc_complete_flag) //等待采集完成
{
ets_delay_us(10);
}
// time2=micros()-time1;
timerStop(timer);
adc_complete_flag = 0; //清標(biāo)志
digitalWrite(led, LOW);
memset(data_json, '\0', strlen(data_json)); //將數(shù)組清空
strcat(data_json, "{");
strcat(data_json, "\"format\":\"pcm\",");
strcat(data_json, "\"rate\":8000,"); //采樣率 如果采樣率改變了,記得修改該值,只有16000、8000兩個固定采樣率
strcat(data_json, "\"dev_pid\":1537,"); //中文普通話
strcat(data_json, "\"channel\":1,"); //單聲道
strcat(data_json, "\"cuid\":\"666666\","); //識別碼 隨便打幾個字符,但最好唯一
strcat(data_json, "\"token\":\"24.8f6143793af76191.2592000.1713789066.282335-57722200\","); //token 這里需要修改成自己申請到的token
strcat(data_json, "\"len\":32000,"); //數(shù)據(jù)長度 如果傳輸?shù)臄?shù)據(jù)長度改變了,記得修改該值,該值是ADC采集的數(shù)據(jù)字節(jié)數(shù),不是base64編碼后的長度
strcat(data_json, "\"speech\":\"");
strcat(data_json, base64::encode((uint8_t*)adc_data, sizeof(adc_data)).c_str()); //base64編碼數(shù)據(jù)
strcat(data_json, "\"");
strcat(data_json, "}");
// Serial.println(data_json);
int httpCode;
http_client.setTimeout(5000);
http_client.begin("http://vop.baidu.com/server_api"); //https://vop.baidu.com/pro_api
http_client.addHeader("Content-Type", "application/json");
httpCode = http_client.POST(data_json);
if (httpCode == 200) {
if (httpCode == HTTP_CODE_OK) {
response = http_client.getString();
http_client.end();
Serial.println(response);
// Parse JSON response
// DynamicJsonDocument jsonDoc(512);
deserializeJson(jsonDoc, response);
String question = jsonDoc["result"][0];
// 訪問"result"數(shù)組,并獲取其第一個元
// 輸出結(jié)果
Serial.println("\n Input:" + question);
answer = getGPTAnswer(question);
Serial.println("Answer: " + answer);
Serial.println("Enter a prompt:");
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http_client.errorToString(httpCode).c_str());
}
}
// while (!digitalRead(buttonPin))
// ;
Serial.printf("Recognition complete\r\n");
}
// }
vTaskDelay(1);
}
4. 上傳驗(yàn)證
如果提示Compilation error: ArduinoJson.h: No such file or directory
直接在庫管理安裝Arduinojson
庫
打開串口監(jiān)視器,注意右下角選擇換行符,選擇115200波特率,輸入你想問的問題,他就可以回答你
4.1 對話測試
串口發(fā)送“1”,開始錄音,然后返回對話結(jié)果,以上是兩次連續(xù)對話效果??????
4.2 報(bào)錯
- 如果返回error ,大家對照列表查詢錯誤代碼,結(jié)合提示排查解決
- 如果第一次可以二次自動重啟
-
可以配置下載程序運(yùn)行在core0
5. 總結(jié)
??????現(xiàn)在,我們在本教程中,您學(xué)習(xí)了如何使用ESP32S3 Sense接入語音識別+MiniMax模型對話。??????從而實(shí)現(xiàn)對外部世界進(jìn)行感知,充分認(rèn)識這個有機(jī)與無機(jī)的環(huán)境,后期會持續(xù)分享esp32跑freertos實(shí)用案列??????科學(xué)地合理地進(jìn)行創(chuàng)作和發(fā)揮效益,然后為人類社會發(fā)展貢獻(xiàn)一點(diǎn)微薄之力。??????文章來源:http://www.zghlxwxcb.cn/news/detail-853422.html
如果你有任何問題,可以通過下面的二維碼加入鵬鵬小分隊(duì),期待與你思維的碰撞??????文章來源地址http://www.zghlxwxcb.cn/news/detail-853422.html
到了這里,關(guān)于【ESP32S3 Sense接入語音識別+MiniMax模型對話】的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!