esp32C3合宙 连接豆包AI

1.arduino IDE环境配置

安装esp32开发板

选择开发板类型

串口配置

这个很重要

2.代码

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

// 1. Replace with your network credentials
const char* ssid = "输入你的WIFI名称";
const char* password = "WIFI密码";

// 2. Replace with your OpenAI API key
const char* doubao_apiKey = "豆包API key";

// Send request to OpenAI API
String inputText = "你好,豆包!";
String apiUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";

String answer;
String getGPTAnswer(String inputText) {
  HTTPClient http;
  http.setTimeout(20000);
  http.begin(apiUrl);
  http.addHeader("Content-Type", "application/json");
  String token_key = String("Bearer ") + doubao_apiKey;
  http.addHeader("Authorization", token_key);
  String payload = "{\"model\":\"接入点名称\",\"messages\":[{\"role\":\"system\",\"content\":\"你是我的AI助手vor,你必须用中文回答且字数不超过85个\"},{\"role\":\"user\",\"content\":\"" + inputText + "\"}],\"temperature\": 0.3}";

  int httpResponseCode = http.POST(payload);
  if (httpResponseCode == 200) {
    String response = http.getString();
    http.end();
    Serial.println(response);

    // Parse JSON response
    DynamicJsonDocument jsonDoc(1024);
    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() {
  // Initialize Serial
  Serial.begin(115200);

  // Connect to Wi-Fi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  answer = getGPTAnswer(inputText);
  Serial.println("Answer: " + answer);
  Serial.println("Enter a prompt:");
}

void loop() {
  // do nothing
  if (Serial.available()) {
    inputText = Serial.readStringUntil('\r');  //  \r表示结束符为回车符
    // inputText.trim();
    Serial.println("\n Input:" + inputText);

    answer = getGPTAnswer(inputText);
    Serial.println("Answer: " + answer);
    Serial.println("Enter a prompt:");
  }
  // delay(2);
}

3.注意事项

改成回车,因为程序设置了读取输入的字符串读到/r(回车符)就结束

参考文档:【ESP32接入国产大模型之豆包】 - 文章 - 开发者社区 - 火山引擎

### 关于ESP32-S3与Arduino相关的项目或教程 #### 使用ESP32-S3作为开发板的基础设置 为了使ESP32-S3能够在Arduino环境中正常工作,需先确认该型号已加入至Arduino IDE的支持列表中。一旦完成环境搭建,在IDE内可通过路径`工具>开发板>`找到对应的ESP32-S3选项[^1]。 #### 实现ESP32-S3的SPP蓝牙通信功能 针对希望利用ESP32-S3开展基于Bluetooth SPP (Serial Port Profile) 协议的应用场景而言,开发者可以借助Arduino平台编写程序来激活并管理ESP32内部集成的蓝牙模块,进而达成与其他兼容设备间的无线数据交换目的[^2]。 下面给出一段简单的示例代码用于初始化ESP32-S3上的蓝牙服务,并建立一个基本的服务端连接: ```cpp #include "BluetoothSerial.h" #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it. #endif BluetoothSerial SerialBT; void setup() { // 初始化串行监视器波特率为9600bps Serial.begin(9600); // 启动蓝牙序列接口,默认参数 SerialBT.begin("ESP32_S3_BT"); // 设备名称 Serial.println("The device started, now you can pair it with bluetooth!"); } void loop() { if (Serial.available()) { SerialBT.write(Serial.read()); // 将来自PC的数据转发给配对成功的客户端 } if (SerialBT.available()){ Serial.write(SerialBT.read()); // 把接收到的信息回传显示在监控窗口里 } } ``` 此段代码实现了两个主要的功能:一是允许其他蓝牙装置发现当前ESP32-S3节点;二是维持两者间稳定可靠的双向通讯链路。 #### 集成图形界面库(LVGL)到ESP32-S3项目当中 对于那些计划在其作品里面引入GUI组件的人来说,LVGL是一个非常不错的选择。它不仅提供了丰富的控件种类供调用者选用,而且易于移植到不同类型的微控制器平台上。要让ESP32-S3支持LVGL,则需要按照特定流程安装必要的依赖项以及调整相应的编译配置[^3]。 以下是简化版的操作指南: - 下载官方发布的最新版本lvgl源码包; - 解压后挑选出适用于ESP32系列MCU的核心部分(即`lvgl`, `ESP32_Display_Panel`, 和`ESP32_IO_Expander`三个目录),并将它们放置于个人资料夹下的Arduino/libraries子文件夹之中; - 修改完上述操作之后重启Arduino IDE使之生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值