How roberttoups/temp_humidity_api works
Project type and purpose
• This is an IoT‐style microservice running on a Raspberry Pi. It reads temperature and humidity from a DHT22/AM2302 sensor, exposes those readings via a REST API, and optionally logs them locally on a schedule.
Main components
a. Hardware
• DHT22/AM2302 sensor
• Raspberry Pi Zero (running Raspbian OS)
b. Operating system & system services
• Raspbian Linux
• systemd (temp_humidity_api.service) – ensures the API is always running
• cron – schedules get_local_data.py for periodic data dumps
c. Application layer
• temp_humidity_api.py – Flask application, reads sensor via Adafruit_DHT, exposes /metrics
• get_local_data.py – CLI script to read sensor and append CSV files under temperature_data/
d. WSGI and web server
• Gunicorn – runs the Flask app with worker processes on port 8000
• Nginx – reverse proxy on port 80, access restricted by IP range
e. Local storage
• File system under /home/pi/temp_humidity_api/temperature_data/ containing timestamped CSVs
f. External clients
• Authorized users or scripts calling http://<pi_ip>:<port>/metrics (curl, dashboards, monitoring tools)
Relationships & data flows
1. DHT22 sensor → (GPIO) → temp_humidity_api.py / get_local_data.py
2. temp_humidity_api.py → (Flask) → Gunicorn ← systemd
3. Gunicorn → (HTTP) → Nginx → (HTTP) → External client
4. cron → get_local_data.py → (file I/O) → temperature_data CSV files
Architectural patterns & principles
• Separation of concerns: sensor logic, web/API logic, service management, and data persistence are distinct.
• Use of a WSGI server (Gunicorn) behind a reverse proxy (Nginx) for production‐level reliability and security.
• systemd for resilience and auto-restart. cron for simple periodic tasks.
• Minimal layering fits an embedded IoT use case.
Technologies & frameworks
• Python 3, Flask (lightweight REST framework), Adafruit_DHT library
• Gunicorn (WSGI server), Nginx (reverse proxy)
• systemd, cron (Linux service and scheduling)
• Bash scripting (install_api.sh)
• CSV file storage for local data
Essential elements
• Component names and their roles
• Data flows with arrowheads and labels
• Service orchestration (cron, systemd)
• Network boundaries (port 8000 vs 80, IP whitelist)
• Local file system for data storage