Skip to content

Commit

Permalink
修复第一次对话后不能被再次唤醒问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzongming committed Jul 21, 2024
1 parent f3c07bd commit a1be28f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 42 deletions.
33 changes: 17 additions & 16 deletions client/esp-ai/examples/client/client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,38 @@ ESP_AI_wifi_config wifi_config = { "oldwang", "oldwang520" };
// [必 填] 服务配置: { 服务IP, 服务端口 }
ESP_AI_server_config server_config = { "192.168.1.5", 8080 };
// [必 填] 离线唤醒方案:{ 方案, 识别阈值 }, "edge_impulse" | "diy",为 "diy" 时可调用 esp_ai.wakeUp() 方法进行唤醒
ESP_AI_wake_up_config wake_up_config = { "edge_impulse", 0.7 };
ESP_AI_wake_up_config wake_up_config = { "edge_impulse", 0.7 };

// [可留空] 麦克风引脚配置:{ bck_io_num, ws_io_num, data_in_num }
ESP_AI_i2s_config_mic i2s_config_mic = {4, 5, 6};
ESP_AI_i2s_config_mic i2s_config_mic = { 4, 5, 6 };
// [可留空] 扬声器引脚配置:{ bck_io_num, ws_io_num, data_in_num, 采样率 }
ESP_AI_i2s_config_speaker i2s_config_speaker = {16, 17, 15, 16000};
ESP_AI_i2s_config_speaker i2s_config_speaker = { 16, 17, 15, 16000 };
// [可留空] 音量调节配置:{ 输入引脚,输入最大值(1024|4096),默认音量(0-1) }
ESP_AI_volume_config volume_config = { 34, 4096, 0.5 };

// 收到指令后的回调,比如开灯、关灯,由服务端配置。
void on_command(char command_id, char data) {
Serial.print("收到指令");
Serial.println(command_id);
Serial.println(data);
// 收到指令后的回调,比如开灯、关灯,由服务端配置。
void on_command(String command_id, String data) {
Serial.printf("\n收到指令:%s -- %s\n", command_id, data);

// 控制小灯演示
// if (command_id == "device_open_001") {
// Serial.println("开灯");
// digitalWrite(led_pin, HIGH);
// }
// if (command_id == "device_close_001") {
// Serial.println("关灯");
// digitalWrite(led_pin, LOW);
// }
}

void setup() {
Serial.begin(115200);
// 开始运行 ESP-AI
esp_ai.begin({ i2s_config_mic, i2s_config_speaker, wifi_config, server_config, wake_up_config, volume_config, debug });
// wifi 测试代码
// Serial.println(esp_ai.wifiIsConnected() ? "已连接" : "未连接");
// Serial.println(esp_ai.localIP().c_str());
// 用户指令监听
esp_ai.onEvent(on_command);
}

void loop() {
// 连接wifi后会返回 True
if (!esp_ai.wifiIsConnected()) {
return;
}
esp_ai.loop();
esp_ai.loop();
}
10 changes: 7 additions & 3 deletions client/esp-ai/src/esp-ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ void ESP_AI::loop()
DEBUG_PRINTLN(debug, volume_config.volume);
}

// DEBUG_PRINTLN(debug, start_ed);
if (ws_connected && start_ed != "1" && (strcmp(wake_up_config.wake_up_scheme, "edge_impulse") == 0))
{
// DEBUG_PRINTLN(debug, can_voice);
// buffer 准备好后就进行推理
if (inference.buf_ready != 0 && can_voice == "1")
{
Expand Down Expand Up @@ -332,7 +334,7 @@ void ESP_AI::loop()

// 开始录音
digitalWrite(LED_BUILTIN, HIGH);
start_ed = "1";
start_ed = "1";

JSONVar data;
data["type"] = "start";
Expand All @@ -352,8 +354,10 @@ void ESP_AI::loop()
is_send_server_audio_over = "1";
// 恢复可以录音的状态
can_voice = "1";
// if(tts_task_id === "")
// start_ed = "0";
// 告诉服务端播放完毕
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTIN, LOW);

JSONVar data;
data["type"] = "client_out_audio_over";
Expand Down Expand Up @@ -414,7 +418,7 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
JSONVar data_1;
data_1["type"] = "play_audio_ws_conntceed";
String sendData = JSON.stringify(data_1);
webSocket.sendTXT(sendData);
webSocket.sendTXT(sendData);
break;
}
case WStype_TEXT:
Expand Down
57 changes: 43 additions & 14 deletions example/example.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include <esp-ai.h>
#include <string.h>

ESP_AI esp_ai;
// [必 填] 是否调试模式, 会输出更多信息
bool debug = true;
// [必 填] wifi 配置: { wifi 账号, wifi 密码 } 注意:要用双引号!
ESP_AI_wifi_config wifi_config = { "oldwang", "oldwang520" };
// [必 填] 服务配置: { 服务IP, 服务端口 }
ESP_AI_server_config server_config = { "192.168.1.5", 8080 };
ESP_AI_server_config server_config = { "192.168.3.3", 8080 };
// [必 填] 离线唤醒方案:{ 方案, 识别阈值 }, "edge_impulse" | "diy",为 "diy" 时可调用 esp_ai.wakeUp() 方法进行唤醒
ESP_AI_wake_up_config wake_up_config = { "edge_impulse", 0.7 };
// ESP_AI_wake_up_config wake_up_config = { "diy", 0.7 };

