How codingforentrepreneurs/django-iot-with-timescaledb works
Project Identification
• The project is a Django IoT application that collects time series sensor data using TimescaleDB.
• It is designed to run on multiple Raspberry Pi devices (“djangopi”, “djangopi-2”, etc.), each hosting a Django instance that interacts with local sensors.
• The deployment and configuration are handled via Ansible; additional critical services include Redis (for Celery tasks) and external TimescaleDB Cloud for data storage.
File Structure
• The top-level directories include configuration files like the Dockerfile, .env samples, and a “compose.yaml” indicating containerization.
• The “src” directory holds the Django application (with a project module “cfehome” and an application called “sensors”). This illustrates the typical web server, API, and background task (Celery) structure.
• The “ansible” directory contains an assortment of playbooks and roles (e.g., deploy_django, connect_hosts, redis, services_config) which automate configuration across multiple nodes (Raspberry Pis).
• Additional elements, like “requirements.txt” and “requirements.dev.txt”, detail package dependencies.
Components & Relationships
• Ansible Automation Engine:
• Central component responsible for orchestrating deployments, connecting hosts, and managing service restarts across nodes.
• Uses various playbooks (connect_hosts, deploy_django, restart_services) to manage both the application and ancillary services.
• IoT Nodes (Raspberry Pis):
• Each device runs a Django instance (using Gunicorn and possibly behind an Nginx proxy), accesses local sensors via the “sensors” app, and communicates with peers using Ansible-configured host connections.
• They operate with Celery workers (using Redis as a broker) for asynchronous tasks.
• External Services:
• TimescaleDB Cloud: This is the main database component for storing the time series data coming from the sensors.
• Redis: Likely configured locally on each node or as a shared service for task queuing with Celery in the Django app.
• Application Components:
• Web Application: The Django project (found under src/cfehome) handles API endpoints (api.py), configuration (settings.py), task scheduling (celery.py), and web serving (wsgi.py/asgi.py).
• Sensors Module: Special functionality for collecting sensor data, as seen in the “sensors” sub-folder with models, views, tasks, etc.
• Static and Template Layers: Static resources and Django templates provide the frontend for administrative/user interfaces.
Architectural Patterns & Design Principles
• Distributed Deployment: The use of Ansible playbooks and a dynamic host inventory.
• Separation of Concerns: Clear separation between the web application (Django), background task execution (Celery with Redis), and the data layer (TimescaleDB).
• Containerization: Presence of Docker-related files suggests that containerization may be a deployment method.
• Scalability & Modularity: Each component (Django instance, Celery, Redis, and TimescaleDB) can be scaled or maintained separately.