Python for IoT Domain¶
When you'd use this
Collect sensor data, message with MQTT, and run on edge devices.
Connect and control IoT devices — sensors, actuators, telemetry — and move data to the cloud.
What you'll learn¶
- The shape of an IoT system
- Smooth noisy sensor data (tested)
- Messaging with MQTT
- Edge vs cloud processing
- The device + library ecosystem
IoT (Internet of Things) connects physical sensors and devices to software. Python runs across the stack — on the device (Embedded Python), at the edge, and in the cloud. The sensor-processing example here is run-verified.
The shape of an IoT system¶
The shape of an IoT system — a key concept in Python for IoT.
sensors → edge device → messaging → cloud/backend → dashboard
(temp, (Raspberry Pi, (MQTT) (store, analyze) (visualize,
motion) microcontroller) alert)
Data flows from cheap sensors, through a local device that may pre-process it, over a lightweight protocol (usually MQTT), to a backend that stores and acts on it.
Smoothing noisy sensor data (tested)¶
Smoothing noisy sensor data in Python for IoT — what it is and when to use it.
Real sensors are noisy — readings jitter. A moving average smooths them, which is one of the most common IoT data tasks. Runnable:
from collections import deque
class MovingAverage:
def __init__(self, window):
self.buf = deque(maxlen=window) # keeps only the last `window` values
def add(self, x):
self.buf.append(x)
return sum(self.buf) / len(self.buf)
ma = MovingAverage(window=3)
print([round(ma.add(v), 2) for v in [10, 20, 30, 40]])
Output:
The average slides over the last 3 readings: after 40, the window is (20, 30, 40) → 30. Using deque(maxlen=3) means old values drop off automatically — an elegant, efficient sliding window. This same pattern smooths temperature, filters spikes, and detects trends on-device.
Messaging with MQTT¶
The lightweight pub/sub protocol that connects devices to the cloud.
MQTT is the dominant IoT protocol — lightweight publish/subscribe designed for unreliable networks and tiny devices. A device publishes readings to a topic; backends subscribe. It's the same pub/sub decoupling as an event bus, tuned for constrained devices.
import paho.mqtt.client as mqtt # pip install paho-mqtt
client = mqtt.Client()
client.connect("broker.example.com", 1883)
# Device publishes a reading
client.publish("home/livingroom/temp", "21.5")
# Backend subscribes
def on_message(client, userdata, msg):
print(f"{msg.topic}: {msg.payload.decode()}")
client.subscribe("home/+/temp") # + is a single-level wildcard
client.on_message = on_message
client.loop_forever()
MQTT snippet needs paho-mqtt + a broker
This follows paho-mqtt's documented API and isn't run-verified here (the moving-average code is). Note the topic wildcard home/+/temp — MQTT's hierarchical topics with wildcards make it easy to subscribe to whole categories of devices.
Edge vs cloud¶
Process on-device for latency/privacy or in the cloud for scale.
A key IoT design decision is where processing happens:
- Edge (on the device) — process data locally: smooth it, detect events, act immediately. Lower latency, less bandwidth, works offline. Constrained by the device's CPU/power.
- Cloud — send raw data up for heavy analysis, storage, ML, dashboards. More power, but needs connectivity and bandwidth.
Most real systems split the work: the edge filters/aggregates (like our moving average), sending only meaningful data up — saving bandwidth and battery.
The ecosystem¶
paho-mqtt, MicroPython, and cloud IoT SDKs.
| Layer | Tools |
|---|---|
| Device runtime | MicroPython, CircuitPython, CPython on Pi (Embedded) |
| Messaging | paho-mqtt, MQTT brokers (Mosquitto) |
| Hardware I/O | gpiozero, RPi.GPIO (Embedded Linux) |
| Backend | FastAPI, time-series DBs (InfluxDB) |
| Dashboards | Grafana, Home Assistant |
Practice exercises¶
- Add a
max/mintracker alongside the moving average to flag out-of-range readings. - Extend
MovingAverageto detect a spike (current reading far from the average) and return a flag. - Design MQTT topic names for a home with several rooms and sensor types.
- Explain when you'd process on the edge vs send raw data to the cloud, with a concrete example.
- Simulate a sensor stream (random walk) and smooth it, printing raw vs smoothed values.
💬 Discussion
Have a question about this topic? Found an error? Share your thoughts below.