11浏览
查看: 11|回复: 0

[进阶] Arduino UNO Q 板载 Nanobot 自动化编程指南之二

[复制链接]
本帖最后由 topdog 于 2026-3-24 23:01 编辑

第二章 Nanobot的AI赋能,网络搜索,知识库和自我学习技能

每个开发者都有自己编程的习惯,笔者也尝试了VScode,CodeBuddy,Qwen,Trae CN,Kiro等编译器,但是ssh连接Arduino UNO Q(以下简称:Q板)时都会出现不稳定的现象,最好的方法是MobaXterm和Claude code,上传和下载文件特别方便。
一,MobaXterm的使用
MobaXterm的下载地址:MobaXterm Xserver with SSH, telnet, RDP, VNC and X11 - Download
1,此时Arduino UNO Q已经登录到局域网上了,获得的IP是192.168.3.20,用户名输入:arduino,点击ok。那么这就完成了ssh登录的配置。

Arduino UNO Q 板载 Nanobot 自动化编程指南之二图2

2,Q板作为SBC(单板计算机)使用,有的爱好者喜欢VNC连接,但是配置起来非常麻烦。笔者用RDP就很方便易用。

  1. # 编辑SSH配置文件
  2. sudo nano /etc/ssh/sshd_config
  3. # 确保以下行未被注释,Nanobot网络和显示需要的运行条件
  4. Port 22
  5. AddressFamily any
  6. ListenAddress 0.0.0.0
  7. ListenAddress ::
  8. GatewayPorts yes
  9. X11Forwarding yes
  10. X11DisplayOffset 10
  11. X11UseLocalhost yes
  12. # 重启SSH服务
  13. sudo systemctl restart sshd
  14. # 安装xauth(关键!)Debian 13已经自带
  15. sudo apt install xauth
  16. sudo apt install xrdp
  17. # 启动xrdp和状态
  18. systemctl status xrdp
  19. systemctl start xrdp
复制代码

3,在MobaXterm中设置RDP Session,端口是3389。RDP连接状态下,就不需要另外购置鼠标键盘来操作了,直接用PC的输入设备来远程操控 Q板。

4,在PC的搜索栏输入远程桌面,输入IP:3389。也能够远程登录到Arduino UNO Q的桌面。
Arduino UNO Q 板载 Nanobot 自动化编程指南之二图1

5,一般用ssh登录,在终端输入:claude ,启动Claude Code来为下面配置Nanobot进行AI赋能。调试好以后,另外一种AI赋能就是用飞书通信通道和Nanobot进行自然语言的交流操作。

Arduino UNO Q 板载 Nanobot 自动化编程指南之二图3

二,Nanobot的调试
1,掉电后的自启动。

  1. 设置 Nanobot 自启动
  2. 方法:使用 systemd 用户服务
  3.   1. 创建服务文件
  4.   mkdir -p ~/.config/systemd/user
  5.   nano ~/.config/systemd/user/nanobot-gateway.service
  6.   2. 写入以下内容
  7.   [Unit]
  8.   Description=Nanobot Gateway Service
  9.   After=network-online.target
  10.   Wants=network-online.target
  11.   [Service]
  12.   Type=simple
  13.   WorkingDirectory=/home/arduino/nanobot
  14.   ExecStart=/home/arduino/nanobot/.venv/bin/nanobot gateway
  15.   Restart=always
  16.   RestartSec=10
  17.   TimeoutStartSec=30
  18.   TimeoutStopSec=10
  19.   # 环境变量
  20.   Environment="PATH=/home/arduino/nanobot/.venv/bin:/usr/local/bin:/usr/bin:/bin"
  21.   # 安全设置
  22.   NoNewPrivileges=yes
  23.   ProtectSystem=strict
  24.   ReadWritePaths=/home/arduino /tmp
  25.   # 日志
  26.   StandardOutput=journal
  27.   StandardError=journal
  28.   [Install]
  29.   WantedBy=default.target
  30.   3. 启用并启动服务
  31.   # 重新加载 systemd 配置
  32.   systemctl --user daemon-reload
  33.   # 启用自启动
  34.   systemctl --user enable nanobot-gateway
  35.   # 立即启动
  36.   systemctl --user start nanobot-gateway
  37.   # 查看状态
  38.   systemctl --user status nanobot-gateway
  39.   4. 允许用户服务在登录前启动(可选)
  40.   如果需要系统启动时就运行(不必等用户登录):
  41.   sudo loginctl enable-linger $USER   
