Distributed Ddjango IOT Application. Stacks: Pyhton/Django/Celery/Redis/TimescaleDb/JavaScript/PostGress/Raspberry PI
Distribute Django across IoT Devices (Raspberry PI) to collect Time Series data from sensors using TimescaleDB.
Ansible will help automate the configuration for however many Raspberry Pis you have.
Going forward, each Raspberry Pi will be configured with the root user as core
and each Pi will have a unique hostname with the pattern:
djangopi
djangopi-2
djangopi-3
- etc
To access these pis on your local network, you can use:
This configuration is done when you flash (aka install) the Raspberry Pi on a new MicroSD card with the Raspberry Pi Imager. The OS used will be dependant on your Raspberry Pi version. For this guide, sed the headless os called Raspberry Pi Lite (64-bit) with custom settings to automatically:
- set hostname (
djangopi
, etc) - set root user (
core
) - connect to local wifi
- install my SSH public key (
~/.ssh/id_rsa.pub
) for passwordless SSH sessions (very useful for Ansible)
mkdir -p django-iot
cd django-iot
git clone https://github.com/sempedia/distributed-django-iot.git .
mac/linux/wsl
python3 -m venv env
source env/bin/activate
windows powershell
c:\path\to\python.exe -m venv env
.\env\Scripts\activate
With virtual environment activated:
pip install --upgrade pip
pip install -r requirements.dev.txt
pip install -r requirements.txt
cp .env.web-sample .env.prod
Ensure .env.prod
resembles:
DJANGO_SECRET_KEY="django-insecure-ajq-plfh&4uh6rea1zliteu+bszy57v*g%t0t^j2i^6)w%t"
DATABASE_URL="postgresql://local_username:local_password@localhost:5555/local_database"
CELERY_BROKER_REDIS_URL="redis://localhost:6555/0"
-
Sign up on timescaledb to get your production-ready
DATABASE_URL
-
Under "Services" click "+ Create Service"
-
Select
Time Series and Analytics
as your service type -
For the configuration:
- the
Region
so it's near you (e.g.us-east-1
if you are in New York) Service name
set it todjango-iot
- all other defaults are fine.
- the
-
Copy the connection string (also known as
Service URL
); this will show the password once in the format:postgres://tsdbadmin:[email protected]:37802/tsdb?sslmode=require
Next, run:
ansible-vault encrypt .env.prod
Create a password you will remember. To decrypt, you can run ansible-vault decrypt .env.prod
.
This will ensure your .env
file does not leak any secrets or passwords.
Check the current inventory file:
cat ansible/inventory.ini
In the repo, it yields:
[main]
djangopi.local node_id=1
[nodes]
djangopi-2.local node_id=2
djangopi-3.local node_id=3
djangopi-4.local node_id=4
# djangopi-5.local node_id=5
[all:vars]
ansible_user=core
Update the amount of nodes as needed.
cd ansible
ansible-playbook playbooks/connect_hosts.yaml
This will make pi-to-pi communication possible. Run this each time you add a new node to the cluster (e.g. a new pi to collect data)
From within the ansible/
directory, run:
ansible-playbook playbooks/deploy_django.yaml
By default, this will install the code from this repo. If you want to use your own repo, open up playbooks/deploy_django.yaml
and replace https://github.com/codingforentrepreneurs/django-iot-with-timescaledb
in the vars.github_repo:
configuration with your value.
After you push run:
ansible-playbook playbooks/restart_services.yaml
ansible-playbook playbooks/connect_hosts.yaml
ansible-playbook playbooks/deploy_django.yaml
ansible-playbook playbooks/restart_services.yaml