-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttlednative.py
126 lines (106 loc) · 3.69 KB
/
mqttlednative.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'''
What is sent:
{
"command": "",
"red": 255,
"green": 0,
"blue": 0,
"brightness": 0.15
}
command: one of "Black" "Red" "Green" "Blue" " Yellow" "Rainbow" "Off"
'''
import json
import logging
import time
import blinkt
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
# if set to false, the lights will stay on when the program exits
# blinkt.set_clear_on_exit(False)
# Get broker and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("secrets are kept in secrets.py, please add them there!")
raise
# Custom MQTT message callback
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
# need to process the payload to get what I want/need.
# so not the whole string
doc = json.loads(message.payload)
command = ""
try:
command = doc["command"]
red = doc["red"]
green = doc["green"]
blue = doc["blue"]
brightness = doc["brightness"]
blinkt.set_all(red, green, blue, brightness)
except (ValueError, RuntimeError, KeyError) as e:
print("Failed to parse values from MQTT message\n", e)
# specific commands
if command == "Blue" :
blinkt.set_all(0, 0, 255)
blinkt.show()
if command == "Red" :
blinkt.set_all(255, 0, 0)
blinkt.show()
if command == "Yellow" :
blinkt.set_all(255, 127, 0)
blinkt.show()
if command == "Green" :
blinkt.set_all(0, 255, 0)
blinkt.show()
if command == "Black" :
blinkt.set_all(0, 0, 0, 0.0)
blinkt.show()
if command == "Off" :
blinkt.set_all(0, 0, 0, 0.0)
blinkt.show()
if command == "Rainbow" :
blinkt.set_pixel(0, 255, 0, 0) #red
blinkt.set_pixel(1, 255, 127, 0) # orange
blinkt.set_pixel(2, 255, 255, 0) #yellow
blinkt.set_pixel(3, 0, 255, 0) #green
blinkt.set_pixel(4, 0, 255, 127) #cyan
blinkt.set_pixel(5, 0, 127, 255) # blue cyan
blinkt.set_pixel(6, 0, 0, 255) # blue
blinkt.set_pixel(7, 127, 0, 255) # blue magenta (violet?)
blinkt.show()
blinkt.show()
host = secrets["broker"]
rootCAPath = secrets["root_ca_path"]
certificatePath = secrets["certificate_path"]
privateKeyPath = secrets["private_key_path"]
clientId = secrets["client_id"]
topic = secrets["topic"]
print("endpoint", host)
port = 8883
# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
myAWSIoTMQTTClient.configureEndpoint(host, port)
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.subscribe(topic, 1, customCallback)
time.sleep(2)
while True:
time.sleep(10)