复制代码

  

2,Nanobot的项目结构

  1. Nanobot 项目结构
  2.   nanobot/
  3.   ├── nanobot/                    # 核心模块
  4.   │   ├── __main__.py             # 入口点: python -m nanobot
  5.   │   │
  6.   │   ├── cli/                    # 命令行接口
  7.   │   │   └── commands.py         # Typer CLI 命令
  8.   │   │
  9.   │   ├── agent/                  # Agent 核心
  10.   │   │   ├── loop.py             # 主循环引擎 (接收消息→构建上下文→调用LLM→执行工具→返回响应)
  11.   │   │   ├── context.py          # 上下文构建器
  12.   │   │   ├── memory.py           # 记忆存储
  13.   │   │   ├── subagent.py         # 子代理管理
  14.   │   │   ├── skills.py           # 技能系统
  15.   │   │   └── tools/              # 内置工具
  16.   │   │       ├── filesystem.py   # 文件操作
  17.   │   │       ├── shell.py        # 命令执行
  18.   │   │       ├── web.py          # 网络搜索/抓取
  19.   │   │       ├── spawn.py        # 子进程
  20.   │   │       ├── knowledge.py    # 知识库
  21.   │   │       └── ...
  22.   │   │
  23.   │   ├── channels/               # 消息渠道
  24.   │   │   ├── base.py             # 渠道基类
  25.   │   │   ├── manager.py          # 渠道管理器
  26.   │   │   ├── feishu.py           # 飞书/Lark
  27.   │   │   ├── dingtalk.py         # 钉钉
  28.   │   │   ├── mochat.py           # MoChat (微信)
  29.   │   │   ├── qq.py               # QQ
  30.   │   │   └── email.py            # 邮件
  31.   │   │
  32.   │   ├── config/                 # 配置系统
  33.   │   │   ├── schema.py           # Pydantic 配置模型
  34.   │   │   └── loader.py           # 配置加载器
  35.   │   │
  36.   │   ├── providers/              # LLM 提供商
  37.   │   │   ├── base.py             # 基类
  38.   │   │   ├── litellm_provider.py # LiteLLM 适配
  39.   │   │   ├── registry.py         # 提供商注册表
  40.   │   │   └── ...
  41.   │   │
  42.   │   ├── bus/                    # 消息总线
  43.   │   │   ├── queue.py            # 消息队列
  44.   │   │   └── events.py           # 事件定义
  45.   │   │
  46.   │   ├── session/                # 会话管理
  47.   │   │   └── manager.py          # 会话管理器
  48.   │   │
  49.   │   ├── skills/                 # 技能扩展
  50.   │   │   ├── weather/            # 天气查询
  51.   │   │   ├── github/             # GitHub 操作
  52.   │   │   ├── summarize/          # 文本摘要
  53.   │   │   ├── memory/             # 记忆管理
  54.   │   │   ├── cron/               # 定时任务
  55.   │   │   ├── tmux/               # 终端复用
  56.   │   │   └── ...
  57.   │   │
  58.   │   ├── knowledge/              # 知识库
  59.   │   │   ├── base.py             # 基类
  60.   │   │   └── local_kb.py         # 本地知识库
  61.   │   │
  62.   │   ├── heartbeat/              # 心跳服务
  63.   │   │   └── service.py          # 定时心跳检测
  64.   │   │
  65.   │   ├── cron/                   # 定时任务
  66.   │   │
  67.   │   └── utils/                  # 工具函数
  68.   │
  69.   ├── .venv/                      # Python 虚拟环境
  70.   ├── pyproject.toml              # 项目依赖
  71.   ├── start_gateway.sh            # 启动脚本
  72.   └── nanobot_arch.png            # 架构图
复制代码


3,删减改查
在确保核心组件和功能前提下,对原来程序进行删减,把原有条目总数量压缩一半。

Arduino UNO Q 板载 Nanobot 自动化编程指南之二图5

Nanobot 的网络搜索实现在 /home/arduino/nanobot/nanobot/agent/tools/web.py

(1}. WebSearchTool (网络搜索)
支持两种搜索方式:
a,百度千帆搜索 API(默认);
b,通义千问内置搜索(模型自带);

(2). WebFetchTool (网页抓取)
抓取 URL 并提取内容:
- 使用 Readability 提取正文
- 支持 HTML → Markdown 转换
- 支持 JSON 直接解析

