SlideShare a Scribd company logo
Django for Internet of Things:
from hackathon
to production
Anna Schneider
DjangoCon US, 18 July 2016
@windupanna
I’m Anna :)
Co-founder and CTO at WattTime
WattTime.org @wattTime
UnconsciousBiasProject.org @UBP_STEM
what??
Django for Internet of Things:
from hackathon
to production
everyone’s favorite framework
🎸🐍
Django
Internet of Things (IoT)
when a thing you don’t normally think of as a computer
can transmit data and respond to controls in real time
🏡🚗 📡🎮⏰
some people like to write the code on the thing,
I like to talk to things through their APIs
Internet of Things (IoT)
☁️ 🚗☁️you them the thing
a really fun way to write
really fragile code really quickly
hackathon
💻☕️ 👏⚠️
when you can ship your code and trust it
while you’re having fun at DjangoCon
production
💻 🍎 🚢 😎
design patterns (and anti-patterns)
for writing and deploying Django projects
to monitor and control an IoT device using its API
so you can get started fast then build well
what you’ll learn
☕️ 🚢
😱
python manage.py startproject awesome_iot_hackathon
💻☕️ 👏⚠️
models!
the books app
Book
title
author
year
Book
title
author
year
Author
name
hometown
Book
title
author
year
Book
title
author
year
Author
name
hometown
Author
name
hometown
the books IoT app
Observation
value
device
timestamp
Device
name
location
Device
name
location
Device
name
location
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
the IoT app
Device
name
location
vendor ID
Device
name
location
vendor ID
Device
name
location
vendor ID
☕️ 🚢
vendor’s unique ID(s)
whatever data you need
to send to their API
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
the IoT app
plan for big(gish)
time series data
eg use db_index
🚢
Device
name
location
vendor ID
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Device
name
location
vendor ID
Device
name
location
vendor ID
the IoT app
Attribute vs Status
numerical vs str/bool
values
Observation
value
device
timestamp
Device
name
location
vendor ID
Observation
value
device
timestamp
Observation
value
device
timestamp
Attribute
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Attribute
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Attribute
value
device
timestamp
Device
name
location
vendor ID
Device
name
location
vendor ID
Observation
value
device
timestamp
Observation
value
device
timestamp
Observation
value
device
timestamp
Status
is_on
device
timestamp
☕️ 🚢
views?
models
views? tasks!
☁️ 🚗☁️you them the thing
request
response
☁️ 💻you them
request
response
normal
views
IoT
tasks
what tasks do we need to do?
pull new attributes
pull new on/off statuses
set attributes
set on/off statuses
monitor control
tasks.py
def do_something_awesome(device):
# decide to turn on or off
is_on = run_decision_criterion()
# set status using device API
set_status(device, is_on)
# create status in db
status = device.status_set.create(
is_on=is_on,
valid_at=timezone.now(),
)
# return
return status
☕️
the awesome part
tasks.py
def do_something_awesome(device):
# decide to turn on or off
is_on = run_decision_criterion()
# set status using device API
set_status(device, is_on)
# create status in db
status = device.status_set.create(
is_on=is_on,
valid_at=timezone.now(),
)
# return
return status
🚢
bad for asynchronous
😱
tasks.py
def do_something_awesome(device_id):
# get device from pk
device = Device.objects.get(
pk=device_id
)
# decide to turn on or off
is_on = run_decision_criterion()
# set status using device API
set_status(device, is_on)
# create status in db
status = device.status_set.create(
is_on=is_on,
valid_at=timezone.now(),
)
# return
return [status.pk]
pass pks
pro: don’t rely on database state
con: may be extra queries
🚢
put it together!
models
tasks
the hackathon app
myapp
models.py
Device
Attribute
Status
tasks.py
pull_status
set_status
pull_attributes
set_attributes
client.py
admin.py
views.py
tests.py
☕️
the production apps
devices
models.py
admin.py
views.py
tests.py
interactions
tasks.py
views.py
tests.py
vendor
client.py
tests.py
observations
models.py
admin.py
views.py
tests.py
views for
adding/removing devices
analytics,
DRF for dashboards
models for logging,
views for clickable tasks
swappable
vendors
🚢
deploy!
models
tasks
apps
deploy tasks?
goals:
• run any task (control or monitor)
• at frequent, deterministic times
• outside of request/response cycle
cron in the cloud
1) the hackathon way
two ways to automate
management commands + Heroku Scheduler
☕️
python manage.py do_something_awesome --device_id=1
Heroku Scheduler
pros:
• easy to set up
cons:
• limited frequencies
(daily, hourly, 10 min)
• “best effort” service
(may skip some)
☕️
2) the production way
task functions + celery periodic task scheduler
🚢
two ways to automate
Celery
distributed message queuing system for asynchronous stuff
slow event-driven tasks
send the user sign-up email
scheduled periodic tasks
run the daily report
🚢
web servers
Celery architecture (how)
tasks
tasks
worker servers
messages results result store
message broker
transport queue
messages
tasks
scheduler
🚢
Celery architecture (how)
scheduler
do_awesome do_awesome
worker
status.pk
☁️
turn on
on!
Status(is_on=True)
🚢
from celery import shared_task
@shared_task
def set_status(device_id):
...
@shared_task
def do_something_awesome(device_id):
...
just add decorator :)
tasks.py
🚢
(what)
from celery.schedules import crontab
SCHEDULE = {
'run_task': {
# use the actual path
'task': ‘do_something_awesome’,
# every 5 minutes
'schedule': crontab(minute='*/5'),
# args and kwargs
'args': [],
'kwargs': {},
},
}
very close to crontab
once per minute - once per year
schedule.py
🚢
(when)
from celery.schedules import crontab
SCHEDULE = {
'run_task': {
# use the actual path
'task': ‘do_something_awesome’,
# every 5 minutes
'schedule': crontab(minute='*/5'),
# args and kwargs
'args': [],
'kwargs': {'device_id': d.pk},
} for d in Device.objects.all()
}
schedule.py
🚢
static arguments only
code is only evaluated once at run time,
better to have one task spawn daughters
(when)
😱
from celery.schedules import crontab
SCHEDULE = {
'run_task': {
# use the actual path
'task': ‘run_all',
# every 5 minutes
'schedule': crontab(minute='*/5'),
# args and kwargs
'args': [],
'kwargs': {},
}
}
@shared_task
def run_all():
for d in Device.objects.all():
do_something_awesome(d.pk)
schedule.py
🚢
static arguments only
code is only evaluated once at run time,
better to have one task spawn daughters
(when)
production, Celerified
devices
models.py
admin.py
views.py
tests.py
interactions
tasks.py
schedule.py
views.py
tests.py
brand
client.py
tests.py
observations
models.py
admin.py
views.py
tests.py
🚢
django_iot
__init__.py
celery.py
settings.py
wsgi.py
urls.py
django_iot
Procfile
requirements.txt
manage.py
cookiecutter https://github.com/aschn/cookiecutter-django-iot.git
☕️ 🚢
• think time series data: mind your models
• async not request/response: tasks not views
• ☕️ easy but rigid: Heroku Scheduler
• 🚢 flexible but complex: celery periodic tasks
• hack better+faster: cookiecutter-django-iot
• IoT can be for good, not just for fun and profit
what have we learned?
Thanks!!
Anna Schneider
@windupanna
@wattTime
cookiecutter https://github.com/aschn/cookiecutter-django-iot.git

