-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_mqttClient.py
108 lines (84 loc) · 2.87 KB
/
run_mqttClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from django.core.management.base import BaseCommand, CommandError
from myapp.models import *
import os
import sys
import django
import json
import paho.mqtt.client as mqtt
import time
import datetime
class Command(BaseCommand):
help = 'Invokes the mqtt client and subscribes to the thingspeak server'
def handle(self, *args, **options):
# ---- Enter your code below.......!
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, message):
print("message topic: " + message.topic)
print("message received: " + str(message.payload.decode("utf-8")))
msg = json.loads(str(message.payload.decode("utf-8")))
channel = msg['channel_id']
creationDate = msg['created_at']
entryNumber = msg['entry_id']
humidity = msg['field1']
temperature = msg['field2']
accelx = msg['field3']
accely = msg['field4']
accelz = msg['field5']
latitude = msg['field6']
longitude = msg['field7']
# Create a new database entry
table = TemperatureAndHumidityData()
table.timestamp = creationDate
table.temperature = temperature
table.humidity = humidity
table.lat = latitude
table.lon = longitude
table.accelx = accelx
table.accely = accely
table.accelz = accelz
table.save()
def on_log(client, userdata, level, buf):
print("log: " + buf)
# ThingSpeak MQTT Broker: do not change
MQTTHOST = "mqtt.thingspeak.com"
#Channel ID: Enter your channel id!
CHANNELID = "1217029"
#ThingSpeak Read Key: Enter your personal read key!
THINGSAPIKEY_READ ="LWCNVALJH2I4S0GJ"
#MQTT client name: Enter a unique but arbitrary string!
CLIENT_NAME = "abm123456"
#ThingSpeaks standard MQTT port number: do not change
PORT=1883
#Some additional parameters: leave these values unchanged
KEEPALIVE=600
QOS_LEVEL=0
#---->
#Insert your code:
#use the predefined parameters,
#create a mqtt paho client
#and attach the appropriate callback functions;
#don't forget to invoke the "username_pw_set()" function
#with the username and ThingSpeak API key
#in advance to the call of the "connect()" function;
#subscribe to your ThingSpeak channel
#and use "loop_forever()" at the end of your program
#instead of the loop_start()/loop_stop() method!
Connected = False
client = mqtt.Client(CLIENT_NAME)
client.username_pw_set(username = CLIENT_NAME, password = "61T4NH4L9AFAD1X1")
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTTHOST, PORT)
client.loop_start()
#while Connected != True:
#time.sleep (0.1)
client.subscribe("channels/1217029/subscribe/json/LWCNVALJH2I4S0GJ")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
client.disconnect()
client.loop_stop()
client.loop_forever()