Skip to main content
You’ll build a device on a Raspberry Pi Pico W that the agent can ask for its temperature and whose LED it can switch and blink, over Wi-Fi and MQTT, with no extra hardware. You need a Pico W (the W variant has Wi-Fi), a data-capable micro-USB cable, a 2.4 GHz network, Python 3.8+ on your computer, and an agent created with lua init. Verified against lua-cli 3.45.0.

What you’ll build

  • A Pico W running the MicroPython client as pico-sensor.
  • Four commands: led_on, led_off, blink, and read_temperature.
  • Credentials kept in a config.py on the board, out of version control.
1

Flash MicroPython

Hold the BOOTSEL button while plugging the Pico in; it mounts as a USB drive. Download the Pico W .uf2 from micropython.org and copy it onto the drive. The board reboots into MicroPython.
2

Copy the client to the board

The client ships inside the lua-device-client package on PyPI. Install it and mpremote on your computer, then copy the file to the board as lua_device.py.
3

Create the credential and config

From the agent project, issue a device credential for the device name pico-sensor. It needs an email sign-in (lua auth configure, then choose Email) and grants commands and triggers by default.
Copy LUA_AGENT_ID and LUA_DEVICE_CREDENTIAL from that file into a config.py beside your Wi-Fi settings, then copy it to the board; keep both files out of version control.
config.py
4

Write main.py

main.py runs at boot. The client joins Wi-Fi from the constructor arguments, and run() blocks while handling commands and heartbeats.
main.py
5

Run it

Start the program with the REPL attached so you can read the client’s log lines; after this, it also runs on every power-up.
The client logs these lines as it connects; the text comes from the client source, not from a recorded run.
Client log
MQTT CONNACK error: rc=5 means the credential, agent ID, or device name don’t match the binding, or the credential is a scoped API key from the dashboard rather than a device credential.
6

Ask the agent

From the agent project, drive the board. The model calls device__pico_sensor__blink with { "times": 5 } and device__pico_sensor__read_temperature.
lua devices status --device-name pico-sensor --ci prints 🟢 Device 'pico-sensor' is online, and lua devices test --device-name pico-sensor --command read_temperature --ci prints the board’s reading. Unplug the board: once the broker stops hearing from it, within 1.5 times the 60-second MQTT keep-alive, it publishes the device’s will and the state changes to offline.
Leave the board connected for more than 24 hours and the agent loses the four command tools until the board reconnects: the platform keeps a device’s command list for 24 hours from connect, and heartbeats don’t extend it. run() reconnects only after an error, so power-cycle the board daily or replace run() with a loop of your own that calls disconnect() and connect() before the day is up.

What you learned

  • The MicroPython client is one file copied from the PyPI package. See MicroPython device client.
  • Commands declared with @device.command become tools while the board is connected; an inputSchema tells the model what to send. See Expose device commands as tools.
  • The board talks MQTT over WebSocket on port 443, so it works behind most firewalls. See MQTT protocol.

Next steps

Factory monitor

Add sensors and fire triggers from the Pico W.

Handle device triggers

Run code on the platform when the board reports an event.