More Related Content

What's hot (20)

PPTX
BLOCK DIAGRAM OF HARDWIRED CONTROL UNIT
Rahul Sharma
 
PDF
Oil circuit breakers
jawaharramaya
 
PPTX
Townsend ’s theory
vishalgohel12195
 
PPT
Air circuit breakers
Prasant Kumar
 
DOC
C programming project by navin thapa
Navinthp
 
PPTX
Pic 18 microcontroller
Ashish Ranjan
 
PPTX
Superposition theorem
Jayanshu Gundaniya
 
PPTX
Power Electronics-DC-AC Converters/Inverters.pptx
Poornima D
 
PPTX
Hospital management system dfd
college of agriculture information technology
 
PPTX
Psps ppt
anu_janani
 
PDF
555 Timer Ic
Vaibhav Lone
 
PPTX
Applications of Z transform
AakankshaR
 
PPT
Vector calculus
raghu ram
 
PDF
7SD52/53 Line Differential Protection Relay
ashwini reliserv
 
PDF
Fault Calculations
michaeljmack
 
PPTX
Streamer Theory of Breakdown in Gases.pptx
KarthikeyanK816516
 
PPTX
Cockroft Walten Generator
MAYURSINH RATHOOD
 
PPTX
Power system protection
Anu Priya
 
PPTX
Bridge Rectifier Circuit with Working Operation and Their Types
elprocus
 
PPTX
132 kv g.s.s chambal jaipur
monga786
 
BLOCK DIAGRAM OF HARDWIRED CONTROL UNIT
Rahul Sharma
 
Oil circuit breakers
jawaharramaya
 
Townsend ’s theory
vishalgohel12195
 
Air circuit breakers
Prasant Kumar
 
C programming project by navin thapa
Navinthp
 
Pic 18 microcontroller
Ashish Ranjan
 
Superposition theorem
Jayanshu Gundaniya
 
Power Electronics-DC-AC Converters/Inverters.pptx
Poornima D
 
Hospital management system dfd
college of agriculture information technology
 
Psps ppt
anu_janani
 
555 Timer Ic
Vaibhav Lone
 
Applications of Z transform
AakankshaR
 
Vector calculus
raghu ram
 
7SD52/53 Line Differential Protection Relay
ashwini reliserv
 
Fault Calculations
michaeljmack
 
Streamer Theory of Breakdown in Gases.pptx
KarthikeyanK816516
 
Cockroft Walten Generator
MAYURSINH RATHOOD
 
Power system protection
Anu Priya
 
Bridge Rectifier Circuit with Working Operation and Their Types
elprocus
 
132 kv g.s.s chambal jaipur
monga786
 

Viewers also liked (10)

PDF
Masters of SlideShare
Kapost
 
PDF
Async Tasks with Django Channels
Albert O'Connor
 
KEY
Django Celery
Mat Clayton
 
PDF
Django channels
Andy Dai
 
PDF
Scalable Django Architecture
Rami Sayar
 
PDF
12 tips on Django Best Practices
David Arcos
 
PDF
Scalable web application architecture
postrational
 
PPTX
Internet of Things and Big Data: Vision and Concrete Use Cases
MongoDB
 
PDF
Architecture of a Modern Web App
scothis
 
PDF
Web Development with Python and Django
Michael Pirnat
 
Masters of SlideShare
Kapost
 
Async Tasks with Django Channels
Albert O'Connor
 
Django Celery
Mat Clayton
 
Django channels
Andy Dai
 
Scalable Django Architecture
Rami Sayar
 
12 tips on Django Best Practices
David Arcos
 
Scalable web application architecture
postrational
 
Internet of Things and Big Data: Vision and Concrete Use Cases
MongoDB
 
Architecture of a Modern Web App
scothis
 
Web Development with Python and Django
Michael Pirnat
 

Similar to Django for IoT: From hackathon to production (DjangoCon US) (20)

PDF
Practical Celery
Cameron Maske
 
PDF
7 ways to execute scheduled jobs with python
Hugo Shi
 
PDF
Django at Scale
bretthoerner
 
PDF
An Introduction to Celery
Idan Gazit
 
PDF
Using Python for IoT: a return of experience, Alexandre Abadie
Pôle Systematic Paris-Region
 
PDF
Python for IoT, A return of experience
Alexandre Abadie
 
PDF
Airflow tutorials hands_on
pko89403
 
PDF
Django Celery - A distributed task queue
Alex Eftimie
 
PPTX
Django deployment best practices
Erik LaBianca
 
PDF
Python for IoT Development: A Beginner-Friendly Approach
Shiv Technolabs Pvt. Ltd.
 
PDF
Elastic network of things with mqtt and micro python
Wei Lin
 
PDF
Tasks: you gotta know how to run them
Filipe Ximenes
 
PPTX
IoT ppts unitvxbxbdbdgaqarqfqwrfqa 4.pptx
rajukolluri
 
PPTX
IoT ppts unit 4.pptxdaaaaffffffffffaaaaaaaaaaaaff
rajukolluri
 
PDF
(Ebook) Python for DevOps: Learn Ruthlessly Effective Automation by Noah Gift...
betshanumo
 
PDF
IOT Based Smart City: Weather, Traffic and Pollution Monitoring System
IRJET Journal
 
PDF
Python-for-DevOps-Learn-Ruthlessly-Effective-Automation-by-Noah-Gift_-Kennedy...
MinhTrnNht7
 
PDF
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
KEY
Django Deployment with Fabric
Jonas Nockert
 
PPT
Course Notes-Unit 5.ppt
SafaM3
 
Practical Celery
Cameron Maske
 
7 ways to execute scheduled jobs with python
Hugo Shi
 
Django at Scale
bretthoerner
 
An Introduction to Celery
Idan Gazit
 
Using Python for IoT: a return of experience, Alexandre Abadie
Pôle Systematic Paris-Region
 
Python for IoT, A return of experience
Alexandre Abadie
 
Airflow tutorials hands_on
pko89403
 
Django Celery - A distributed task queue
Alex Eftimie
 
Django deployment best practices
Erik LaBianca
 
Python for IoT Development: A Beginner-Friendly Approach
Shiv Technolabs Pvt. Ltd.
 
Elastic network of things with mqtt and micro python
Wei Lin
 
Tasks: you gotta know how to run them
Filipe Ximenes
 
IoT ppts unitvxbxbdbdgaqarqfqwrfqa 4.pptx
rajukolluri
 
IoT ppts unit 4.pptxdaaaaffffffffffaaaaaaaaaaaaff
rajukolluri
 
(Ebook) Python for DevOps: Learn Ruthlessly Effective Automation by Noah Gift...
betshanumo
 
IOT Based Smart City: Weather, Traffic and Pollution Monitoring System
IRJET Journal
 
Python-for-DevOps-Learn-Ruthlessly-Effective-Automation-by-Noah-Gift_-Kennedy...
MinhTrnNht7
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
Django Deployment with Fabric
Jonas Nockert
 
Course Notes-Unit 5.ppt
SafaM3
 

Recently uploaded (20)

PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Hashing Introduction , hash functions and techniques
sailajam21
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 

Django for IoT: From hackathon to production (DjangoCon US)