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:
djangopidjangopi-2djangopi-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/activatewindows powershell
c:\path\to\python.exe -m venv env
.\env\Scripts\activateWith virtual environment activated:
pip install --upgrade pip
pip install -r requirements.dev.txt
pip install -r requirements.txtcp .env.web-sample .env.prodEnsure .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 Analyticsas your service type -
For the configuration:
- the
Regionso it's near you (e.g.us-east-1if you are in New York) Service nameset 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.prodCreate 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.iniIn 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=coreUpdate the amount of nodes as needed.
cd ansible
ansible-playbook playbooks/connect_hosts.yamlThis 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.yamlBy 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.yamlansible-playbook playbooks/connect_hosts.yaml
ansible-playbook playbooks/deploy_django.yaml
ansible-playbook playbooks/restart_services.yaml