Overview
The MicroPython client (lua_device.py) runs on microcontrollers with as little as 264KB of RAM. It uses MQTT natively and supports the full device protocol: commands, triggers, heartbeats, reconnection, and idempotency dedup.
Hardware Requirements
The client depends on
umqtt.robust (preferred) or umqtt.simple, both included in standard MicroPython firmware for the Pico W.Setup
1
Flash MicroPython firmware
Download the latest MicroPython UF2 from micropython.org and flash it to your Pico W.
2
Copy the client library
Copy
lua_device.py to your Pico W. You can use Thonny, mpremote, or rshell:3
Connect to WiFi
Create a
main.py that connects to WiFi before creating the device:4
Create your device
Add the device setup after WiFi connection:
5
Register commands and run
Register command handlers and start the main loop:
LuaDevice Class Reference
Constructor
Methods
The @device.command Decorator
The preferred way to register command handlers:
payload dict and must return a dict (or None). If the handler raises an exception, the error message is sent back to the agent.
Complete Example: LED + DHT22
A Pico W that controls an onboard LED and reads a DHT22 temperature/humidity sensor:Troubleshooting
OSError: [Errno 110] ETIMEDOUT
OSError: [Errno 110] ETIMEDOUT
The Pico W cannot reach the MQTT broker. Check:
- WiFi is connected (
wlan.isconnected()returnsTrue) - DNS resolution works (try
socket.getaddrinfo("mqtt.heylua.ai", 443)) - No firewall blocking port 443 (WebSocket)
MemoryError or device freezes
MemoryError or device freezes
The Pico W has limited RAM. Try:
- Reduce
_dedup_ttl(default 300 seconds) if you are processing many commands - Avoid large payloads in command responses
- Use
gc.collect()periodically in your main loop - Compile
lua_device.pyto.mpybytecode withmpy-crossto save RAM
Commands not received
Commands not received
Ensure:
device.connect()completed without error- You are calling
device.run()or manually callingdevice._client.check_msg()in a loop - The command name in your handler matches the name the agent is using
SSL handshake fails
SSL handshake fails
Some MicroPython builds have limited TLS support. Try:
- Update to the latest MicroPython firmware
- Set
use_ssl=Falsetemporarily for debugging (not recommended in production)
Next Steps
Pico W Setup Guide
Step-by-step hardware setup with photos and wiring
Industrial Sensor Example
Complete factory monitoring example on Pico W
MQTT Transport
MQTT topic structure and QoS details
Triggers
Send events from your device to the agent

