Edge Computing
Local data processing, anomaly detection, and TinyML on devices
Skills taught: embedded.edge
Why Process Data at the Edge?
Imagine a factory with 500 vibration sensors reading at 100 Hz each. That's 50,000 data points per second — 4.3 billion per day. Sending all of that to the cloud is prohibitively expensive and introduces 100+ ms latency for local decisions. Edge computing moves intelligence to the device: detect anomalies locally in <1 ms, only upload summaries or alerts.
Cloud-only vs Edge-first Architecture
- Cloud-only: Every raw reading sent to cloud for processing
- Edge-first: Process and filter locally, send summaries/alerts
Pattern 1: Rolling Window Aggregation
Instead of uploading every reading, compute statistics over a time window (e.g., last 60 seconds) and upload the summary once per window. This reduces cloud writes by 60× while preserving meaningful information.
from collections import deque
from statistics import mean, stdev
import json
import time
import paho.mqtt.client as mqtt
WINDOW_SIZE = 60 # 60 readings = 1 minute at 1/second
UPLOAD_EVERY = 60 # upload summary every 60 readings
window = deque(maxlen=WINDOW_SIZE)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "edge-01")
client.connect("localhost", 1883)
client.loop_start()
def read_temperature():
"""Replace with actual sensor read."""
import random
return 22.0 + random.gauss(0, 0.5)
def upload_summary():
if len(window) < 2:
return
summary = {
"count": len(window),
"mean": round(mean(window), 2),
"min": round(min(window), 2),
"max": round(max(window), 2),
"stdev": round(stdev(window), 3),
"window": WINDOW_SIZE,
"ts": int(time.time())
}
client.publish("edge/01/summary", json.dumps(summary), qos=1)
print(f"Uploaded summary: {summary}")
counter = 0
while True:
temp = read_temperature()
window.append(temp)
counter += 1
if counter >= UPLOAD_EVERY:
upload_summary()
counter = 0
time.sleep(1)
Pattern 2: Statistical Anomaly Detection
Detect anomalies locally using the z-score: how many standard deviations a reading is from the recent mean. A z-score > 2.5 indicates an unusual event worth alerting. This costs zero cloud requests for normal data.
from collections import deque
from statistics import mean, stdev
import json, time
import paho.mqtt.client as mqtt
WINDOW = deque(maxlen=60) # rolling 60-reading baseline
Z_ALERT = 2.5 # z-score threshold for alert
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "anomaly-01")
client.connect("localhost", 1883)
client.loop_start()
def process_reading(value: float) -> None:
WINDOW.append(value)
if len(WINDOW) < 10:
return # not enough data yet
avg = mean(WINDOW)
sd = stdev(WINDOW)
if sd == 0:
return # constant signal, no anomaly possible
z = abs(value - avg) / sd
if z > Z_ALERT:
# Anomaly: send real-time alert
alert = {
"type": "anomaly",
"value": round(value, 2),
"mean": round(avg, 2),
"z_score": round(z, 2),
"ts": int(time.time())
}
client.publish("edge/01/alert", json.dumps(alert), qos=1, retain=False)
print(f"ALERT: {alert}")
else:
# Normal: just print locally
print(f"Normal: {value:.2f} (z={z:.2f})")
import random
while True:
reading = 25.0 + random.gauss(0, 0.5)
# Simulate an occasional spike
if random.random() < 0.02:
reading += random.uniform(5, 10)
process_reading(reading)
time.sleep(1)
TinyML: Running ML Models on the Device
TinyML runs pre-trained machine learning models directly on microcontrollers or the Raspberry Pi — no cloud needed for inference. TensorFlow Lite models run on Pi in milliseconds; TensorFlow Lite Micro runs on Arduino Nano 33 BLE Sense (256 KB SRAM) for keyword detection, gesture recognition, and anomaly detection.
# Running a TensorFlow Lite model on Raspberry Pi
# Install: pip install tflite-runtime (lighter than full TF)
import numpy as np
try:
import tflite_runtime.interpreter as tflite
except ImportError:
import tensorflow as tf
tflite = tf.lite
# Load the model (trained on desktop, deployed as .tflite)
interpreter = tflite.Interpreter(model_path="anomaly_detector.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
def predict(sensor_readings: list) -> float:
"""Returns anomaly score 0.0 (normal) to 1.0 (anomaly)."""
# Reshape to model's expected input shape
input_data = np.array(sensor_readings, dtype=np.float32).reshape(input_details[0]['shape'])
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])
return float(output[0][0])
# Example: predict on a window of 10 readings
readings = [23.1, 23.3, 23.0, 22.9, 23.2, 23.1, 23.4, 23.0, 35.0, 23.1]
score = predict(readings)
print(f"Anomaly score: {score:.3f} {'ANOMALY' if score > 0.5 else 'Normal'}")
Tip: For edge deployments: use NumPy for fast statistical processing (10–100× faster than pure Python loops on Pi), store data in SQLite for local buffering during internet outages, and use a background thread for MQTT communication so sensor reading never blocks.
Warning: Edge ML models drift over time as real-world conditions change (seasonal temperature shifts, sensor aging). Implement a retraining pipeline: collect new labeled examples periodically, retrain offline, deploy updated .tflite via OTA update. A model that was 95% accurate in January may be 70% accurate in July without retraining.
Frequently Asked Questions
What is TinyML and what can it run on?
TinyML is ML inference on constrained hardware. TensorFlow Lite runs on Raspberry Pi (all models) and Python single-board computers. TensorFlow Lite Micro runs on microcontrollers with ≥256 KB SRAM: Arduino Nano 33 BLE Sense, ESP32-S3, STM32H7, and Raspberry Pi RP2040. Use cases: keyword detection, gesture classification, anomaly detection, predictive maintenance.
How do I handle sensor data during internet outages?
Buffer locally in SQLite: insert each reading to a local database with an 'uploaded' flag. A background thread periodically checks internet connectivity, queries un-uploaded rows, sends them in batches, and marks them uploaded. This gives you resilience without losing data during outages.
What's the difference between edge computing and fog computing?
Edge computing processes data on the device itself (Pi, ESP32, sensor node). Fog computing is a layer between edge devices and the cloud — typically a local gateway server (e.g., industrial PC or on-premise server) that aggregates data from many edge nodes before sending to cloud. The terms are often used interchangeably in practice.