🛠️ Raspberry Pi Pico W IoT Guide: Generalized Firmware for Home Automation
A privacy-first, reproducible firmware for Raspberry Pi Pico W—designed for seamless Home Assistant integration and scalable IoT projects.
📋 Table of Contents
🔍 High-Level Overview
This Raspberry Pi Pico W firmware creates a generalized IoT agent for home automation. Instead of hardcoding behavior, each device retrieves GPIO configuration from a database via API, based on its physical setup. The same firmware runs across all Pico W devices, reducing maintenance and enabling scalable, privacy-respecting automation.
📚 Pico W Firmware Workflow
The firmware follows a streamlined process to enable IoT functionality with Raspberry Pi Pico W:
- Connect to Wi-Fi: Attempts connection to configured networks.
- Register with Environment: Sends device metadata to an API to identify itself.
- Retrieve GPIO Configuration: Downloads pin mappings defining inputs (e.g., sensors) and outputs (e.g., relays).
- Read Inputs: Monitors sensor values and publishes to an MQTT topic for Home Assistant.
- Listen for Output Instructions: Subscribes to MQTT topics for commands.
- Apply Outputs: Executes instructions on configured GPIO outputs.
- Publish Acknowledgment: Confirms output actions via MQTT for Home Assistant feedback.
📄 Sample Raspberry Pi Pico W Firmware Log
This log demonstrates the Pico W’s startup, registration, GPIO setup, and MQTT communication for IoT integration.
View Sample Log Output
Script starting...
Trying to connect to ccc-home-IoT...
Waiting for connection to ccc-home-IoT... 0
Waiting for connection to ccc-home-IoT... 1
Waiting for connection to ccc-home-IoT... 2
Waiting for connection to ccc-home-IoT... 3
Waiting for connection to ccc-home-IoT... 4
Trying to connect to homatica...
Waiting for connection to homatica... 0
Waiting for connection to homatica... 1
Waiting for connection to homatica... 2
Waiting for connection to homatica... 3
Connected, IP: 192.168.1.252
Time set took 62ms
set_time took 9200ms
Loop start: Free memory 179312, Time 5501211
Registration payload: {'prev_outputs': {}, 'mac': '28:cd:c1:0e:c0:db', 'input_vals': {}, 'mqtt_input_topic': '', 'iscon': True, 'isreged': False, 'logerrors': False, 'ssid': 'homatica', 'ip': '192.168.1.252', 'hasioconf': False, 'device_id': None, 'printdebug': None, 'printerr': True, 'device_type': 'pico_w', 'name': 'I need a name', 'mqtt_output_topic': ''}
Free memory before register: 178240
Register request took 77ms
Registration Response: {'results': {'device_id': 8, 'refrsh_int': 1, 'name': 'Testing pico', 'mqtt_output_topic': 'testing/pico/outputs', 'mqtt_acked_output_topic': 'test', 'mqtt_input_topic': 'testing/pico/inputs', 'device_grp': 'test'}, 'status': '200 Ok'}
Register took 121ms
Registered: {'device_name': 'Testing pico', 'printerr': True, 'name': 'Testing pico', 'device_type': 'pico_w', 'printdebug': None, 'logerrors': False, 'mqtt_output_topic': 'testing/pico/outputs', 'mqtt_acked_output_topic': 'test', 'ssid': 'homatica', 'mac': '28:cd:c1:0e:c0:db', 'device_grp': 'test', 'refrsh_int': 1, 'isreged': True, 'mqtt_input_topic': 'testing/pico/inputs', 'ip': '192.168.1.252', 'hasioconf': False, 'iscon': True, 'prev_outputs': {}, 'device_id': 8, 'input_vals': {}}
Free memory before get_io_config: 178064
Get IO config request took 55ms
Received io_conf: {'{"SCL":9, "SDA":8}': {'io_type': 'AHT20', 'io_mode': 'IN', 'ioid': '{"SCL":9, "SDA":8}', 'io_name': 'Temperature and Humidity'}, '4': {'io_type': 'ADC', 'io_mode': 'IN', 'ioid': '4', 'io_name': 'Internal Temperature'}, '25': {'io_type': 'LED', 'io_mode': 'OUT', 'ioid': '25', 'io_name': 'LED'}}
AHT20 init command sent successfully on attempt 1
Initialized AHT20 on SCL=9, SDA=8
Initialized ADC on pin 4
Initialized LED on pin 25 as OUTPUT
Get IO config took 340ms
Got IO Config: {'device_name': 'Testing pico', 'printerr': True, 'name': 'Testing pico', 'device_type': 'pico_w', 'printdebug': None, 'logerrors': False, 'mqtt_output_topic': 'testing/pico/outputs', 'mqtt_acked_output_topic': 'test', 'ssid': 'homatica', 'mac': '28:cd:c1:0e:c0:db', 'device_grp': 'test', 'refrsh_int': 1, 'isreged': True, 'mqtt_input_topic': 'testing/pico/inputs', 'ip': '192.168.1.252', 'hasioconf': True, 'iscon': True, 'prev_outputs': {}, 'device_id': 8, 'input_vals': {}}
MQTT client reconnected with ID: 28cdc10ec0db
MQTT setup took 171ms
MQTT connected and subscribed to outputs
Read IO {"SCL":9, "SDA":8} took 82ms
Read IO 4 took 0ms
Read input vals took 255ms
Published inputs: {'{"SCL":9, "SDA":8}': {'io_duty': '', 'io_v': {'humidity': 57.37753, 'temperature': 25.27618}, 'io_name': 'Temperature and Humidity', 'io_type': 'AHT20', 'io_freq': '', 'ioid': '{"SCL":9, "SDA":8}', 'io_mode': 'IN'}, '4': {'io_duty': '', 'io_v': 30.32764, 'io_name': 'Internal Temperature', 'io_type': 'ADC', 'io_freq': '', 'ioid': '4', 'io_mode': 'IN'}}
Publish took 7ms
Availability publish took 1ms
Published availability: online
🛡️ Privacy by Design
All data stays local with no cloud dependencies or vendor lock-in. This Raspberry Pi Pico W firmware respects your network boundaries and integrates with Home Assistant using open MQTT protocols. You control the logic, data, and infrastructure.
🧩 Dependencies & Setup for Pico W IoT
The firmware requires these components for Raspberry Pi Pico W IoT projects:
- SQL Database: Stores device metadata and GPIO configurations.
- API Server: Manages device registration and configuration delivery.
- MQTT Broker: Enables real-time communication with Home Assistant.
Setup files and schema:
- iot_api.py – API endpoints for registration and config
- config.yaml – Routing and environment settings
- DB_Tables.txt – SQL schema for device and GPIO tables
🗃️ Database Schema & Samples for IoT
Devices Table
CREATE TABLE `devices` (
`eid` int(11) NOT NULL AUTO_INCREMENT,
`mac` varchar(50),
`ip` varchar(15),
`name` varchar(50),
`device_type` varchar(50),
`updated` datetime,
`last_seen` datetime,
`refrsh_int` int(11),
`device_grp` varchar(50),
`mqtt_input_topic` varchar(255),
`mqtt_output_topic` varchar(255),
`mqtt_acked_output_topic` varchar(255),
PRIMARY KEY (`eid`)
);
Sample Entry
eid: 2
mac: 28:cd:c1:0c:63:ce
ip: 192.168.1.157
name: BedRoom Thermostat
device_type: pico_w
updated: 2025-09-01 15:31:41
last_seen: 2025-09-23 18:39:21
refrsh_int: 5
device_grp: HVAC
mqtt_input_topic: hvac/bedroom/inputs
mqtt_output_topic: hvac/bedroom/outputs
mqtt_acked_output_topic: hvac/bedroom/outputs/acked
Device IO Config Table
CREATE TABLE `device_io_config` (
`eid` int(11) NOT NULL AUTO_INCREMENT,
`device_id` int(11),
`ioid` varchar(555),
`io_name` varchar(50),
`io_description` varchar(255),
`io_type` varchar(20),
`io_mode` varchar(10),
`updated` datetime,
PRIMARY KEY (`eid`)
);
Sample Entries
eid: 3
device_id: 2
ioid: 25
io_name: LED
io_description: LED
io_type: LED
io_mode: OUT
updated: 2025-08-01 17:23:40
eid: 4
device_id: 2
ioid: 4
io_name: Internal Temperature
io_description: Internal Temperature
io_type: ADC
io_mode: IN
updated: 2025-08-01 17:23:40
eid: 5
device_id: 2
ioid: 22
io_name: Ambient Temperature
io_description: Ambient Temperature
io_type: DS18x20
io_mode: IN
updated: 2025-08-15 16:54:45
eid: 6
device_id: 8
ioid: {"SCL":9, "SDA":8}
io_name: Temperature and Humidity
io_description: Temperature and Humidity
io_type: AHT20
io_mode: IN
updated: 2025-09-30 12:54:45
🏡 Home Assistant Integration with Pico W
Using MQTT and REST APIs, the Raspberry Pi Pico W becomes a fully compatible Home Assistant node. You can:
- Display sensor data in dashboards
- Trigger automations based on input states
- Send output commands via scripts or UI
- Receive acknowledgments for output actions
This makes the Pico W a low-cost, transparent alternative to commercial IoT devices.
🚀 Getting Started with Pico W IoT
Step 1: Flash Firmware
Flash main.py to your Raspberry Pi Pico W to enable IoT functionality.
Step 2: Set Up API and Database
Configure the API server and SQL database using the provided files.
Step 3: Connect to Wi-Fi and MQTT
Establish Wi-Fi and MQTT broker connections for real-time communication.
Step 4: Install MQTT Client
- Create a folder named
umqttin the/libdirectory on your Pico W. - Copy
simple.py(and optionallyrobust.py) into/lib/umqtt/. - Verify firmware includes:
from umqtt.simple import MQTTClient.
Step 5: Register Device
Register the device with the API to begin automating.
Need help? Visit Homatica’s contact page or check our blog for IoT tutorials.
🌱 Homatica’s Philosophy
This project reflects Homatica’s ethos: build with intention, clarity, and respect for privacy. Using open protocols and reproducible logic, you control your home automation.
❓ FAQ: Common Pico W IoT Questions
How do I troubleshoot Wi-Fi connection issues?
Check your SSID/password in config.yaml, ensure signal strength, and review the firmware log for errors.
Can I use this firmware with other IoT platforms?
Yes, any platform supporting MQTT (e.g., Node-RED) can integrate with this firmware.
What GPIO pins are supported?
The firmware supports all Pico W GPIO pins, configurable via the database for inputs (e.g., ADC, sensors) or outputs (e.g., LEDs, relays).