4,通信通道和QQ邮箱
笔者只用了飞书的通信通道,网上教程很多,限于篇幅关系就不展开讲。
QQ邮箱要接收Nanobot的劳动成果,设置中要QQ邮箱授权码:
登录 QQ邮箱网页版,设置 → 账户 → POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,生成授权码。

5,Nanobot的自我学习,修复和管理
知识库和clawhub skill是笔者独特之处。使Nanobot对专项知识有理论的支撑。
Arduino UNO Q 板载 Nanobot 自动化编程指南之二图4


  1. 一、Memory 记忆系统(核心学习机制)
  2.   位置: /home/arduino/nanobot/nanobot/agent/memory.py
  3.   双层记忆架构
  4.   ~/.nanobot/workspace/memory/
  5.   ├── MEMORY.md     # 长期记忆(始终加载到上下文)
  6.   └── HISTORY.md    # 事件日志(grep 搜索,不自动加载)
  7.   自动整合机制
  8.   class MemoryStore:
  9.       async def consolidate(self, session, provider, model, ...):
  10.           """
  11.           1. 提取旧对话
  12.           2. 调用 LLM 分析,提取关键信息
  13.           3. 更新 MEMORY.md(长期事实)
  14.           4. 追加到 HISTORY.md(事件日志)
  15.           """
  16.   工作流程:
  17.   1. 当会话消息超过阈值时自动触发
  18.   2. LLM 分析对话内容
  19.   3. 通过 save_memory 工具调用保存结果
  20.   4. 更新 session.last_consolidated 标记
  21.   触发条件:
  22.   - 消息数超过 memory_window // 2
  23.   - 或显式调用 archive_all=True
  24.   ---
  25.   二、Heartbeat 心跳服务(自我唤醒)
  26.   位置: /home/arduino/nanobot/nanobot/heartbeat/service.py
  27.   class HeartbeatService:
  28.       """定期唤醒 agent 检查任务"""
  29.       async def _tick(self):
  30.           """
  31.           1. 读取 HEARTBEAT.md
  32.           2. LLM 决定是否有任务需要执行
  33.           3. 有任务则执行并通过通知渠道返回结果
  34.           """
  35.   配置:
  36.   {
  37.     "gateway": {
  38.       "heartbeat": {
  39.         "enabled": true,
  40.         "interval_s": 1800  // 30分钟
  41.       }
  42.     }
  43.   }
  44.   HEARTBEAT.md 示例:
  45.   # Heartbeat Tasks
  46.   ## Periodic
  47.   - 每2小时检查新闻并发送摘要
  48.   - 每天检查系统状态
  49.   ## One-time
  50.   - 2026-03-25 10:00 提醒会议
  51.   ---
  52.   三、Knowledge Base 知识库
  53.   位置: /home/arduino/nanobot/nanobot/knowledge/
  54.   功能:
  55.   - 创建/删除知识库
  56.   - 添加文档/文件
  57.   - 语义搜索
  58.   工具调用:
  59.   # 创建知识库
  60.   knowledge_base_create(name="project-docs", description="项目文档")
  61.   # 添加文档
  62.   knowledge_base_add(kb_id="xxx", content="...", metadata={...})
  63.   # 搜索
  64.   knowledge_base_search(kb_id="xxx", query="如何配置...", top_k=5)
  65.   ---
  66.   四、Skill 技能系统(自我扩展)
  67.   1. skill-creator(创建技能)
  68.   位置: /home/arduino/nanobot/nanobot/skills/skill-creator/
  69.   技能结构:
  70.   skill-name/
  71.   ├── SKILL.md          # 必需:技能说明 + YAML frontmatter
  72.   ├── scripts/          # 可选:脚本文件
  73.   ├── references/       # 可选:参考文档
  74.   └── assets/           # 可选:资源文件
  75.   创建流程:
  76.   # 1. 初始化
  77.   scripts/init_skill.py my-skill --path ~/.nanobot/workspace/skills
  78.   # 2. 编辑 SKILL.md
  79.   # 3. 打包
  80.   scripts/package_skill.py my-skill
  81.   2. clawhub(技能仓库)
  82.   搜索安装技能:
  83.   # 搜索
  84.   npx --yes clawhub@latest search "web scraping" --limit 5
  85.   # 安装
  86.   npx --yes clawhub@latest install <slug> --workdir ~/.nanobot/workspace
  87.   ---
复制代码
(待续)

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

本版积分规则

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

硬件清单

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

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

mail