-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_subscribe_thingspeak.py
98 lines (71 loc) · 2.67 KB
/
mqtt_subscribe_thingspeak.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
#!/usr/bin/env python
import time
import os
import requests
import json
import paho.mqtt.client as mqtt
Connected = False #global variable for the state of the connection
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
client.subscribe("channels/1925304")
Connected = True
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']
def on_log(client, userdata, level, buf):
print("log: " + buf)
# ThingSpeak MQTT Broker: do not change
THINGS_DEVICE_MQTTSERV = "mqtt3.thingspeak.com"
#Channel ID: Enter your channel id!
CHANNELID = "1925304"
#ThingSpeak Device user name: Enter your personal user name!
THINGS_DEVICE_USERNAME = "KwUABBcFEQQwLTQ3LyEEHjU"
#ThingSpeak Device password: Enter your password!
THINGS_DEVICE_PASSWORD = "X358xFsnRTNt+rVaUVN2l81F"
#ThingSpeak Device client id: Enter the client id!
CLIENT_NAME = "KwUABBcFEQQwLTQ3LyEEHjU"
#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 password
#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 #global variable for the state of the connection
client = mqtt.Client("KwUABBcFEQQwLTQ3LyEEHjU") #create new instance
client.username_pw_set(THINGS_DEVICE_USERNAME, password=THINGS_DEVICE_PASSWORD) #set username and password
client.on_connect= on_connect #attach function to callback
client.on_message= on_message #attach function to callback
client.connect(THINGS_DEVICE_MQTTSERV, port=PORT) #connect to broker
client.loop_start() #start the loop
while Connected != True: #Wait for connection
time.sleep(0.1)
print Connected
client.subscribe("channels/1925304")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
client.disconnect()
client.loop_stop()