Skip to content

Commit

Permalink
public 2.28.19
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzongming committed Nov 15, 2024
1 parent e71a8d2 commit 070635b
Show file tree
Hide file tree
Showing 22 changed files with 516 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ example/index_copy.js

# builds
build

test-server
.vscode/
_del/

Expand Down
8 changes: 6 additions & 2 deletions client/esp-ai/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include "globals.h"

String ESP_AI_VERSION = "2.11.5";
String ESP_AI_VERSION = "2.13.6";
String start_ed = "0";
String can_voice = "1";
String is_send_server_audio_over = "1";
Expand All @@ -48,6 +48,7 @@ I2SStream i2s;
VolumeStream esp_ai_volume(i2s);
EncodedAudioStream esp_ai_dec(&esp_ai_volume, new MP3DecoderHelix()); // Decoding stream


WebServer esp_ai_server(80);

// 麦克风默认配置 { bck_io_num, ws_io_num, data_in_num }
Expand Down Expand Up @@ -286,4 +287,7 @@ void set_local_data(String field_name, String new_value)
EEPROM.commit();
// 确保数据被写入 EEPROM
delay(100);
}
}

std::vector<int> digital_read_pins;
std::vector<int> analog_read_pins;
6 changes: 5 additions & 1 deletion client/esp-ai/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ extern WebServer esp_ai_server;
extern I2SStream i2s;
extern EncodedAudioStream esp_ai_dec; // Decoding stream
extern VolumeStream esp_ai_volume;



extern String ESP_AI_VERSION;
Expand Down Expand Up @@ -246,4 +247,7 @@ typedef struct
String ext5; // 备用5
} saved_info;
String get_local_data(const String &field_name);
void set_local_data(String field_name, String new_value);
void set_local_data(String field_name, String new_value);

extern std::vector<int> digital_read_pins;
extern std::vector<int> analog_read_pins;
1 change: 1 addition & 0 deletions client/esp-ai/src/init/mic_i2s_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int ESP_AI::mic_i2s_init(uint32_t sampling_rate)
{
Serial.println("[Error] Error in initializing dma buffer with 0");
}



return int(ret);
Expand Down
2 changes: 1 addition & 1 deletion client/esp-ai/src/sdk/begin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void ESP_AI::begin(ESP_AI_CONFIG config)
wake_up_scheme = String(wake_up_config.wake_up_scheme);
if (wake_up_scheme == "pin_high" || wake_up_scheme == "pin_low")
{
pinMode(wake_up_config.pin, INPUT);
pinMode(wake_up_config.pin, INPUT_PULLDOWN);
}
}
if (wake_up_scheme == "edge_impulse")
Expand Down
38 changes: 37 additions & 1 deletion client/esp-ai/src/sdk/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ String cleanString(String input)
return output;
}

// 延迟多久发网服务器
int read_to_s_delay_time = 100;
long prev_read_to_s_delay_time = millis();

JSONVar digitalReadJSONData;
JSONVar analogReadJSONData;

