Hardware List
Setup with Thonny
1
Install Thonny IDE
Download Thonny for your operating system. Thonny has built-in MicroPython support for the Pico W.
2
Flash MicroPython firmware
- Hold the BOOTSEL button on the Pico W
- While holding, connect the USB cable to your computer
- Release BOOTSEL — the Pico appears as a USB drive
- Download the latest
.uf2firmware from micropython.org/download/RPI_PICO_W - Drag the
.uf2file onto the Pico USB drive - The Pico reboots automatically with MicroPython
3
Configure Thonny for Pico W
- Open Thonny
- Go to Tools > Options > Interpreter
- Select MicroPython (Raspberry Pi Pico)
- Select the correct port (usually auto-detected)
- Click OK
- You should see the MicroPython REPL in the bottom panel
4
Upload the Lua device library
- Open
lua_device.pyin Thonny - Go to File > Save as…
- Select Raspberry Pi Pico as the target
- Save as
lua_device.pyon the Pico
5
Configure WiFi credentials
Create a
config.py file on the Pico with your WiFi and Lua credentials:6
Create main.py
Create the main application file. This runs automatically when the Pico boots:
7
Run and test
- Click the green Run button in Thonny (or press F5)
- You should see:
- Open
lua chatin your terminal and try:
File Structure on Pico
After setup, the Pico should have these files:Troubleshooting
Pico does not appear as USB drive
Pico does not appear as USB drive
- Make sure you are holding BOOTSEL before plugging in the USB cable
- Try a different USB cable (some are charge-only, not data)
- Try a different USB port on your computer
WiFi connection fails
WiFi connection fails
- Pico W only supports 2.4 GHz WiFi (not 5 GHz)
- Check SSID and password in
config.py(case-sensitive) - Move the Pico closer to the router
- Check
wlan.status()for error codes: -1 = connection failed, -2 = no matching SSID, -3 = auth failed
MQTT connection fails
MQTT connection fails
- Verify
agent_idandapi_keyinconfig.py - Check WiFi is connected first (
wlan.isconnected()) - Ensure port 443 (WebSocket) is not blocked by your network
- Try
device = LuaDevice(..., use_ssl=False)temporarily for debugging
MemoryError during operation
MemoryError during operation
- Add
import gc; gc.collect()periodically in your code - Compile
lua_device.pyto bytecode:mpy-cross lua_device.py, then copylua_device.mpy - Reduce payload sizes in command responses
- Keep the dedup cache small (it auto-cleans at 100 entries)
Device connects but commands do not work
Device connects but commands do not work
- Check the REPL output for error messages
- Verify command names match between the handler and what the agent expects
- Ensure
device.run()is being called (it is the message processing loop) - Check that
device.connect()completed without errors
DHT22 sensor not reading
DHT22 sensor not reading
- Check wiring: data pin to GP15, 10k pull-up resistor between data and 3.3V
- DHT22 requires at least 2 seconds between readings
- Try
dht_sensor.measure()in the REPL first to verify
Next Steps
MicroPython Client
Full LuaDevice class reference
Factory Monitor
Industrial monitoring example on Pico W
MQTT Transport
MQTT topic structure and QoS details
Triggers
Send events from the Pico W to your agent

