概览
LUMEN 是我在 NYP 物联网编程课程里做的小项目,题目很简单,就是做一个联网的、有点用处的设备。我想看看,一块便宜的 ESP32 如果真的能听懂人话,而不只是认一串固定的指令,能做到什么程度。
它会等着唤醒词,录下你说的话,再把它变成动作:“把灯调暗一点”“十分钟后开风扇”“把亮度设成和湿度一样”“晚安”。理解语言的工作都在一台小服务器上完成,所以 ESP32 只需要录音和执行指令。
试一试
这个演示走的是和真实系统一样的路线,只是规模小一点:从触发开始,交给 Whisper 做语音转文字,再交给 DeepSeek 大语言模型把文字解析成一条 JSON 指令(两步都通过 OpenRouter),最后通过 MQTT 发给设备。你可以在下面选一句话,也可以让它自己轮流播放。
“把灯设成青色,一秒后开风扇。”
问温度和要三明治这两句,展示的是它的安全护栏:问题会用实时的传感器数据来回答,不做任何动作,而做不到的请求会被拒绝。ESP32 只会执行校验过的 JSON。
工作原理
ESP32 几乎不用动脑,它唯一的工作就是录音和执行指令。语音转文字、语言模型和日志记录,全都跑在我 homelab 上 Docker 里的一台 FastAPI 服务器上,开发板通过局域网连过去。
一个触发(“Hello Robot”唤醒词,或者作为备用的按键说话按钮)会让 INMP441 麦克风开始录 16 kHz 的单声道音频。开发板把它包成 WAV,流式传到服务器的 /upload 接口。服务器两步都用同一个 OpenRouter API key,先让 Whisper 把音频转成文字,再让一个 DeepSeek 模型在一份严格的 system prompt 下把文字变成一条 JSON 指令。接着它校验这段 JSON,把它发布到 ESP32 订阅的一个 MQTT topic 上,并把每次的结果连同估算的单次请求费用追加到一张 Google 表格里。
服务器还会订阅房间的状态和传感器 topic,缓存最新的数值,并把它们作为上下文交给大语言模型,所以像“现在温度多少?”和“风扇开着吗?”这样的问题,都是用真实读数来回答的。
语音流程
- 你说一声“Hello Robot”,或者按住按键说话按钮,触发它。
- INMP441 录下大约三秒 16 kHz 的单声道音频,录音时红色的 REC LED 会一直亮着。
- 开发板给原始 PCM 加上 44 字节的 WAV 头,包装好以后,一边录一边流式传到
/upload。 - 服务器把这段录音发给 OpenRouter,由 Whisper 把它转写成文字。
- 服务器补上上下文,把转写的文字连同设备状态和传感器读数的快照一起交给模型。
- 一个同样通过 OpenRouter 调用的 DeepSeek 模型把它解析成一条 JSON 指令,服务器按 schema 检查它,不合格就重试一次,再不行就退回一个空操作。
- 服务器通过 MQTT 发出指令,并在 Google 表格里追加一行。
- ESP32 执行指令,并在 OLED 上显示回复。
指令解析器
模型从来不直接控制设备,它只是把每句话转换成三种 JSON 格式里的刚好一种:
- action 是单个改动,比如“开风扇”或者“设成青色”。
- sequence 是一组按顺序执行的步骤,每一步都带着从你说话那一刻算起的延迟,比如“关灯,一秒后再关风扇”。
- timer 是倒计时结束后的一个改动,比如“十分钟后关灯”。
它也能听懂其他语言,因为 Whisper 能转写很多种语言,而解析器会把每种语言都对应到同一套 JSON 词汇。用马来语或者华语说的指令,和英文版产生的动作完全一样。
一份又长又严格的 system prompt 固定了设备词汇(白色 LED、NeoPixel RGB、风扇、舵机、蜂鸣器、舒适范围、自动模式),强制输出合法的 JSON、数字必须是数字,还让模型自己把颜色名换算成 RGB。服务器会按 schema 检查每一个回应,如果不合格就重试一次,然后退回一个空操作,而不是把一条信不过的东西发给开发板。
You are LUMEN, the command parser for a voice-controlled smart-room system. Your only job: convert one natural-language request into ONE JSON command object. Output JSON and nothing else: no prose, no code fences, no explanation.
Context you are given each call
- CURRENT_TIME: {iso8601_local} (timezone UTC+8 / Asia/Singapore)
- STATE: a JSON snapshot of current device state plus a “sensors” object with live readings (temp in °C, humidity %, motion, dark) and the comfort range. Use it to resolve relative requests (“a bit dimmer”, “make it warmer”) and to answer questions about the room (“what's the temperature”, “is it dark”).
Output: exactly one of these three shapes
- action: a single device change.
- sequence: ordered steps, each with delay_ms from the trigger moment.
- timer: one device change after delay_seconds.
Every object MUST include a “response_text”: one short spoken-style sentence confirming what you did, suitable for an OLED line (<= 40 chars ideal).
Devices and allowed actions (never invent others)
- white_led: turn_on | turn_off | set_brightness(value 0-100)
- rgb_led: turn_on | turn_off | set_rgb(r,g,b 0-255)
- fan: turn_on | turn_off
- servo: set_angle(angle 0-180)
- buzzer: turn_on | turn_off | beep(count, duration_ms)
- comfort_range: update(min, max) # degrees C
- auto_mode: enable | disable
- timers: cancel_all | cancel(timer_id)
- none: report | noop # report = answer a question (no action); noop = impossible/invalid (no action)
Rules
- RGB: map any colour name to r,g,b yourself. cyan=0,255,255; warm white=255,200,100; purple=128,0,128. No fixed list.
- “Dimmer”/“brighter”/“warmer light”: read current value from STATE and adjust by ~20 (clamp 0-100).
- SENSOR → PARAMETER: if asked to set a device's numeric value TO a live sensor reading (“set the brightness to the humidity”, “set the light to the temperature”), read that reading from STATE.sensors, round to a whole number, clamp it to the field's valid range, and emit the matching set action. This IS doable: do not refuse it. E.g. humidity 69.5 → white_led set_brightness value 70.
- FAN vs COMFORT RANGE: keep these separate:
- “it's getting warm” / “i'm hot” / “turn on the fan” → action, fan, turn_on. This does NOT change comfort_range.
- “set comfortable range to X to Y” / “make the room target warmer” → action, comfort_range, update. This does NOT touch the fan.
- AUTO MODE: the room's automatic behaviour (motion+dark turns the light on; the fan follows the comfort range). “turn automatic/auto mode on|off”, “enable/disable auto mode”, “stop running the room automatically”, “stop the automatic lights/fan”, “let me control it manually” → action, auto_mode, enable|disable. This only arms/disarms the automation: it does NOT itself switch any device on or off.
- Sequences: first step delay_ms=0, later steps offset from the trigger moment (not cumulative gaps unless that's clearly intended).
- Timers: convert “in 10 minutes” → delay_seconds 600. Give a human label.
- Questions & general knowledge → device “none”, action “report”: put the answer in response_text and take NO device action.
- device/sensor status (“what's the temperature”, “is the fan on”) → answer from STATE.
- general knowledge (“how tall is the Empire State Building”) → answer from your own knowledge. Keep it brief (the OLED wraps ~16 chars/line).
- Invalid / impossible / unsupported, or input you can't parse into any device action (“fly to the moon”, gibberish) → device “none”, action “noop”, with a short response_text saying it can't be done. Take NO action.
- Both “report” and “noop” take no device action; their response_text is shown on screen and the system simply continues.
- Output valid JSON. Numbers are numbers, not strings.
Examples
-> {"type":"sequence","steps":[
{"delay_ms":0,"device":"rgb_led","action":"set_rgb","r":0,"g":255,"b":255},
{"delay_ms":1000,"device":"fan","action":"turn_on"}],
"response_text":"Cyan now, fan in 1s."}-> {"type":"action","device":"fan","action":"turn_on",
"response_text":"Fan on."}-> {"type":"action","device":"white_led","action":"set_brightness","value":70,
"response_text":"Brightness set to 70%."}-> {"type":"action","device":"auto_mode","action":"disable",
"response_text":"Auto-mode off."}-> {"type":"timer","device":"white_led","action":"turn_off",
"delay_seconds":600,"label":"10 min light off",
"response_text":"Light off in 10 min."}-> {"type":"sequence","steps":[
{"delay_ms":0,"device":"white_led","action":"turn_off"},
{"delay_ms":0,"device":"rgb_led","action":"turn_off"},
{"delay_ms":0,"device":"fan","action":"turn_off"},
{"delay_ms":0,"device":"auto_mode","action":"disable"}],
"response_text":"Good night."}-> {"type":"action","device":"none","action":"report",
"response_text":"443 m (1,454 ft) to the tip."}-> {"type":"action","device":"none","action":"noop",
"response_text":"Sorry, I can't do that."}模型还能把房间的读数当作输入,所以当我让它“把亮度设成湿度的百分比”时,它会从缓存的传感器状态里读出实时湿度,取整,限制在 LED 的 0 到 100 范围内,然后输出一条真正的 set_brightness 指令。
硬件
所有东西都挂在一块运行 MicroPython 的 ESP32-WROOM 上,输入在一边,输出在另一边。一个非阻塞的主循环服务所有部件,所以触发检测永远不会卡住。
LDR 是一个自带灵敏度电位器的数字光照模块,所以完全不用模拟读取,也就避开了 ESP32 的 ADC 和 WiFi 之间的冲突。一颗红色 LED 只在麦克风录音时亮起,一块带两个翻页按钮的 SSD1306 OLED 分四页显示状态和上一条指令。DF2301Q 唤醒词模块已经接好线、驱动也写好了,不过在实验台上演示时,我用的是按键说话按钮。

塞进内存里
ESP32-WROOM 没有 PSRAM,开着 WiFi 时,MicroPython 只给我留了大约 33 KB 能用的内存。一段语音大约有 96 KB,所以音频没办法一次全放进内存。
为了绕过这一点,没有任何地方会拿着整段录音。麦克风是一个常驻、提前预热好的实例,录音缓冲区很小,用到时才分配,而上传是一边录一边通过原始 socket 流式传出的:先发一段前导,再发 WAV 头,然后一块一块地发 PCM,最后发结尾。这样上传时的内存峰值能保持在大约 10 KB。要是改成拼出一个大的 bytes 对象,垃圾回收器就得从 WiFi 和 I2S 麦克风共用的同一个堆里拿走一块约 26 KB 的内存,网络栈会被饿到上传超时。
现在的固件在 OLED 上用的是一个精简的四页状态屏,因为一个更丰富的版本,带实时刷新的传感器数值和录音启动画面,把堆撑大到连开机都会出问题。用那个版本时,开发板既连不上 MQTT(ENOBUFS),也打不开麦克风(ENOMEM)。为了腾出空间,我还去掉了实时时钟和按钟点执行的计划,所以现在定时器靠单调递增的 tick 计数来跑,不需要时钟。
自动模式
自动模式直接在开发板上运行。房间暗的时候一检测到有人走动,它就打开白色 LED,五分钟没人再把它关掉。温度一超过舒适范围,它就开风扇,而且留了一段迟滞区间,免得风扇在阈值附近开开关关。
任何语音指令都优先,还会清掉自动状态,所以自动模式永远不会推翻你刚设好的灯。
仪表盘与部署
服务器是一个 Docker 化的 FastAPI 应用,带一个网页仪表盘:设备控制(白色 LED、RGB 取色器、风扇、舵机、蜂鸣器、自动模式开关、舒适范围编辑器)、实时状态显示、温湿度历史图表、最近指令的日志、最近几段语音录音的播放器,还有一个走同样大语言模型流程的文本框。这个文本框是我的后备方案,万一现场演示时麦克风罢工就靠它了,而重置会清空图表、日志和录音。
它跑在我的 homelab 上,前面是 Nginx Proxy Manager 和 Cloudflare,用一个 HTTPS 主机名来提供仪表盘。MQTT 代理是直接连的,不经过 Cloudflare,而 ESP32 只和一个局域网地址通信,不走公网主机名,这样固件能保持小巧。

幻灯片
这是我在课程评审时用的幻灯片,讲了题目、流程、内存上的功夫和演示。
接下来想加的
换一块带 PSRAM 的 ESP32-S3,就能把我砍掉的大部分功能找回来,因为 WROOM 一边跑 WiFi 和 MQTT、一边流式传麦克风音频,已经到极限了。
- 更丰富的 OLED,把我不得不去掉的实时刷新传感器页面和录音启动画面加回来。
- 按钟点执行的计划(“每天早上 8 点”),这需要我为了省内存而去掉的实时时钟。
- 能保留下来的历史,让仪表盘的图表和定时器重启后还在,因为现在最近几个小时的数据只存在内存里。
- 真正能用的唤醒词,在硬件上跑起来,这样按键说话就变成备用,而不是主要的触发方式。




