# roberttoups/temp_humidity_api architecture

> Architecture diagram of the GitHub repository roberttoups/temp_humidity_api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/roberttoups/temp_humidity_api
- Repository: https://github.com/roberttoups/temp_humidity_api

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
graph TB
    %% Hardware Layer
    subgraph "Hardware Layer"
        direction TB
        DHT["DHT22/AM2302 Sensor"]:::hardware
        Pi["Raspberry Pi Zero"]:::hardware
    end

    %% OS & System Services
    subgraph "OS & System Services"
        direction TB
        OS["Raspbian Linux"]:::services
        SD["systemd - temp_humidity_api.service"]:::services
        Cron["cron"]:::services
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        API["Flask API<br/>(temp_humidity_api.py)"]:::application
        Batch["Batch Logger<br/>(get_local_data.py)"]:::application
    end

    %% Web & WSGI Layer
    subgraph "Web & WSGI Layer"
        direction TB
        Gunicorn["Gunicorn<br/>(WSGI server)"]:::web
        Nginx["Nginx<br/>(Reverse Proxy)"]:::web
    end

    %% Local Storage
    subgraph "Local Storage"
        direction TB
        Storage[/"CSV Files<br/>temperature_data/"/]:::storage
    end

    %% External Clients
    subgraph "External Clients"
        direction TB
        Client["Authorized Clients<br/>(curl, dashboards)"]:::external
    end

    %% Project Setup & Docs
    subgraph "Project Setup & Docs"
        direction TB
        Install["install_api.sh"]:::docs
        Readme["README.md"]:::docs
        Gitignore[".gitignore"]:::docs
        Vscode[".vscode/"]:::docs
    end

    %% Connections
    DHT -->|"GPIO4"| API
    DHT -->|"GPIO4"| Batch
    Pi --> OS
    OS --> SD
    OS --> Cron
    SD -->|"manages"| Gunicorn
    API -->|"Flask"| Gunicorn
    Gunicorn -->|"HTTP :8000"| Nginx
    Nginx -->|"HTTP :80 (IP whitelist)"| Client
    Cron -->|"schedule"| Batch
    Batch -->|"file I/O"| Storage

    %% Bootstrap relationships (dashed)
    Install -.-> OS
    Install -.-> SD
    Install -.-> Cron
    Install -.-> API
    Install -.-> Gunicorn
    Install -.-> Nginx

    %% Click Events
    click API "https://github.com/roberttoups/temp_humidity_api/blob/master/temp_humidity_api.py"
    click Batch "https://github.com/roberttoups/temp_humidity_api/blob/master/get_local_data.py"
    click Install "https://github.com/roberttoups/temp_humidity_api/blob/master/install_api.sh"
    click Readme "https://github.com/roberttoups/temp_humidity_api/blob/master/README.md"
    click Gitignore "https://github.com/roberttoups/temp_humidity_api/blob/master/.gitignore"
    click Vscode "https://github.com/roberttoups/temp_humidity_api/tree/master/.vscode/"

    %% Styles
    classDef hardware fill:#b2fab4,stroke:#333,stroke-width:1px
    classDef services fill:#b2dffb,stroke:#333,stroke-width:1px
    classDef application fill:#fff5ba,stroke:#333,stroke-width:1px
    classDef web fill:#ffc090,stroke:#333,stroke-width:1px
    classDef storage fill:#e2f0cb,stroke:#333,stroke-width:1px,shape:cylinder
    classDef external fill:#d4c4fb,stroke:#333,stroke-width:1px
    classDef docs fill:#f2f2f2,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
