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
| Component | Minimum | Recommended |
|---|---|---|
| Board | Raspberry Pi Pico W | Any MicroPython board with WiFi |
| RAM | 264 KB | 512 KB+ |
| Flash | 2 MB | 2 MB |
| MicroPython | v1.20+ | Latest stable |
| Network | WiFi (2.4 GHz) | WiFi with stable connection |
The client depends on
umqtt.robust (preferred) or umqtt.simple, both included in standard MicroPython firmware for the Pico W.Setup
Flash MicroPython firmware
Download the latest MicroPython UF2 from micropython.org and flash it to your Pico W.
LuaDevice Class Reference
Constructor
| Parameter | Type | Default | Description |
|---|---|---|---|
agent_id | str | required | Agent ID to connect to |
api_key | str | required | API key for authentication |
device_name | str | required | Unique name for this device |
server | str | required | MQTT broker hostname (e.g., mqtt.heylua.ai) |
port | int | 8883 | MQTT broker port |
group | str | None | Optional device group name |
use_ssl | bool | True | Enable TLS encryption |
Methods
| Method | Description |
|---|---|
connect() | Connect to the MQTT broker. Sets up LWT and subscriptions. |
disconnect() | Gracefully disconnect. Publishes offline status first. |
run(check_interval_ms=100) | Main loop. Blocks forever, checks for messages and sends heartbeats. |
trigger(name, payload=None) | Fire a trigger event to the agent. |
on_command(name, handler) | Register a command handler (non-decorator style). |
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", 8883)) - No firewall blocking port 8883
MQTTException: 5 (Not authorized)
MQTTException: 5 (Not authorized)
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

