This repository was archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmqtt-sub.py
70 lines (54 loc) · 1.79 KB
/
mqtt-sub.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
import ssl
import paho.mqtt.client as mqtt
from time import sleep
from sense_hat import SenseHat
#from capture import take_photo, reko_detect_faces, read_image
try:
from local_settings import ROOT_CA, CERTFILE, KEYFILE, AWS_IOT_ENDPOINT
except ImportError:
from default_settings import ROOT_CA, CERTFILE, KEYFILE, AWS_IOT_ENDPOINT
sense = SenseHat()
sense.clear()
publish_topic = "$aws/things/sensehat/shadow/update/temperature"
subscribe_topic = "iotbutton/G030MD024412CV3X"
# '#' in the topic is a wildcard so any topic behond that wildcard will work
def on_connect(mqttc, obj, flags, rc):
if rc == 0:
print("Status code: {0} | Connection successful".format(rc))
elif rc == 1:
print("Status code: {0} | Connection refused".format(rc))
def on_message(mqttc, obj, msg):
print(
"Received message from {0} | QoS: {1} | Data: {2}".format(
msg.topic, msg.qos, msg.payload
)
)
# Take the message payload and prints it on the screem
# of the sensehat display
sense.show_message('Hello Meetup!')
sense.clear()
# print('message payload: {}'.format(msg.payload))
# if 'take_pic' in msg.payload:
# take_photo()
# reko_detect_faces(read_image('capture/image.jpg'))
mqttc = mqtt.Client(client_id="mqtt-test")
#callbacks
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.tls_set(
ROOT_CA,
certfile=CERTFILE,
keyfile=KEYFILE,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None
)
mqttc.connect(AWS_IOT_ENDPOINT, port=8883)
mqttc.subscribe(subscribe_topic, qos=1)
mqttc.loop_start()
while 1:
sleep(5)
temp = "%.2f" % sense.get_temperature()
mqttc.publish(
publish_topic, temp, qos=1
)
print("msg sent to {0}: temperature {1}".format(publish_topic, temp))