Chapter 10 — Edge Logic: What Runs on the Device, What Runs on the Server
Every node in this book sits somewhere on a spectrum, from “send every raw reading to the broker” to “decide locally and report later”. Choosing where each piece of logic runs is one of the few architecture decisions in a homelab IoT setup that’s expensive to change later.
flowchart LR
A["Dumb node<br/>every reading to the broker"] --> B["Smart node<br/>filter + batch"]
B --> C["Edge gateway<br/>Pi runs rules, HA, inference"]
C --> D["Server<br/>history, dashboards, LLMs"]
Why Not Send Everything?
On a LAN with mains power, sending every reading is fine, and simpler. The case for doing more on the device appears when one of these is true:
- The node runs on battery. Every transmission costs radio time, and radio time is the energy budget (Chapter 8).
- The response has to be immediate. A valve or an alarm shouldn’t wait for a round trip to a server that might be rebooting.
- The network isn’t guaranteed. WiFi drops, the broker restarts, the homelab is down for an update.
- The data shouldn’t leave the house. Camera frames are the obvious case (Chapter 15).
Four Patterns, Smallest First
All four are a few lines of MicroPython or Arduino, and they combine.
1. Deadband: send on change, not on schedule
# publish only when the value moved more than 0.5 °C, or every 30 min regardless
if abs(temp - last_sent) > 0.5 or now - last_time > 30 * 60:
client.publish(b"home/balcony/temperature", str(temp))
last_sent, last_time = temp, now
The heartbeat matters: without it, a stable reading looks the same as a dead node.
2. Batch: send several readings at once
buffer.append((now, temp, hum))
if len(buffer) >= 12: # e.g. one hour of 5-minute readings
client.publish(b"home/balcony/batch", json.dumps(buffer))
buffer.clear()
One WiFi connection per hour instead of twelve. On a solar node this is often the single biggest saving. Across deep sleep the buffer has to live in RTC memory or flash (Chapter 8).
3. Local threshold: act first, report second
if soil_moisture < DRY_THRESHOLD:
valve.on() # act locally (Chapter 6)
client.publish(b"garden/alert/dry", str(soil_moisture))
The decision doesn’t depend on the broker being up. The server learns about it and can still apply smarter rules on top.
4. Store and forward: never lose a reading to an outage
try:
client.publish(topic, payload)
except OSError:
with open("pending.jsonl", "a") as f: # flash, survives reboot
f.write(payload + "\n")
# on the next successful connect: replay pending.jsonl, then delete it
Combined with MQTT QoS 1 (Chapter 9), this covers the gap between “the node sent it” and “the server stored it”.
The Gateway Tier: Where a Pi Earns Its Place
Between microcontrollers and the server there’s often a Raspberry Pi (Chapter 1). It’s the natural home for edge logic that’s too heavy for an ESP32:
- Home Assistant automations run locally, so a switch still works when the internet doesn’t (Chapter 19).
- Object detection: Frigate runs inference next to the camera, with a Coral TPU if needed, and sends events instead of video (Chapter 15).
- A local LLM can summarise sensor data without it leaving the house (Chapter 20).
Microcontrollers do read, filter and act. The Pi does rules and inference. The server does history, dashboards (Chapter 17) and anything that needs data from many nodes at once.
Deciding Where a Piece of Logic Goes
| Keep it on the node or gateway if… | Send it to the server if… |
|---|---|
| it must react in under a second | it needs history (trends, averages over weeks) |
| it must work with the network down | it combines data from several nodes |
| it saves radio time on a battery node | it’s a heavy model or an LLM |
| the raw data is private (video, audio) | you’ll want to change it often without touching hardware |
When in doubt, keep the node simple and the logic on the server. Server-side logic is easier to change, and an over-clever node is the one you have to climb up to and re-flash. Even with OTA (Chapter 8), simpler firmware fails less.
Takeaway: Deadband, batch, local threshold, store-and-forward: four small patterns that cover most edge needs. Put reactions and privacy at the edge, history and intelligence on the server, and keep the microcontroller as simple as the use case allows.