2025-12-11 20:44:29 [显示全部楼层]
10浏览
查看: 10|回复: 0

[项目] ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯

[复制链接]
本帖最后由 云天 于 2025-12-11 20:47 编辑

       本项目创造性地将小智AI(ESP32-S3 AI摄像头)与掌控板相结合,通过UDP协议实现了语音和图像识别双重控制方式。这一创新方案突破了传统小智AI系统与其他主控设备通信的技术壁垒,为创客作品制作提供了一种简单、高效且灵活的解决方案。
【技术特点与创新点】
       1. 创新性的UDP通信架构
       本项目最大的技术突破在于使用UDP协议作为小智AI与外部主控设备之间的通信桥梁。传统的小智AI系统与其他设备通信需要复杂的MCP协议实现,而本项目通过简单的UDP消息传递,大大降低了开发门槛和系统复杂性。
       2. 验证双重控制方式
       演示两种控制方式:
  • 语音控制:用户通过自然语言对话控制设备
  • 图像识别控制:通过摄像头识别纸上文字进行控制

       3. 开放式架构设计
       通过UDP通信,小智AI可以与任何支持UDP协议的主控设备进行通信,包括但不限于掌控板、Arduino、树莓派等,极大地扩展了小智AI的应用场景。
【硬件组成】
ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯图1

       1.小智AI(ESP32-S3 AI摄像头)
  • ESP32-S3双核处理器
  • 内置摄像头模块
  • 麦克风阵列
  • Wi-Fi连接功能
ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯图3

      2.掌控板(主控设备)
  • ESP32主控
  • 集成RGB LED灯
  • OLED显示屏
  • 多种传感器接口
【软件实现】
       小智AI固件修改
       修改了小智AI的开源代码,在mcp_server.cc中实现了UDP通信功能:
       1.增加头文件
  1. #include <lwip/sockets.h>  // 添加socket头文件
  2. #include <string.h>        // 添加字符串处理头文件
复制代码
       2.UDP初始化
  1. #define UDP_SERVER_IP "192.168.31.163"  // 目标服务器IP
  2. #define UDP_SERVER_PORT 8888            // 目标服务器端口
  3. // 声明UDP发送函数
  4. static bool send_udp_letter(const char letter);
