From micro:bit to Linux: Adding MQTT to Our Tiny Sensor Data Pipeline
From micro:bit to Linux: Adding MQTT to Our Tiny Sensor Data Pipeline
A micro:bit takes a temperature reading. A Raspberry Pi Pico W receives it, adds some context, and passes it across the network. A Linux workstation cleans the incoming data and turns it into CSV, JSON, and SQLite.
That was our pipeline in Article #1.
Now we're going to change one part of it.
Instead of having the Pico W send each record directly to a Linux application with HTTP POST, we're going to put MQTT in the middle.
The important thing is what does not change. The micro:bit still measures. The Pico still receives and timestamps. Linux still processes the incoming records with Python and Pandas. We still end up with CSV, JSON, and SQLite.
We're changing the transport layer.
Our new pipeline looks like this:
micro:bit
↓
sensor measurement
↓
UART serial
↓
Pico W
↓
timestamp + JSON
↓
MQTT publish
↓
MQTT broker
↓
Linux subscriber
↓
Python + Pandas
↓
cleaned data
├── CSV
├── JSON
└── SQLite
Same data pipeline. One new piece.
What MQTT Adds
MQTT introduces three useful roles:
publisher
↓
broker
↓
subscriber
The Pico W becomes our publisher.
An MQTT broker receives the Pico's publications.
Our Linux Python program becomes a subscriber.
MQTT organizes messages using names called topics. For our temperature data, we might use:
sensors/microbit/temperature
The Pico publishes sensor records to that topic.
The Linux program subscribes to that topic.
This is different from the HTTP version of our project.
With HTTP, the relationship looked like this:
Pico W
↓
HTTP POST
↓
specific Linux endpoint
The Pico needed to know where to send the POST.
With MQTT, the relationship becomes:
Pico W
↓
publish
↓
MQTT broker
↓
subscribe
↓
Linux application
The publisher and subscriber do not communicate directly with each other. Both communicate with the broker and agree on the topic.
That difference is the reason we're adding MQTT.
The Hardware Hasn't Changed
The physical arrangement remains straightforward.
We have a BBC micro:bit collecting sensor data.
The micro:bit connects to a Raspberry Pi Pico W through UART serial.
The Pico W connects over Wi-Fi to the local network.
Our Linux workstation receives and processes the measurements.
MQTT hasn't required us to redesign the sensor side of the project.
The change happens in software, between the Pico and the Linux data-processing program.
Follow the Data Again
Let's start with exactly the same observation as Article #1.
The micro:bit measures:
23
It turns that measurement into a simple line of text:
23\n
UART carries it as bytes:
b"23\n"
The Pico receives those bytes, decodes the measurement, and adds a timestamp.
Conceptually, we now have:
2026-10-15T16:03:27,23
But just as in Article #1, we don't have to treat this as a CSV line internally.
The Pico can represent the observation as a Python dictionary:
record = {
"timestamp": timestamp,
"temperature": int(temperature)
}
That distinction is useful.
The information is still simply a time and a temperature reading. Its representation changes depending on what the next part of the pipeline requires.
For MQTT, we'll serialize that dictionary as JSON.
Conceptually:
Python dictionary
↓
JSON
↓
MQTT payload bytes
↓
broker
Code on the micro:bit
Nothing needs to change here.
The micro:bit still has the same small job:
- Read the sensor.
- Turn the reading into text.
- Send it through UART.
- Wait.
- Repeat.
Our program remains:
from microbit import *
uart.init(
baudrate=9600,
tx=pin0,
rx=pin1
)
while True:
temp = temperature()
uart.write(str(temp) + "\n")
sleep(5000)
The micro:bit doesn't know we replaced HTTP with MQTT.
It doesn't need to know.
It measures something and reports it.
That is still its entire responsibility.
Code on the Pico W
The beginning of the Pico's job also remains familiar:
UART IN
↓
decode reading
↓
add timestamp
↓
build record
Our record is still:
record = {
"timestamp": timestamp,
"temperature": int(temperature)
}
Now comes the change.
Instead of handing that dictionary to an HTTP library for a POST, we serialize the record as JSON and publish it with MQTT.
import ujson
from umqtt.simple import MQTTClient
client = MQTTClient(
"pico01",
"192.168.1.100"
)
client.connect()
topic = b"sensors/microbit/temperature"
payload = ujson.dumps(record)
client.publish(
topic,
payload.encode()
)
The important transformation is:
Python dictionary
↓
ujson.dumps()
↓
JSON string
↓
.encode()
↓
bytes
↓
MQTT publish
So our original observation might now travel through MQTT as JSON resembling:
{
"timestamp": 1792109007,
"temperature": 23
}
The Pico publishes those bytes under the topic:
sensors/microbit/temperature
In Article #1 the Pico pushed the record directly toward the Linux HTTP application.
Here the Pico publishes the record to the broker.
The MQTT Broker
This is the new component in our pipeline.
Pico W
↓
publish
↓
MQTT broker
↓
deliver
↓
subscriber
For a local Linux experiment, Eclipse Mosquitto is a convenient MQTT broker.
On Debian or Ubuntu it can be installed along with its command-line clients:
sudo apt install mosquitto mosquitto-clients
The broker's responsibility is not to clean our temperature readings or build DataFrames.
It handles MQTT messaging between publishers and subscribers.
That means our Pico doesn't have to know about the Python data-processing program itself.
It knows about:
broker
+
topic
Likewise, the subscriber doesn't have to communicate directly with the Pico. It subscribes to the broker for the topic it wants.
This gives us a useful kind of separation.
One Publisher, More Than One Subscriber
Here's where the broker model starts becoming interesting.
Our Python ingestion program can subscribe to:
sensors/microbit/temperature
But another program can subscribe too.
For example, while the Python application continues receiving measurements for Pandas, we could watch sensor traffic from a terminal using Mosquitto's command-line subscriber:
mosquitto_sub -t "sensors/#"
Conceptually:
┌──→ Python ingestion
│
Pico W → MQTT broker ┼──→ terminal monitor
│
└──→ another subscriber
The Pico hasn't changed.
It still publishes one measurement to one topic.
The broker can distribute matching publications to subscribers.
That is an important contrast with our original point-to-point HTTP design.
The Linux Workstation
Our Linux machine still does the heavier data work.
The difference is how the record arrives.
Previously:
HTTP POST
↓
Python web service
Now:
MQTT broker
↓
subscriber
↓
Python
Suppose the MQTT subscriber receives payload bytes representing:
{
"timestamp": 1792109007,
"temperature": 23
}
The subscriber decodes the bytes and parses the JSON:
import json
record = json.loads(
message.payload.decode()
)
records.append(record)
The exact callback around this code depends on the MQTT client library being used on Linux. But again, our focus is the state of the data.
Watch what happens:
MQTT payload bytes
↓
.decode()
↓
JSON text
↓
json.loads()
↓
Python dictionary
↓
records collection
After several MQTT messages arrive, our collection might conceptually contain:
MQTT #1 → one record
MQTT #2 → one record
MQTT #3 → one record
MQTT #4 → one record
↓
collection of records
↓
Pandas
Then we're back in familiar territory:
import pandas as pd
df = pd.DataFrame(records)
Once the messages have become ordinary Python records, Pandas doesn't need to know whether they originally arrived through HTTP or MQTT.
The transport layer has done its job.
One Dataset, Three Destinations
Everything downstream can remain essentially the same as Article #1.
Once Pandas has cleaned our data, we can produce the same three data products.
CSV
For Excel and spreadsheet users:
df.to_csv(
"temperature_clean.csv",
index=False
)
Producing something like:
timestamp,temperature
2026-10-15T16:03:27,23
2026-10-15T16:03:32,23
2026-10-15T16:03:37,24
JSON
For developers, applications, APIs, and automation:
df.to_json(
"temperature.json",
orient="records",
date_format="iso"
)
SQLite
And for a relational database:
import sqlite3
connection = sqlite3.connect(
"temperature.db"
)
df.to_sql(
"readings",
connection,
if_exists="append",
index=False
)
connection.close()
Our final outputs are still:
-
temperature_clean.csv -
temperature.json -
temperature.db
MQTT didn't replace any of them.
MQTT changed how the measurements traveled into our data-processing environment.
Watch the State of the Data
This remains the heart of the project.
Our observation starts as:
23
A sensor measurement.
Then:
b"23\n"
UART bytes.
Then:
{
"timestamp": 1792109007,
"temperature": 23
}
A Python dictionary on the Pico.
Then:
{
"timestamp": 1792109007,
"temperature": 23
}
JSON.
Then:
MQTT payload bytes
Published under:
sensors/microbit/temperature
Then the Linux subscriber reverses part of the process:
MQTT bytes
↓
JSON
↓
Python dictionary
↓
Pandas DataFrame
And eventually:
CSV
JSON
SQLite
It is still fundamentally the same observation.
The representation and state of the data change as the observation crosses system boundaries.
The Bigger Idea
Put the two pipelines next to each other.
Article #1 used HTTP:
micro:bit
↓
Pico W
↓
HTTP POST
↓
Linux
↓
Pandas
↓
CSV / JSON / SQLite
Article #2 uses MQTT:
micro:bit
↓
Pico W
↓
MQTT publish
↓
broker
↓
subscriber
↓
Linux
↓
Pandas
↓
CSV / JSON / SQLite
We didn't rebuild the project.
We changed one architectural layer.
And because each component already had a clearly defined responsibility, most of the system remained recognizable.
micro:bit
MEASURE
↓
Pico W
TIMESTAMP + PACKAGE + PUBLISH
↓
MQTT
DISTRIBUTE
↓
Linux
PROCESS + STORE + DISTRIBUTE
The micro:bit still doesn't know about Pandas.
The Pico still doesn't know about CSV or SQLite.
And now the Pico doesn't need to communicate directly with the final data-processing application either.
It publishes a structured observation to a topic.
Subscribers decide what to do with it.
That's the useful idea behind adding MQTT to our little pipeline.
MQTT isn't the project.
The broker isn't the project.
The micro:bit and Pico aren't individually the project either.
The project is the flow of data through the system.
Everything else has a particular responsibility along the way.