// [可留空] 麦克风引脚配置:{ bck_io_num, ws_io_num, data_in_num }
ESP_AI_i2s_config_mic i2s_config_mic = { 4, 5, 6 };
Expand All @@ -17,7 +19,7 @@ ESP_AI_i2s_config_speaker i2s_config_speaker = { 16, 17, 15, 16000 };
// [可留空] 音量调节配置:{ 输入引脚,输入最大值(1024|4096),默认音量(0-1) }
ESP_AI_volume_config volume_config = { 34, 4096, 0.5 };

// 收到指令后的回调,比如开灯、关灯,由服务端配置。
// 收到指令后的回调,比如开灯、关灯,由服务端配置。
void on_command(String command_id, String data) {
Serial.printf("\n收到指令:%s -- %s\n", command_id, data);

Expand All @@ -33,7 +35,8 @@ void on_command(String command_id, String data) {
}

void setup() {
Serial.begin(115200);
// Serial.begin(115200);
Serial.begin(9600);
// 开始运行 ESP-AI
esp_ai.begin({ i2s_config_mic, i2s_config_speaker, wifi_config, server_config, wake_up_config, volume_config, debug });
// 用户指令监听
Expand All @@ -42,24 +45,50 @@ void setup() {

void loop() {
esp_ai.loop();
// 连接wifi后会返回 True
if (!esp_ai.wifiIsConnected()) {
return;
}


// Serial.println("收集");
// // 第三方模快唤醒(如搭配 天问 使用)
// char tw[20] = "";
// char str[] = "";
// int i = 0;
// while (Serial.available()) // 当发现缓存中有数据时,将数据送至字符数组a中
// {
// tw[i] = Serial.read();
// // Serial.println(tw[i]);
// while (Serial.available()) {
// str[i] = Serial.read();
// Serial.println(str[i]);
// i++;
// delay(3);
// }
// if(tw){
// Serial.println(tw);

// if (strlen(str) >= 1) {
// Serial.println("内容");
// Serial.println(str);
// // esp_ai.wakeUp()
// int openIndex = find_string(str, "hello");
// if(openIndex != -1){
// esp_ai.wakeUp();
// }
// // int closeIndex = find_string(a, "close_desklamp");
// // // Serial.println(openIndex);
// }
}




int find_string(char str[], char substr[]){
int i, j, k;
int len1 = strlen(str);
int len2 = strlen(substr);

for(i = 0; i < len1 - len2 + 1; i++){
k = i;
j = 0;
while(str[k] == substr[j] && j < len2){
k++;
j++;
}
if(j == len2){
return i;
}
}
return -1;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esp-ai",
"version": "1.13.2",
"version": "1.13.3",
"description": "Provide a complete set of AI dialogue solutions for your development board, including but not limited to the IAT+LLM+TTS integration solution for the ESP32 series development board. | 为你的开发板提供全套的AI对话方案,包括但不限于 `ESP32` 系列开发板的 `IAT+LLM+TTS` 集成方案。",
"repository": "https://github.com/wangzongming/esp-ai",
"main": "src/index.js",
Expand Down
6 changes: 2 additions & 4 deletions src/functions/iat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ async function cb({ device_id, text }) {
...G_devices.get(device_id),
first_session: true,
})
ws_client && ws_client.send("session_end");
// ws_client && ws_client.send("iat_end");
ws_client && ws_client.send("session_end");
devLog && console.log('\n\n === 会话结束 ===\n\n')
break;
default:
Expand Down Expand Up @@ -71,8 +70,7 @@ async function cb({ device_id, text }) {
...G_devices.get(device_id),
first_session: true,
})
ws_client && ws_client.send("session_end");
// ws_client && ws_client.send("iat_end");
ws_client && ws_client.send("session_end");
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/functions/init_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function init_server() {
// console.log(JSON.parse(data));
switch (type) {
case "start":

// console.log(first_session, iat_server_connected, client_out_audio_ing)
// ...
if (iat_server_connected || client_out_audio_ing) {
// devLog && console.log("--- IAT 识别途中收到重新输入音频");
Expand Down Expand Up @@ -128,10 +128,10 @@ function init_server() {
pauseInputAudio: true
});
} else {
await play_temp("du.pcm", ws);
add_audio_out_over_queue(() => {
add_audio_out_over_queue("warning_tone", () => {
start_iat();
})
await play_temp("du.pcm", ws);
}

// ============= LLM 测试 =============
Expand Down Expand Up @@ -167,7 +167,7 @@ function init_server() {
})
break;
}
} else {
} else {
// 采集的音频数据
if (started && data && data.length && send_pcm && iat_server_connected) {
// 发送数据
Expand Down

0 comments on commit a1be28f

Please sign in to comment.