ESP32调用当地部署的ChatTTS

打印 上一主题 下一主题

主题 1006|帖子 1006|积分 3028

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1 当地部署ChatTTS

参考 windows10部署ChatTTS+Apifox调用
2 实现过程

部署好当地ChatTTS模子后,测试接口正常。让ESP32接入局域网络,发送post请求生成wav文件,ESP32通过远程获取文件来得到音频文件,并播放音频文件。
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <ArduinoJson.h>
  4. const char* ssid = "EzhanNet";
  5. const char* password = "11111111";
  6. const char* serverUrl = "http://192.168.0.121:9994/tts";
  7. void sendPostTTSRequest() {
  8.   if (WiFi.status() == WL_CONNECTED) {
  9.     HTTPClient http;
  10.     http.begin(serverUrl);  // 目标URL
  11.     String payload = "text=windows&voice=1111&top_p=0.7&top_k=20&temperature=0.3&custom_voice=3531&refine_max_new_token=384";
  12.     http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 设置请求头
  13.     int httpResponseCode = http.POST(payload); // 发送 POST 请求
  14.     if (httpResponseCode > 0) {
  15.       String response = http.getString(); // 获取响应
  16.       Serial.printf("Response code: %d\n", httpResponseCode);
  17.       // Serial.println("Response: " + response);
  18.       getWavFile(response);
  19.     } else {
  20.       Serial.println("发送 POST 时出错: " + String(httpResponseCode));
  21.     }
  22.     http.end(); // 结束请求
  23.   } else {
  24.     Serial.println("WiFi not connected");
  25.   }
  26. }
  27. void getWavFile(String response) {
  28.   // 解析 JSON 响应
  29.       DynamicJsonDocument docJson(4096);
  30.       DeserializationError error = deserializeJson(docJson, response);
  31.       if (!error) {
  32.         const char* response_info = docJson["url"];
  33.         Serial.println("chattts speak file: " + String(response_info));
  34.         HTTPClient http2;
  35.         http2.begin(String(response_info));
  36.         int httpCode = http2.GET();
  37.         if(httpCode > 0) {
  38.             // HTTP header has been send and Server response header has been handled
  39.             Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  40.             // file found at server
  41.             if(httpCode == HTTP_CODE_OK) {
  42.                 String payload = http2.getString();
  43.                 Serial.println(payload);
  44.             }
  45.         } else {
  46.             Serial.printf("[HTTP] GET... failed, error: %s\n", http2.errorToString(httpCode).c_str());
  47.         }
  48.         http2.end();
  49.       } else {
  50.         Serial.println("解析 JSON 失败: " + String(error.c_str()));
  51.       }
  52. }
  53. void setup() {
  54.   Serial.begin(115200);
  55.   // 连接到WiFi
  56.   WiFi.begin(ssid, password);
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(500);
  59.     Serial.println("Connecting to WiFi...");
  60.   }
  61.   Serial.println("Connected to WiFi");
  62.   Serial.print("IP : ");
  63.   Serial.println(WiFi.localIP());
  64.   sendPostTTSRequest();
  65. }
  66. void loop() {
  67. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表