void ESP_AI::loop()
{
esp_ai_server.handleClient();
Expand All @@ -55,7 +62,8 @@ void ESP_AI::loop()
if (WiFi.status() != WL_CONNECTED)
{

if (net_status == "2" && net_status != "0" && ap_connect_err != "1"){
if (net_status == "2" && net_status != "0" && ap_connect_err != "1")
{
// 内置状态处理
status_change("0");
}
Expand Down Expand Up @@ -165,4 +173,32 @@ void ESP_AI::loop()
delay(3);

}

// 上报传感器数据
long curTime = millis();
if (curTime - prev_read_to_s_delay_time > read_to_s_delay_time)
{
for (int i = 0; i < digital_read_pins.size(); i++)
{
int pin = digital_read_pins[i];
int reading = digitalRead(pin);
digitalReadJSONData["type"] = "digitalRead";
digitalReadJSONData["pin"] = pin;
digitalReadJSONData["value"] = reading;
String sendData = JSON.stringify(digitalReadJSONData);
esp_ai_webSocket.sendTXT(sendData);
}
for (int i = 0; i < analog_read_pins.size(); i++)
{
int pin = analog_read_pins[i];
int reading = analogRead(pin);
analogReadJSONData["type"] = "analogRead";
analogReadJSONData["pin"] = pin;
analogReadJSONData["value"] = reading;
String sendData = JSON.stringify(analogReadJSONData);
esp_ai_webSocket.sendTXT(sendData);
}

prev_read_to_s_delay_time = curTime;
}
}
32 changes: 32 additions & 0 deletions client/esp-ai/src/webSocketEvent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,38 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
String value = (const char *)parseRes["value"];
set_local_data(field, value);
}
else if (type == "hardware-fns")
{
int pin = (int)parseRes["pin"];
String fn_name = (const char *)parseRes["fn_name"];
String str_val = (const char *)parseRes["str_val"];
int num_val = (int)parseRes["num_val"];

if (fn_name == "pinMode")
{
str_val == "OUTPUT" && (pinMode(pin, OUTPUT), true);
str_val == "INPUT" && (pinMode(pin, INPUT), true);
str_val == "INPUT_PULLUP" && (pinMode(pin, INPUT_PULLUP), true);
str_val == "INPUT_PULLDOWN" && (pinMode(pin, INPUT_PULLDOWN), true);
}
else if (fn_name == "digitalWrite")
{
str_val == "HIGH" && (digitalWrite(pin, HIGH), true);
str_val == "LOW" && (digitalWrite(pin, LOW), true);
}
else if (fn_name == "digitalRead")
{
digital_read_pins.push_back(pin);
}
else if (fn_name == "analogWrite")
{
analogWrite(pin, num_val);
}
else if (fn_name == "analogRead")
{
analog_read_pins.push_back(pin);
}
}
}
}

Expand Down
Binary file not shown.
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": "2.21.19",
"version": "2.28.19",
"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
31 changes: 31 additions & 0 deletions src/functions/client_messages/analogRead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2024 小明IO
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Commercial use of this software requires prior written authorization from the Licensor.
* 请注意:将 ESP-AI 代码用于商业用途需要事先获得许可方的授权。
* 删除与修改版权属于侵权行为,请尊重作者版权,避免产生不必要的纠纷。
*
* @author 小明IO
* @email 1746809408@qq.com
* @github https://github.com/wangzongming/esp-ai
* @websit https://espai.fun
*/

async function fn({ device_id, value, pin }) {
const { read_pin_cbs } = G_devices.get(device_id);
read_pin_cbs.get(pin)?.(value);
}

module.exports = fn
31 changes: 31 additions & 0 deletions src/functions/client_messages/digitalRead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2024 小明IO
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Commercial use of this software requires prior written authorization from the Licensor.
* 请注意:将 ESP-AI 代码用于商业用途需要事先获得许可方的授权。
* 删除与修改版权属于侵权行为,请尊重作者版权,避免产生不必要的纠纷。
*
* @author 小明IO
* @email 1746809408@qq.com
* @github https://github.com/wangzongming/esp-ai
* @websit https://espai.fun
*/

async function fn({ device_id, value, pin }) {
const { read_pin_cbs } = G_devices.get(device_id);
read_pin_cbs.get(pin)?.(value);
}

module.exports = fn
6 changes: 5 additions & 1 deletion src/functions/client_messages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const client_out_audio_over = require("./client_out_audio_over")
const client_receive_audio_over = require("./client_receive_audio_over")
// const tts = require("./tts")
const set_wifi_config_res = require("./set_wifi_config_res")
const digitalRead = require("./digitalRead")
const analogRead = require("./analogRead")

module.exports = {
audio,
Expand All @@ -17,5 +19,7 @@ module.exports = {
client_out_audio_over,
client_receive_audio_over,
// tts,
set_wifi_config_res
set_wifi_config_res,
digitalRead,
analogRead
}
45 changes: 8 additions & 37 deletions src/functions/client_messages/play_audio_ws_conntceed.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const isOutTimeErr = require("../../utils/isOutTimeErr");
*/
async function fn({ device_id }) {
try {

const { devLog, gen_client_config } = G_config;
const { ws, client_params, client_version, error_catch, tts_buffer_chunk_queue } = G_devices.get(device_id);
const user_config = await gen_client_config({
Expand Down Expand Up @@ -93,45 +92,17 @@ async function fn({ device_id }) {
})

const TTS_FN = require(`../tts`);
const { user_config: { connected_reply } } = G_devices.get(device_id);
const { user_config: { connected_reply, intention = [] } } = G_devices.get(device_id);

ws && ws.send(JSON.stringify({ type: "stc_time", stc_time: +new Date() + "" }));

// // 缓存必要的 TTS ing...
// const reqTTS = [_user_config.f_reply, _user_config.sleep_reply, _user_config.connected_reply];
// if (user_config.intention && Array.isArray(user_config.intention)) {
// user_config.intention.forEach((item) => {
// reqTTS.push(item?.message)
// })
// }
// // 需要改为以音色为主键...
// for (const text of reqTTS) {
// const tts_cache_key = `${device_id}_${text}`;
// const cache_TTS = G_get_cahce_TTS(tts_cache_key);
// if (!cache_TTS) {
// devLog && log.t_info(`正在缓存 "${text}" TTS...`);
// const sessionIdBuffer = Buffer.from("", 'utf-8');
// G_set_cahce_TTS(tts_cache_key, sessionIdBuffer);

// let combinedBuffer = sessionIdBuffer;
// await TTS_FN(device_id, {
// text: text,
// reRecord: false,
// pauseInputAudio: true,
// text_is_over: true,
// is_cache: true,
// frameOnTTScb(bufferAudio, is_over) {
// // console.log(text, is_over, bufferAudio);
// combinedBuffer = Buffer.concat([combinedBuffer, bufferAudio]);
// if (is_over) {
// devLog && log.t_info(`缓存 "${text}" TTS 完毕`);
// G_set_cahce_TTS(tts_cache_key, combinedBuffer);
// }
// }
// })
// }
// }

intention.forEach(({ instruct, pin }) => {
if (instruct === "__io_high__" || instruct === "__io_low__") {
!pin && log.error(`__io_high__ 指令必须配置 pin 数据`);
G_Instance.pinMode(device_id, pin, "OUTPUT");
}
})


// 播放ws连接成功语音
if (connected_reply) {
Expand Down
22 changes: 17 additions & 5 deletions src/functions/init_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const isOutTimeErr = require("../utils/isOutTimeErr");
const TTS_buffer_chunk_queue = require("../utils/tts_buffer_chunk_queue");
const {
audio, start, play_audio_ws_conntceed, client_out_audio_ing: client_out_audio_ing_fn,
client_out_audio_over, cts_time, client_receive_audio_over, set_wifi_config_res
client_out_audio_over, cts_time, client_receive_audio_over, set_wifi_config_res, digitalRead, analogRead
} = require("../functions/client_messages");
const { add_audio_out_over_queue_hoc, run_audio_out_over_queue_hoc, clear_audio_out_over_queue_hoc } = require("./device_fns/audio_out_over_queue")

Expand Down Expand Up @@ -90,26 +90,32 @@ function init_server() {
// 已输出流量 kb
useed_flow: 0,
// 是否处于 IAT 预备状态
iat_readiness: false
iat_readiness: false,
read_pin_cbs: new Map(),
});

ws.isAlive = true;
ws.device_id = device_id;

onDeviceConnect && onDeviceConnect({ ws, device_id, client_version });
onDeviceConnect && onDeviceConnect({
ws, device_id, client_version, client_params,
instance: G_Instance
});

ws.on('message', async function (data) {
const comm_args = { device_id };
try {
if (typeof data === "string") {
const { type, tts_task_id, stc_time, session_id, sid, text, success } = JSON.parse(data);
const { type, tts_task_id, stc_time, session_id, sid, text, success, value, pin } = JSON.parse(data);
comm_args.session_id = session_id;
comm_args.tts_task_id = tts_task_id;
comm_args.sid = sid;
comm_args.stc_time = stc_time;
comm_args.type = type;
comm_args.text = text;
comm_args.success = success;
comm_args.value = value;
comm_args.pin = pin;
switch (type) {
case "start":
start(comm_args);
Expand All @@ -135,6 +141,12 @@ function init_server() {
case "set_wifi_config_res":
set_wifi_config_res(comm_args);
break;
case "digitalRead":
digitalRead(comm_args);
break;
case "analogRead":
analogRead(comm_args);
break;
}
} else {
ws.isAlive = true;
Expand Down Expand Up @@ -170,7 +182,7 @@ function init_server() {
if (!auth_success) {
ws.send(JSON.stringify({
type: "auth_fail",
message: `${auth_message || "-"}`,
message: `${auth_message || "-"}`,
code: isOutTimeErr(auth_message) ? "007" : auth_code,
}));
// 防止大量失效用户重复请求
Expand Down
Loading

0 comments on commit 070635b

Please sign in to comment.