-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLED_Controller.py
155 lines (133 loc) · 5.8 KB
/
LED_Controller.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# ------------------------------------------
# --- Author: Pradeep Singh
# --- Date: 29th March 2017
# --- Version: 1.0
# --- Description: This python script will leverage AWS IoT Shadow to control LED
# ------------------------------------------
# Import package
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import ssl, time, sys, json
# =======================================================
# Set Following Variables
# AWS IoT Endpoint
MQTT_HOST = ""
# CA Root Certificate File Path
CA_ROOT_CERT_FILE = ""
# AWS IoT Thing Name
THING_NAME = ""
# AWS IoT Thing Certificate File Path
THING_CERT_FILE = "42f2bc1d29-certificate.pem.crt"
# AWS IoT Thing Private Key File Path
THING_PRIVATE_KEY_FILE = "42f2bc1d29-private.pem.key"
# =======================================================
# =======================================================
# No need to change following variables
LED_PIN = 14
MQTT_PORT = 8883
MQTT_KEEPALIVE_INTERVAL = 45
SHADOW_UPDATE_TOPIC = "$aws/things/" + THING_NAME + "/shadow/update"
SHADOW_UPDATE_ACCEPTED_TOPIC = "$aws/things/" + THING_NAME + "/shadow/update/accepted"
SHADOW_UPDATE_REJECTED_TOPIC = "$aws/things/" + THING_NAME + "/shadow/update/rejected"
SHADOW_UPDATE_DELTA_TOPIC = "$aws/things/" + THING_NAME + "/shadow/update/delta"
SHADOW_GET_TOPIC = "$aws/things/" + THING_NAME + "/shadow/get"
SHADOW_GET_ACCEPTED_TOPIC = "$aws/things/" + THING_NAME + "/shadow/get/accepted"
SHADOW_GET_REJECTED_TOPIC = "$aws/things/" + THING_NAME + "/shadow/get/rejected"
SHADOW_STATE_DOC_LED_ON = """{"state" : {"reported" : {"LED" : "ON"}}}"""
SHADOW_STATE_DOC_LED_OFF = """{"state" : {"reported" : {"LED" : "OFF"}}}"""
# =======================================================
# Initiate GPIO for LED
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(LED_PIN, GPIO.OUT)
# Initiate MQTT Client
mqttc = mqtt.Client("client2")
# Master LED Control Function
def LED_Status_Change(Shadow_State_Doc, Type):
# Parse LED Status from Shadow
DESIRED_LED_STATUS = ""
print "\nParsing Shadow Json..."
SHADOW_State_Doc = json.loads(Shadow_State_Doc)
if Type == "DELTA":
DESIRED_LED_STATUS = SHADOW_State_Doc['state']['LED']
elif Type == "GET_REQ":
DESIRED_LED_STATUS = SHADOW_State_Doc['state']['desired']['LED']
print "Desired LED Status: " + DESIRED_LED_STATUS
# Control LED
if DESIRED_LED_STATUS == "ON":
# Turn LED ON
print "\nTurning ON LED..."
GPIO.output(LED_PIN, GPIO.HIGH)
# Report LED ON Status back to Shadow
print "LED Turned ON. Reporting ON Status to Shadow..."
mqttc.publish(SHADOW_UPDATE_TOPIC,SHADOW_STATE_DOC_LED_ON,qos=1)
elif DESIRED_LED_STATUS == "OFF":
# Turn LED OFF
print "\nTurning OFF LED..."
GPIO.output(LED_PIN, GPIO.LOW)
# Report LED OFF Status back to Shadow
print "LED Turned OFF. Reporting OFF Status to Shadow..."
mqttc.publish(SHADOW_UPDATE_TOPIC,SHADOW_STATE_DOC_LED_OFF,qos=1)
else:
print "---ERROR--- Invalid LED STATUS."
# Define on connect event function
# We shall subscribe to Shadow Accepted and Rejected Topics in this function
def on_connect(mosq, obj, rc):
print "Connected to AWS IoT..."
# Subscribe to Delta Topic
mqttc.subscribe(SHADOW_UPDATE_DELTA_TOPIC, 1)
# Subscribe to Update Topic
#mqttc.subscribe(SHADOW_UPDATE_TOPIC, 1)
# Subscribe to Update Accepted and Rejected Topics
mqttc.subscribe(SHADOW_UPDATE_ACCEPTED_TOPIC, 1)
mqttc.subscribe(SHADOW_UPDATE_REJECTED_TOPIC, 1)
# Subscribe to Get Accepted and Rejected Topics
mqttc.subscribe(SHADOW_GET_ACCEPTED_TOPIC, 1)
mqttc.subscribe(SHADOW_GET_REJECTED_TOPIC, 1)
# Define on_message event function.
# This function will be invoked every time,
# a new message arrives for the subscribed topic
def on_message(mosq, obj, msg):
if str(msg.topic) == SHADOW_UPDATE_DELTA_TOPIC:
print "\nNew Delta Message Received..."
SHADOW_STATE_DELTA = str(msg.payload)
print SHADOW_STATE_DELTA
LED_Status_Change(SHADOW_STATE_DELTA, "DELTA")
elif str(msg.topic) == SHADOW_GET_ACCEPTED_TOPIC:
print "\nReceived State Doc with Get Request..."
SHADOW_STATE_DOC = str(msg.payload)
print SHADOW_STATE_DOC
LED_Status_Change(SHADOW_STATE_DOC, "GET_REQ")
elif str(msg.topic) == SHADOW_GET_REJECTED_TOPIC:
SHADOW_GET_ERROR = str(msg.payload)
print "\n---ERROR--- Unable to fetch Shadow Doc...\nError Response: " + SHADOW_GET_ERROR
elif str(msg.topic) == SHADOW_UPDATE_ACCEPTED_TOPIC:
print "\nLED Status Change Updated SUCCESSFULLY in Shadow..."
print "Response JSON: " + str(msg.payload)
elif str(msg.topic) == SHADOW_UPDATE_REJECTED_TOPIC:
SHADOW_UPDATE_ERROR = str(msg.payload)
print "\n---ERROR--- Failed to Update the Shadow...\nError Response: " + SHADOW_UPDATE_ERROR
else:
print "AWS Response Topic: " + str(msg.topic)
print "QoS: " + str(msg.qos)
print "Payload: " + str(msg.payload)
def on_subscribe(mosq, obj, mid, granted_qos):
#As we are subscribing to 3 Topics, wait till all 3 topics get subscribed
#for each subscription mid will get incremented by 1 (starting with 1)
if mid == 3:
# Fetch current Shadow status. Useful for reconnection scenario.
mqttc.publish(SHADOW_GET_TOPIC,"",qos=1)
def on_disconnect(client, userdata, rc):
if rc != 0:
print "Diconnected from AWS IoT. Trying to auto-reconnect..."
# Register callback functions
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_disconnect = on_disconnect
# Configure TLS Set
mqttc.tls_set(CA_ROOT_CERT_FILE, certfile=THING_CERT_FILE, keyfile=THING_PRIVATE_KEY_FILE, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Continue monitoring the incoming messages for subscribed topic
mqttc.loop_forever()