原作者分享的程式:drive.google.com/file/d/1yK41Hcmm2toH-RvoWOsVHtBbvEWt9yUS/view?usp=sharing
xiaozhi-mcp運用的視頻:讓小智語音控制ESP32點燈-MCP插件教程
※安裝好xiaozhi_mcp涵式庫後可以參考它附的範例檔的寫法
#include <WiFi.h>
#include <xiaozhi_mcp.h>
const char* ssid = "您的WiFi名稱";
const char* password = "您的密碼";
const char* mcpEndpoint = "ws://您的MCP接入點網址"; // 從小智後台獲取
WebSocketMCP mcpClient;
// 定義 AI 呼叫的工具:控制 LED
ToolResponse ledControl(const String& params) {
// 假設 AI 傳入參數 {"power": "on"}
if (params.indexOf("on") > -1) {
digitalWrite(2, HIGH); // ESP32 內建 LED 通常在 GPIO 2
return ToolResponse::success("LED 已開啟");
} else {
digitalWrite(2, LOW);
return ToolResponse::success("LED 已關閉");
}
}
void onConnectionStatus(bool connected) {
if (connected) {
Serial.println("[MCP] 連線成功,正在註冊工具...");
// 註冊工具名稱、描述與回標函式
mcpClient.addTool("control_led", "控制設備的 LED 開關", ledControl);
}
}
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
// 設定回調並啟動連線
mcpClient.onStatusChange(onConnectionStatus);
mcpClient.begin(mcpEndpoint);
}
void loop() {
mcpClient.loop(); // 保持連線處理
}
// ESP32-C3 接腳定義及速度變數
const int M1_IN1 = 1;
const int M1_IN2 = 0;
const int M2_IN1 = 3;
const int M2_IN2 = 2;
int userSpeed = 255; //PWM最大值
// --- 驅動函式 ---
void drive(int s1, int s2, int s3, int s4) {
ledcWrite(M1_IN1, s1);
ledcWrite(M1_IN2, s2);
ledcWrite(M2_IN1, s3);
ledcWrite(M2_IN2, s4);
}
if (currentState == SWAY) {
// 撒嬌模式:正弦波前後擺動
swayTick += 0.08;
float s = sin(swayTick) * 200; // 提高力道確保後退順暢
if (s > 150) drive((int)s, 0, (int)s, 0); // 前進
else if (s < -150) drive(0, (int)abs(s), 0, (int)abs(s)); // 後退
else stopMotors(); // 換向中間停頓
}
else if (currentState == MOVE) {
// 一般移動模式
if (currentDir == "forward") drive(userSpeed, 0, 0, userSpeed);
else if (currentDir == "backward") drive(0, userSpeed, userSpeed, 0);
else if (currentDir == "left") drive(0, userSpeed, 0, userSpeed);
else if (currentDir == "right") drive(userSpeed, 0, userSpeed, 0);
else { stopMotors(); currentState = IDLE; }
}
else {
stopMotors();
}
void registerMcpTools() {
mcpClient.registerTool(
"pet_action",
"控制小寵物的動作:forward, backward, left, right, sway, stop",
"{\"type\":\"object\",\"properties\":{\"action\":{\"type\":\"string\",\"enum\":[\"forward\",\"backward\",\"left\",\"right\",\"sway\",\"stop\"]},\"speed\":{\"type\":\"integer\"}},\"required\":[\"action\"]}",
[](const String& params) {
JsonDocument doc;
deserializeJson(doc, params);
String action = doc["action"] | "stop";
int speed = doc["speed"] | 200; //預設馬達PWM值
if (speed > 0) userSpeed = constrain(speed, 150, 250); //在150-250中取值
String msg;
if (action == "sway") {
currentState = SWAY;
currentDir = "stop"; // 清除原本的方向快取
swayTick = 0;
roboEyes.setMood(HAPPY);
roboEyes.anim_laugh();
msg = "我在主人面前開心地晃來晃去~";
}
else if (action == "stop") {
currentState = IDLE;
currentDir = "stop";
roboEyes.setMood(DEFAULT);
msg = "好的,我乖乖休息陪你。";
}
else {
currentState = MOVE;
currentDir = action;
roboEyes.setMood(HAPPY);
msg = "我正努力邁開小短腿跑向你喔!";
}
// 採用您要求的回傳寫法
return WebSocketMCP::ToolResponse(msg, true);
}
);
桌面機器人MoCoffee