Skip to main content
You’ll build a device on a Raspberry Pi Pico W that watches a machine: the agent can read its sensors and trip an emergency-stop relay, and the board itself fires vibration_anomaly when readings cross a threshold, which a handler on the platform stores and hands to the model. You need a Pico W set up as in Run a device on a Raspberry Pi Pico W, an analog vibration sensor on GP26, a temperature sensor on GP27, and a relay on GP16; without sensors the code still runs with noise readings. Verified against lua-cli 3.45.0.

What you’ll build

  • A MicroPython device cnc-monitor-01 with read_vibration, read_temperature, and emergency_stop.
  • A main loop that polls, heartbeats, and fires vibration_anomaly every 10 seconds when needed.
  • A defineDeviceTrigger handler that stores the event and invokes the agent.
1

Create the credential

From the agent project, issue a device credential for device name cnc-monitor-01; the default operations, commands and triggers, are what the monitor needs. Copy LUA_AGENT_ID and LUA_DEVICE_CREDENTIAL from the file into config.py on the board together with the Wi-Fi settings, as in the Pico W tutorial.
2

Write main.py

The loop mirrors what run() does, so the periodic anomaly check can sit beside command polling: poll messages, heartbeat every 30 seconds, prune the duplicate cache, reconnect on any error. It reaches into the client’s underscore-prefixed members, which a later client version may rename.
main.py
emergency_stop trips the relay whenever the agent calls it, from any channel the agent is connected to; the description and persona steer the model but don’t gate the call. Add a hardware or firmware interlock if a wrong call is dangerous.
3

Handle the trigger on the agent

The handler stores every anomaly and asks the model to assess it; device.name identifies the board.
src/devices/VibrationAnomaly.ts
4

Register and publish

Register the trigger on the agent, then push it live.
src/index.ts
5

Run and verify

Copy main.py to the board and start it, then read the sensors from chat and watch for the trigger.
Tap the vibration sensor hard enough to cross 3 g: within 10 seconds the board fires the trigger and the log shows the handler’s run.

What you learned

  • trigger() on MicroPython returns at once; the acknowledgment arrives on the trigger_ack topic and is printed. See MicroPython device client.
  • A standalone defineDeviceTrigger matches the name exactly and runs with { device: { name } }. See Handle device triggers.
  • Triggers are limited to 10 per second per agent, so sample on an interval rather than on every reading. See Reliability and limits.

Next steps

Warehouse station

The same trigger pattern from a Node device.

MQTT protocol

The topics this board publishes and subscribes to.