-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathreader.py
45 lines (35 loc) · 1.4 KB
/
reader.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
import paho.mqtt.client as mqtt
import uuid
import ssl
# the # wildcard means we subscribe to all subtopics of IDD
topic = 'IDD/#'
# some other examples
# topic = 'IDD/a/fun/topic'
#this is the callback that gets called once we connect to the broker.
#we should add our subscribe functions here as well
def on_connect(client, userdata, flags, rc):
print(f"connected with result code {rc}")
client.subscribe(topic)
# you can subsribe to as many topics as you'd like
# client.subscribe('some/other/topic')
# this is the callback that gets called each time a message is recived
def on_message(cleint, userdata, msg):
print(f"topic: {msg.topic} msg: {msg.payload.decode('UTF-8')}")
# you can filter by topics
# if msg.topic == 'IDD/some/other/topic': do thing
# Every client needs a random ID
client = mqtt.Client(str(uuid.uuid1()))
# configure network encryption etc
client.tls_set(cert_reqs=ssl.CERT_NONE)
# this is the username and pw we have setup for the class
client.username_pw_set('idd', 'device@theFarm')
# attach out callbacks to the client
client.on_connect = on_connect
client.on_message = on_message
#connect to the broker
client.connect(
'farlab.infosci.cornell.edu',
port=8883)
# this is blocking. to see other ways of dealing with the loop
# https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php#network-loop
client.loop_forever()