Skip to content

Commit 30a4648

Browse files
committed
add Django MQTT support
1 parent 4616d52 commit 30a4648

File tree

10 files changed

+42
-3
lines changed

10 files changed

+42
-3
lines changed

examples/django/develop/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"django.contrib.sessions",
3838
"django.contrib.messages",
3939
"django.contrib.staticfiles",
40-
"iotcore.djangoiot"
40+
# "iotcore.djangoiot", # Broker only
41+
"iot"
4142
]
4243

4344
MIDDLEWARE = [

examples/django/develop/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
"""
1717
from django.contrib import admin
1818
from django.urls import path
19+
from iot import views
1920

2021
urlpatterns = [
2122
path("admin/", admin.site.urls),
23+
path("sub", views.subscribe),
24+
path("pub", views.publish),
2225
]

examples/django/iot/__init__.py

Whitespace-only changes.

examples/django/iot/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

examples/django/iot/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class IotConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "iot"

examples/django/iot/migrations/__init__.py

Whitespace-only changes.

examples/django/iot/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

examples/django/iot/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

examples/django/iot/views.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.http import JsonResponse
2+
from iotcore import IotCore
3+
4+
iot = IotCore()
5+
iot.background_loop_forever()
6+
7+
8+
def mqtt_callback(data):
9+
print(f"Django >: {data}")
10+
11+
12+
def subscribe(request):
13+
iot.subscribe("iot", mqtt_callback)
14+
return JsonResponse({"response": "subscribed"})
15+
16+
17+
def publish(request):
18+
iot.publish("iot", "demo")
19+
return JsonResponse({"response": "published"})

iotcore/djangoiot/apps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
class IotConfig(AppConfig):
77
default_auto_field = "django.db.models.BigAutoField"
88
name = "iotcore.djangoiot"
9+
iotcore_instance = None
910

1011
def ready(self):
1112
server_status = os.environ.get("MQTT_SERVER_RUNNING", None)
1213
if server_status is None:
1314
print("Starting MQTT server!")
1415
os.environ["MQTT_SERVER_RUNNING"] = "true"
15-
iot = IotCore()
16-
iot.background_loop_forever()
16+
self.iotcore_instance = IotCore()
17+
self.iotcore_instance.background_loop_forever()
1718
else:
1819
print("Server already running!")

0 commit comments

Comments
 (0)