Skip to content

Distributed Django IOT Application. Stacks: Pyhton/Django/Celery/Redis/Ansible/Docker/TimescaleDb/JavaScript/PostGres/Raspberry PI/ChartJs

License

Notifications You must be signed in to change notification settings

sempedia/distributed-django-iot

Repository files navigation

distributed-django-iot

Distributed Ddjango IOT Application. Stacks: Pyhton/Django/Celery/Redis/TimescaleDb/JavaScript/PostGress/Raspberry PI

Django IoT with TimescaleDB

Distribute Django across IoT Devices (Raspberry PI) to collect Time Series data from sensors using TimescaleDB.

Getting Started with Ansible, Raspberry Pis, Django, and 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)

1. Clone this repo

mkdir -p django-iot
cd django-iot
git clone https://github.com/sempedia/distributed-django-iot.git .

2. Create virtual environment and install dev packages

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

3. Create .env.prod for encrypted environment variables

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"

4. Create a new TimescaleDB Cloud service:

  1. Sign up on timescaledb to get your production-ready DATABASE_URL

  2. Under "Services" click "+ Create Service"

  3. Select Time Series and Analytics as your service type

  4. 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 to django-iot
    • all other defaults are fine.
  5. 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

5. Encrypt .env.prod

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.

6. Create or update inventory.ini in ansible/

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.

7. Run Connect Hosts

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)

8. Push to Prod

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

9. Refresh Services

ansible-playbook playbooks/connect_hosts.yaml


ansible-playbook playbooks/deploy_django.yaml


ansible-playbook playbooks/restart_services.yaml