Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - #!/usr/bin/python3
 - import ssl
 - import paho.mqtt.client as mqtt
 - import cmd
 - import time
 - import json
 - import configparser
 - devStatus = None
 - def on_connect(mqttc, obj, flags, rc):
 - if rc==0:
 - global thingname
 - print ("Connection successful")
 - mqttc.subscribe("$aws/things/"+thingname+"/shadow/lamp", qos=0)
 - elif rc==1:
 - print ("Connection refused")
 - def on_subscribe(mqttc, obj, mid, granted_qos):
 - print("Subscribed")
 - def on_message(mqttc, obj, msg):
 - global devStatus
 - jData = json.loads(msg.payload.decode("utf-8"))
 - devStatus = jData["color"]
 - class ControlInterface(cmd.Cmd):
 - def do_status(self, line):
 - global devStatus
 - print(devStatus)
 - def do_send(self, color):
 - global thingname
 - mqttc.publish(topic="$aws/things/"+thingname+"/shadow/control",payload=color,qos=0)
 - def do_EOF(self, line):
 - return True
 - if __name__ == '__main__':
 - config = configparser.RawConfigParser()
 - config.read('conf/config.prop')
 - thingname = config.get("General","thingname")
 - rootcert = config.get("General","rootcert")
 - certificate = config.get("General","certificate")
 - key = config.get("General","key")
 - endpoint = config.get("General","endpoint")
 - awsport = config.getint("General","port")
 - mqttc = mqtt.Client(client_id="commander")
 - mqttc.on_connect = on_connect
 - mqttc.on_subscribe = on_subscribe
 - mqttc.on_message = on_message
 - mqttc.tls_set(rootcert,
 - certfile=certificate,
 - keyfile=key,
 - tls_version=ssl.PROTOCOL_TLSv1_2,
 - ciphers=None)
 - mqttc.connect(endpoint, port=awsport)
 - mqttc.loop_start()
 - time.sleep(2)
 - mqttc.publish(topic="$aws/things/"+thingname+"/shadow/control",payload="",qos=0)
 - ControlInterface().cmdloop()
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment