0


esp32接入ai模型

esp32接入ai模型与经验分享

前言

本次用到的为arduino环境编写的。
先前我是用到Thonny,但其网络库在api请求时不支持重定向,而arduino库支持。
并且感谢博主 2345VOR (鹏哥) 其博客:
2345VOR
我的代码在他的基础框架进行修改的

实物

在这里插入图片描述

硬件准备

1.一块esp32
在这里插入图片描述
2.屏幕 1.8寸TFT彩屏(驱动芯片ST7735)
在这里插入图片描述
3.语音输入与输出部分
(货物清单在这)
请添加图片描述
4.杜邦线,面包线,led,按键,散热片(芯片发热量有点大)

库文件准备

1.首先要有编程环境(arduino)
2.安装esp32开发板管理
3.安装必要的库文件

  1. #include <TFT_eSPI.h> //tft库
  2. #include <U8g2_for_TFT_eSPI.h> //汉字库头文件,主要是它
  3. #include <WiFi.h>
  4. #include <ArduinoJson.h>
  5. #include <HTTPClient.h>
  6. #include "base64.h"
  7. #include "cJSON.h" //网络请求与解析库
  8. #include <driver/i2s.h>//语音输入输出库
  9. #include "Audio.h"

(网络与解析库文件可以直接在arduino下载)
(<driver/i2s.h>库本身就包含了是i2s库的一个分支)
("Audio.h"库需要自己安装)esp32_audio库
(TFT_esoI.h可以直接在arduino下载)
(<U8g2_for_TFT_eSPI.h>库有博主提供了需要自己放在库文件下)
TFT汉字库

TFT屏幕的驱动设置

详细可以看b站视频驱动设置
我这里带大家简单操作下
1.打开对应的库文件
在这里插入图片描述打开arduino文件夹
在这里插入图片描述打开libaries文件夹
打开对应的库文件夹打开TFT_ESPI库文件夹
在这里插入图片描述选择这个文件,进行屏幕驱动的选择
在这里插入图片描述对于我这块屏幕是st7735驱动芯片驱动的
在对应的宏定义前解除注释就行
(驱动选择选完了之后就是引脚选择了)也在同样的文件下
在这里插入图片描述这是我对应的引脚修改
这些做完之后便可以先用arduino上的TFT例程测试一下
(详细可以看视频)

API请求

推荐调试工具为 (postman)
本次用到的为KIMI的ai模型API(post请求)
kimiAPI官网
语音识别为百度智能云上面的云服务(post请求)
语音输出也是百度智能云上的云服务(get请求)
百度智能云平台官网

(这里得知道如何发送请求,与接收json格式的数据)
(以及注册api账户开启api服务,这会产生一些费用)
【由于篇幅原因,希望您可以去网上自行了解】

语音上传部分

  1. 一.语音数据采集
  2. 语音识别部分为driver/i2s.h库,麦克风部分为INMP441,具体用法
  3. 可以csdn上看其他博主的介绍,值得注意的是这个库虽然在声音信号上的
  4. 数据采集便利了,但其本身双声道库,一个左或者右麦克风只能采集一半的数
  5. 据量,举例:如果我是8k采样160016位数据,按理来说是采集2秒,但实际
  6. 上其接口函数只有前一半右数据(左声道),后一半(右声道)无数据都为0
  7. 为了解决这个问题有两种方法
  8. 1.是数据增加到3200个,这样也是采样两秒(直接out)浪费资源
  9. 2.在接口函数填入数组大小是设置为两倍长度
  10. 二.数据上传部分
  11. 要将采集到的音频数据编码为base64才可上传到百度云,该上传代码
  12. 主要为2345VOR博主所提供

文字转语音

  1. 这个是我看到了其他博主的文章发现audio.h库刚好有函数接口可以播放
  2. 网络音乐(MP3格式),正好百度智能云的文字转语音用get方法获取也为
  3. MP3格式,在游览器上试了下可以(这个不能用postman调试了),所以这
  4. 一部分库函数功能刚好实现

同时输出与输入音频文件

  1. 因为库的底层有冲突站用了同一个I2s,虽然说esp32i2s资源有两个i2s1
  2. i2s2,我尝试了修改其库的底层让他们分别用不同的i2s,但还是会一直重
  3. 启报错(其实是冲突了),网上查了一下是虽然有两个i2s但其实还是同一个
  4. 也就是不能同时输出与输入
  5. 解决办法是:用到时候安装上对应的驱动,用完了再去卸载驱动,给另外
  6. 一个用,这样就不会冲突了

接线部分

  1. 语音输出(max98357
  2. #define I2S_DOUT 25
  3. #define I2S_BCLK 27
  4. #define I2S_LRC 26
  5. max98357的接线端子还要接喇叭(正负不分)
  6. 语音输入(inmp441
  7. #define I2S_WS 32
  8. #define I2S_SD 35
  9. #define I2S_SCK 33
  10. 注意INMP441 L/R接地
  11. tft屏幕
  12. #define TFT_MOSI 2
  13. #define TFT_SCLK 15
  14. #define TFT_CS 17
  15. #define TFT_DC 16
  16. #define TFT_RST 4
  17. LED
  18. #define ledPin 21 (接正极)
  19. 按键
  20. #define keypin 18 (按键脚的另一端接地,且需要给18脚接个上拉电阻)

整个工程arduino源码

  1. #include <TFT_eSPI.h> //tft库
  2. #include <U8g2_for_TFT_eSPI.h> //汉字库头文件,主要是它
  3. #include <WiFi.h>
  4. #include <ArduinoJson.h>
  5. #include <HTTPClient.h>
  6. #include "base64.h"
  7. #include "cJSON.h" //网络请求与解析库
  8. #include <driver/i2s.h>//语音输入输出库
  9. #include "Audio.h"
  10. //json data
  11. char data_json[43700]; //内存不大,只能录两秒钟,设置大了,会报错
  12. //音频输出
  13. #define I2S_DOUT 25
  14. #define I2S_BCLK 27
  15. #define I2S_LRC 26
  16. //文字显示
  17. U8g2_for_TFT_eSPI u8g2;
  18. TFT_eSPI my_tft;
  19. //WiFi
  20. const char* ssid = "OK"; //你的WiFi名称
  21. const char* password = "33333333"; //你的WiFi密码
  22. //声音采集
  23. //按照接线确定编号-----------------------------------------------------------------------------------------------------
  24. #define I2S_WS 32
  25. #define I2S_SD 35
  26. #define I2S_SCK 33
  27. // 使用I2S处理器
  28. #define I2S_PORT I2S_NUM_0
  29. // 定义缓冲区长度
  30. #define bufferLen 16000
  31. int16_t sBuffer[bufferLen]; //内存不大,只能录两秒钟,设置大了,会报错
  32. //-------------------------------------------------------------------------------------------------------------------------
  33. //kimi——api
  34. String Kimi_url = "https://api.moonshot.cn/v1/chat/completions";
  35. //百度文字转音频
  36. String voice_url = "http://tsn.baidu.com/text2audio";
  37. //文字显示函数
  38. void TFT_print(uint16_t x,uint16_t y,String Text,uint16_t TextColor,uint8_t Size)
  39. {
  40. switch(Size)
  41. {
  42. case 1:
  43. u8g2.setFont(u8g2_font_wqy12_t_gb2312); //12像素大小
  44. break;
  45. case 2:
  46. u8g2.setFont(u8g2_font_wqy16_t_gb2312); //16像素大小
  47. break;
  48. }
  49. u8g2.setForegroundColor(TextColor); //字的颜色
  50. u8g2.setCursor(y, x); //位置
  51. u8g2.print(Text); //打印
  52. }
  53. //——————————————————————————————————————————————————————————————————————————————————————————————————————————麦克风驱动安装的函数---------------------------------
  54. void i2s_install() {
  55. //设置I2S处理器配置
  56. const i2s_config_t i2s_config = {
  57. .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
  58. .sample_rate = 8000,
  59. .bits_per_sample = i2s_bits_per_sample_t(16),
  60. .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  61. .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
  62. .intr_alloc_flags = 0,
  63. .dma_buf_count = 8,
  64. .dma_buf_len = 64,
  65. .use_apll = false
  66. };
  67. i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  68. }
  69. void i2s_setpin() {
  70. // 设置I2S引脚配置
  71. const i2s_pin_config_t pin_config = {
  72. .bck_io_num = I2S_SCK,
  73. .ws_io_num = I2S_WS,
  74. .data_out_num = -1,
  75. .data_in_num = I2S_SD
  76. };
  77. i2s_set_pin(I2S_PORT, &pin_config);
  78. }
  79. //-------------------------------------------------------------------------------------------------------------------------------------------------------------
  80. //---------------------------------------------------------------ai回答部分(接入kimi)------------------------------------------
  81. String get_kimi(String question)
  82. {
  83. HTTPClient http1 ;
  84. http1.setTimeout(20000);
  85. http1.begin(Kimi_url);
  86. http1.addHeader("Content-Type", "application/json");
  87. http1.addHeader("Authorization", "sk-4cyiL5............................................................."); //注意得填写你的码
  88. String body="{\"model\":\"moonshot-v1-8k\",\"messages\":[{\"role\":\"system\",\"content\":\"你是我的AI助手小南,你必须用中文回答且字数不超过85个\"},{\"role\":\"user\",\"content\":\"" + question + "\"},{\"role\": \"assistant\",\"name\":\" 小南\", \"content\":\"\", \"partial\": true}],\"temperature\":0.3}";
  89. int httpResponseCode = http1.POST(body);
  90. if(httpResponseCode == 200)
  91. {
  92. String response = http1.getString();
  93. http1.end();
  94. //Serial.println(response);
  95. StaticJsonDocument<1024> json1;
  96. deserializeJson(json1,response);
  97. String json_get1 = json1["choices"];
  98. deserializeJson(json1,json_get1);
  99. String message = json1[0]["message"].as<String>();
  100. deserializeJson(json1,message);
  101. String respone = json1["content"];
  102. //Serial.println(respone);
  103. return respone ;
  104. }
  105. else
  106. {
  107. http1.end();
  108. //Serial.println("error");
  109. return "你问的太快了询问不能频繁当前为一分钟三到四次询问)";//(kimi的api比较菜一分钟只能问他3个问题)
  110. }
  111. }
  112. //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  113. String Voice_2_Text(); // 语音转文字的函数
  114. #define ledPin 21
  115. #define keypin 18
  116. void setup()
  117. {
  118. // put your setup code here, to run once:
  119. Serial.begin(115200);
  120. WiFi.mode(WIFI_STA);
  121. pinMode(ledPin, OUTPUT); //led初始化
  122. pinMode(keypin, INPUT); //按键初始化
  123. digitalWrite(ledPin,HIGH); //开始时,亮灯
  124. my_tft.init(); //初始化
  125. u8g2.begin(my_tft); //将U8g2绑定到TFT
  126. my_tft.setRotation(2); //屏幕旋转方向(可自行修改)
  127. my_tft.setTextColor(TFT_DARKGREEN); //设置屏幕字颜色(用到了字库的话本句没用)
  128. my_tft.fillScreen(TFT_BLACK); //屏幕背景
  129. i2s_install(); //麦克封初始化---安装驱动
  130. i2s_setpin();
  131. i2s_start(I2S_PORT);
  132. TFT_print(80,3,"Voiceinput (inx441) " ,TFT_YELLOW,1);
  133. delay(150);
  134. i2s_driver_uninstall(I2S_PORT); // 驱动卸载 (因为输出输入不能同时有,连个库的底层对应一个i2s,用到的时候在安装)
  135. //audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  136. //audio.setVolume(12); // 0...21
  137. //audio.connecttohost(""); //语音输出驱动(不能在全局定义,应为没有对应的释放接口,只能在局部变量中使用,否则esp32一直重启)
  138. TFT_print(90,3,"Voiceinput (out) " ,TFT_YELLOW,1);//屏幕打印信息
  139. delay(100);
  140. WiFi.begin(ssid, password); //WiFi连接
  141. uint8_t i = 1 ;
  142. TFT_print(10,1,"WiFi is connecting....",TFT_WHITE,1);
  143. while (WiFi.status() != WL_CONNECTED) {
  144. TFT_print(20,i*5+1,".",TFT_WHITE,1);
  145. delay(800);
  146. i++;
  147. if(i>=25)
  148. {
  149. TFT_print(30,1,"WiFi can`t conect ",TFT_RED,1);
  150. TFT_print(40,1,"please check and again",TFT_RED,1);
  151. while(1){}
  152. }
  153. }
  154. digitalWrite(ledPin,LOW);
  155. String ip = WiFi.localIP().toString().c_str();
  156. TFT_print(30,1,"WiFi is connected",TFT_DARKGREEN,1);
  157. TFT_print(40,1,"IP:" + ip ,TFT_DARKGREEN,1);
  158. TFT_print(50,1,"Welcome:" ,TFT_WHITE,1);
  159. TFT_print(60,3,"The api is kimi" ,TFT_WHITE,1);
  160. TFT_print(70,3,"the character is CoNan" ,TFT_WHITE,1);
  161. delay(1500);
  162. my_tft.fillScreen(TFT_BLACK);
  163. }
  164. uint8_t voice_flag = 0 ;//是否有语音生成的状态值
  165. String voic_get; // 文字转语音用到的get网址
  166. void loop()
  167. {
  168. bool VAL_KEY = digitalRead(keypin);
  169. TFT_print(10,1,"Answer:(空闲中) ",TFT_DARKGREEN,1);
  170. if (VAL_KEY ==LOW)
  171. {
  172. delay(500);
  173. String str_error = "error" ;
  174. String get = Voice_2_Text();
  175. if(get.compareTo(str_error) !=0)
  176. {
  177. Serial.println(get);
  178. // inputText.trim();
  179. my_tft.fillScreen(TFT_BLACK);
  180. TFT_print(10,1,"Answer:(思考中)",TFT_RED,1);
  181. Serial.println("\n Input:"+get);
  182. String answer = get_kimi(get);
  183. Serial.println("Answer: " + answer);
  184. Serial.println("Enter a prompt:");
  185. TFT_print(10,1,"Answer:",TFT_WHITE,1);
  186. uint16_t len = answer.length();
  187. uint16_t i =0;
  188. uint16_t j =0;
  189. TFT_print(10,1,"Answer:(交谈中) ",TFT_YELLOW,1);
  190. for(i=0;i<len;i=i+30)
  191. {
  192. uint16_t k=0;
  193. for(k=0;k<30;k=k+3)
  194. {
  195. TFT_print(22+j,5+(k/3)*12, answer.substring(i+k,i+k+3),TFT_WHITE,1);
  196. delay(15);
  197. }
  198. //TFT_print(22+j,5, answer.substring(i,i+30),TFT_WHITE,1);
  199. j+=15;
  200. delay(15);
  201. }//屏幕一个字一个字的显示的遍历
  202. voic_get = voice_url + "?tex=" + answer +"&lan=zh&cuid=2579527033&ctp=1&tok=24.d9c2c.........................&per=111";//+号的&tok=...后面的部分得填写你的tok码
  203. delay(20);
  204. voice_flag = 1;
  205. }else
  206. {
  207. TFT_print(10,1,"Answer:(出错了) ",TFT_RED,1);
  208. TFT_print(25,1,"服务器端错误 ",TFT_YELLOW,1);
  209. }
  210. Serial.println("voice_flag is:");
  211. Serial.println(voice_flag);
  212. }
  213. if(voice_flag == 1)
  214. {
  215. voice_flag = 0;
  216. Audio audio;
  217. audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);//安装驱动
  218. audio.setVolume(12); // 0...21 设置音量大小
  219. const char* voice_url = voic_get.c_str(); //转化数据类型
  220. audio.connecttohost(voice_url); //get请求网络上百度云生成的语音数据
  221. TFT_print(10,1,"Answer:(空闲中) ",TFT_DARKGREEN,1); //屏幕显示
  222. while(1)
  223. {
  224. audio.loop(); //循环播放
  225. VAL_KEY = digitalRead(keypin); //检测按键值
  226. if(VAL_KEY ==LOW )
  227. {
  228. break; //按下时为退出
  229. }
  230. }
  231. }
  232. }
  233. //--------------------------------------------语音识别函数--------------------------------------------------------------
  234. String Voice_2_Text()
  235. {
  236. //安装麦克风驱动-------------------------------
  237. i2s_install(); //麦克封初始化
  238. i2s_setpin();
  239. i2s_start(I2S_PORT);
  240. //-----------------------------------------------
  241. memset(data_json, '\0', strlen(data_json)); //将数组清空
  242. TFT_print(10,1,"Answer:(聆听中) ",TFT_BLUE,1);
  243. size_t bytesIn = 0;
  244. digitalWrite(ledPin,HIGH);
  245. esp_err_t result_voice = i2s_read(I2S_PORT, &sBuffer, 2*bufferLen, &bytesIn, portMAX_DELAY); //应为该函数是用的左右声道同时采集,我们只有一个声道,所以告诉函数采用长度为实际数据的两倍数据长度,这样就可以将所有数据填满
  246. // result = i2s_read(I2S_PORT, &sBuffer, 2*bufferLen, &bytesIn, portMAX_DELAY);
  247. if (result_voice == ESP_OK)//采样完成标志位
  248. {
  249. TFT_print(10,1,"Answer:(识别中) ",TFT_BLUE,1);
  250. digitalWrite(ledPin,LOW);
  251. i2s_driver_uninstall(I2S_PORT);//麦克封驱动卸载
  252. Serial.println("驱动卸载");
  253. strcat(data_json, "{");
  254. Serial.println("json写入");
  255. strcat(data_json, "\"format\":\"pcm\",");
  256. delay(10);
  257. Serial.println("json写入1");
  258. strcat(data_json, "\"rate\":8000,");
  259. Serial.println("json写入2"); //采样率 如果采样率改变了,记得修改该值,只有16000、8000两个固定采样率
  260. strcat(data_json, "\"dev_pid\":1537,");
  261. Serial.println("json写入3"); //中文普通话
  262. strcat(data_json, "\"channel\":1,");
  263. Serial.println("json写入4"); //单声道
  264. strcat(data_json, "\"cuid\":\"2579527033\",");
  265. Serial.println("json写入5"); //识别码 随便打几个字符,但最好唯一
  266. strcat(data_json, "\"token\":\"24.d9c2c44461f80b0.....................................................\","); //这里填写你的tok码
  267. Serial.println("json写入6"); //token 这里需要修改成自己申请到的token
  268. strcat(data_json, "\"len\":32000,");
  269. Serial.println("json写入7"); //数据长度 如果传输的数据长度改变了,记得修改该值,该值是ADC采集的数据字节数,不是base64编码后的长度
  270. strcat(data_json, "\"speech\":\"");
  271. Serial.println("json写入8");
  272. delay(20);
  273. strcat(data_json, base64::encode((uint8_t*)sBuffer, sizeof(sBuffer)).c_str()); //base64编码数据
  274. delay(20);
  275. Serial.println("json写入9");
  276. strcat(data_json, "\"");
  277. Serial.println("json写入10");
  278. strcat(data_json, "}");
  279. Serial.println("json以安好");
  280. int httpCode;
  281. HTTPClient http2;
  282. http2.setTimeout(5000); //设置超出响应时间
  283. http2.begin("http://vop.baidu.com/server_api"); //https://vop.baidu.com/pro_api 语音转文字api
  284. http2.addHeader("Content-Type", "application/json");
  285. httpCode = http2.POST(data_json);
  286. Serial.println("json以发送");
  287. if (httpCode == 200)
  288. {
  289. if (httpCode == HTTP_CODE_OK)
  290. {
  291. Serial.println("接收正确");
  292. String response = http2.getString();
  293. http2.end();
  294. Serial.println(response);
  295. DynamicJsonDocument jsonDoc(1024);
  296. // Parse JSON response
  297. // DynamicJsonDocument jsonDoc(512);
  298. deserializeJson(jsonDoc, response);
  299. String question = jsonDoc["result"][0];
  300. // 访问"result"数组,并获取其第一个元
  301. // 输出结果
  302. Serial.println(question);
  303. return question ;
  304. } else {
  305. Serial.printf("[HTTP] GET... failed, error: %s\n", http2.errorToString(httpCode).c_str());
  306. return "error" ;
  307. }
  308. }
  309. else
  310. {
  311. Serial.println("接收有误区");
  312. return "error";
  313. }
  314. }
  315. }
  316. //---------------------------------------------------------------------------------------------------------

存在的问题

  1. 我只是把他基本功能完成了,还有很多地方可以优化,且到了晚上开机第一次
  2. 使用时都是报错,之后就可以用了。如果您有什么好的办法与优化欢迎留言。

本文转载自: https://blog.csdn.net/2301_79696354/article/details/139411251
版权归原作者 瑾sing 所有, 如有侵权,请联系我们删除。

“esp32接入ai模型”的评论:

还没有评论