Overview
Status LEDs on a Pi provide visual feedback for services, alarms, or sensor thresholds. A thin HTTP API lets browsers and mobile apps toggle pins safely.
Use gpiozero.LED or RPi.GPIO on Raspberry Pi OS.
Implementation
Run a Flask or FastAPI app on the Pi binding to port 5000. Endpoints POST /led/on and POST /led/off set BCM pin HIGH/LOW. Front with nginx and HTTPS if exposed beyond LAN.
Never run the API as root; add the service user to the gpio group on modern Pi OS.
When implementing guidance from Control Raspberry Pi LEDs from Web and Mobile, start in a controlled environment that mirrors production versions of operating systems, runtimes, and network policies. Capture a baseline before changes: export configs, snapshot VMs, or tag releases in source control so rollback stays straightforward if behavior regresses.
Document prerequisites, expected outcomes, and verification steps in a short runbook. Automated checks—smoke tests, health endpoints, or query validations—catch regressions early when platforms receive patches. Security belongs in every workflow: apply least privilege, rotate secrets, and review audit logs after deployment.
If results differ across machines, compare environment variables, permission models, time zones, and regional settings. Intermittent issues often trace to caching layers, stale DNS, or duplicated services bound to the same port.
Example
from gpiozero import LED
from flask import Flask
led = LED(17)
app = Flask(__name__)
@app.post('/led/')
def set_led(state):
getattr(led, 'on' if state == 'on' else 'off')()
return {'ok': True}
Tips
- Use resistors on bare LEDs.
- Prefer MQTT for many concurrent clients.
- Firewall external access.
- Clean up GPIO on shutdown.
- Re-verify after reboots, certificate renewals, or failover exercises.
- Align monitoring and alerts with the failure modes described in this guide.
- Keep vendor documentation links handy for breaking changes between versions.
- Pair automation with a manual spot check during initial production rollout.