复制代码
       3.添加灯光控制工具
       在McpServer::AddCommonTools(){}中添加。这个函数是所有MCP工具的集中注册点,将各种设备控制功能封装为MCP工具,使得AI助手可以通过标准协议调用这些功能。
  1. void McpServer::AddCommonTools() {
  2.     // To speed up the response time, we add the common tools to the beginning of
  3.     // the tools list to utilize the prompt cache.
  4.     // Backup the original tools list and restore it after adding the common tools.
  5.     auto original_tools = std::move(tools_);
  6.     auto& board = Board::GetInstance();
  7.     //修改
  8.     // 添加语音控制灯光工具
  9.    // 添加开灯工具 - 发送字母A
  10.     AddTool("self.light.turn_on",
  11.         "打开灯光。此工具通过UDP向小智AI服务器发送字母'A',表示打开灯光。\n"
  12.         "当用户想要打开灯光或表达类似意图时使用此工具。\n"
  13.         "无需参数。\n"
  14.         "返回:\n"
  15.         "  包含成功状态和消息的JSON对象",
  16.         PropertyList(),
  17.         [](const PropertyList& properties) -> ReturnValue {  // 修改:添加参数
  18.             ESP_LOGI(TAG, "打开灯光 - 发送字母'A'到 %s:%d",
  19.                      UDP_SERVER_IP, UDP_SERVER_PORT);
  20.             
  21.             // 发送UDP数据
  22.             bool result = send_udp_letter('A');
  23.             
  24.             if (result) {
  25.                 std::string success_msg = "成功打开灯光,发送字母'A'到 " +
  26.                                           std::string(UDP_SERVER_IP) + ":" + std::to_string(UDP_SERVER_PORT);
  27.                 ESP_LOGI(TAG, "%s", success_msg.c_str());
  28.                 return "{"success": true, "action": "turn_on", "message": "" + success_msg + ""}";
  29.             } else {
  30.                 std::string error_msg = "打开灯光失败,发送字母'A'到 " +
  31.                                         std::string(UDP_SERVER_IP) + ":" + std::to_string(UDP_SERVER_PORT) + " 失败";
  32.                 ESP_LOGE(TAG, "%s", error_msg.c_str());
  33.                 return "{"success": false, "action": "turn_on", "message": "" + error_msg + ""}";
  34.             }
  35.         });
  36.     // 添加关灯工具 - 发送字母B
  37.     AddTool("self.light.turn_off",
  38.         "关闭灯光。此工具通过UDP向小智AI服务器发送字母'B',表示关闭灯光。\n"
  39.         "当用户想要关闭灯光或表达类似意图时使用此工具。\n"
  40.         "无需参数。\n"
  41.         "返回:\n"
  42.         "  包含成功状态和消息的JSON对象",
  43.         PropertyList(),
  44.         [](const PropertyList& properties) -> ReturnValue {  // 修改:添加参数
  45.             ESP_LOGI(TAG, "关闭灯光 - 发送字母'B'到 %s:%d",
  46.                      UDP_SERVER_IP, UDP_SERVER_PORT);
  47.             
  48.             // 发送UDP数据
  49.             bool result = send_udp_letter('B');
  50.             
  51.             if (result) {
  52.                 std::string success_msg = "成功关闭灯光,发送字母'B'到 " +
  53.                                           std::string(UDP_SERVER_IP) + ":" + std::to_string(UDP_SERVER_PORT);
  54.                 ESP_LOGI(TAG, "%s", success_msg.c_str());
  55.                 return "{"success": true, "action": "turn_off", "message": "" + success_msg + ""}";
  56.             } else {
  57.                 std::string error_msg = "关闭灯光失败,发送字母'B'到 " +
  58.                                         std::string(UDP_SERVER_IP) + ":" + std::to_string(UDP_SERVER_PORT) + " 失败";
  59.                 ESP_LOGE(TAG, "%s", error_msg.c_str());
  60.                 return "{"success": false, "action": "turn_off", "message": "" + error_msg + ""}";
  61.             }
  62.         });
复制代码

       4.UDP发送函数
  1. // UDP发送函数的实现
  2. static bool send_udp_letter(const char letter) {
  3.     int sockfd = -1;
  4.     struct sockaddr_in dest_addr;
  5.    
  6.     // 创建UDP socket
  7.     sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  8.     if (sockfd < 0) {
  9.         ESP_LOGE(TAG, "Failed to create UDP socket: errno %d", errno);
  10.         return false;
  11.     }
  12.    
  13.     // 设置目标地址
  14.     memset(&dest_addr, 0, sizeof(dest_addr));
  15.     dest_addr.sin_family = AF_INET;
  16.     dest_addr.sin_port = htons(UDP_SERVER_PORT);
  17.    
  18.     // 解析目标IP地址
  19.     if (inet_pton(AF_INET, UDP_SERVER_IP, &dest_addr.sin_addr) != 1) {
  20.         ESP_LOGE(TAG, "Invalid IP address: %s", UDP_SERVER_IP);
  21.         close(sockfd);
  22.         return false;
  23.     }
  24.    
  25.     // 发送数据
  26.     ssize_t sent_bytes = sendto(sockfd, &letter, 1, 0,
  27.                                 (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  28.    
  29.     bool success = false;
  30.     if (sent_bytes < 0) {
  31.         ESP_LOGE(TAG, "Failed to send UDP data: errno %d", errno);
  32.     } else if (sent_bytes == 0) {
  33.         ESP_LOGE(TAG, "No data sent");
  34.     } else {
  35.         ESP_LOGI(TAG, "Sent %d bytes to %s:%d", (int)sent_bytes, UDP_SERVER_IP, UDP_SERVER_PORT);
  36.         success = true;
  37.     }
  38.    
  39.     // 关闭socket
  40.     close(sockfd);
  41.     return success;
  42. }
复制代码
【掌控板程序设计】
       使用Mind+ 2.0图形化编程环境,编写了掌控板的UDP服务器程序:
ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯图2
  1. /*!
  2. * MindPlus
  3. * DFRobot, 掌控板
  4. */
  5. #include <MPython.h>
  6. #include <DFRobot_Iot.h>
  7. #include <DFRobot_UDPServer.h>
  8. // 创建对象
  9. DFRobot_Iot myIot;
  10. DFRobot_UDPServer myserver;
  11. void onUdpServerRecvMsg(String message);
  12. // 主程序开始
  13. void setup() {
  14.         mPython.begin();
  15.         myserver.setCallback(onUdpServerRecvMsg);
  16.         rgb.write(-1, 0xFF0000);
  17.         myIot.wifiConnect("***", "*********");
  18.         while (!myIot.wifiStatus()) {yield();}
  19.         myserver.setPort(8888);
  20.         rgb.write(-1, 0x00FF00);
  21.         delay(1000);
  22.         rgb.write(-1, 0x000000);
  23.         display.setCursor(0, 22);
  24.         display.print(myIot.getWiFiSoftIP());
  25. }
  26. void loop() {
  27. }
  28. // 事件回调函数
  29. void onUdpServerRecvMsg(String message) {
  30.         display.fillInLine(3, 0);
  31.         display.setCursorLine(3);
  32.         display.printLine(message);
  33.         if ((message==String("A"))) {
  34.                 rgb.write(-1, 0x0000FF);
  35.         }
  36.         if ((message==String("B"))) {
  37.                 rgb.write(-1, 0x000000);
  38.         }
  39. }
复制代码
【系统工作原理】
       1. 语音控制流程用户说"开灯" → 小智AI语音识别 → MCP工具调用 → UDP发送'A' → 掌控板接收消息 → LED亮起

       2. 图像识别控制流程用户展示"开灯"文字 → 小智AI图像识别 → MCP工具调用 → UDP发送'A' → 掌控板接收消息 → LED亮起

       3. 通信流程小智AI (192.168.31.x) → UDP数据包 → WiFi网络 → 掌控板 (192.168.31.163:8888)

【技术优势】
  • 简化开发流程:无需实现复杂的MCP协议,只需简单的UDP通信
  • 跨平台兼容:支持任何支持UDP协议的主控设备
  • 实时响应:UDP协议的低延迟特性确保快速响应
  • 易于扩展:可轻松添加新的控制命令和设备类型
  • 成本效益:利用现有硬件,无需额外通信模块

【教育应用】
  • STEM教学项目
  • 物联网课程实践
  • 人工智能应用实例

ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯图5


ESP32-S3 AI智能摄像头(小智AI)与掌控板UDP通信控制LED灯图4



【演示视频】













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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail