Skip to main content

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:
The handler receives a 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

The Pico W cannot reach the MQTT broker. Check:
  • WiFi is connected (wlan.isconnected() returns True)
  • DNS resolution works (try socket.getaddrinfo("mqtt.heylua.ai", 443))
  • No firewall blocking port 443 (WebSocket)
Authentication failed. Verify:
  • agent_id matches your agent exactly
  • api_key is valid and not expired
  • device_name contains only lowercase letters, numbers, and hyphens
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.py to .mpy bytecode with mpy-cross to save RAM
Ensure:
  • device.connect() completed without error
  • You are calling device.run() or manually calling device._client.check_msg() in a loop
  • The command name in your handler matches the name the agent is using
Some MicroPython builds have limited TLS support. Try:
  • Update to the latest MicroPython firmware
  • Set use_ssl=False temporarily 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