Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #required libraries
  4. import sys
  5. import ssl
  6. import paho.mqtt.client as mqtt
  7. import json
  8. from pprint import pprint
  9. import Adafruit_CharLCD as LCD
  10. from textwrap import fill
  11.  
  12. #Configuration
  13. rootCAPath = "/home/pi/Cigar-Box/certs/rootCA.pem"
  14. certFilePath = "/home/pi/Cigar-Box/certs/xxxxxxxxxx-certificate.pem.crt"
  15. keyFilePath = "/home/pi/Cigar-Box/certs/xxxxxxxxxx-private.pem.key"
  16. iotThing = "Zorua"
  17. clientID = "Zorua"
  18.  
  19. #Device JSON initialization
  20. device = {'state': {'reported': {'HP':100} } }
  21. device['state']['reported']['color'] = {'r':0, 'g':0, 'b':0}
  22.  
  23. #Create LCD
  24. lcd = LCD.Adafruit_CharLCDPlate()
  25.  
  26. #LCD wrapper
  27. def set_lcd_color(R,G,B):
  28. global lcd
  29. device['state']['reported']['color']['r'] = R
  30. device['state']['reported']['color']['g'] = G
  31. device['state']['reported']['color']['b'] = B
  32. lcd.set_color(R, G, B)
  33. def set_lcd_message(message):
  34. global lcd
  35. device['state']['reported']['msg'] = message
  36. lcd.clear()
  37.  
  38. #Word wrap to fit 16-char wide display and add capitalization
  39. lcd_message = fill(message.capitalize(),16)
  40. lcd.message(lcd_message)
  41.  
  42. # Initialize the LCD using the pins
  43. set_lcd_message('Initializing...')
  44. set_lcd_color(0, 0, 1)
  45.  
  46. #called while client tries to establish connection with the server
  47. def on_connect(mqttc, obj, flags, rc):
  48. print "Connecting..."
  49. if rc==0:
  50. print ("Subscriber Connection status code: "+str(rc)+" | Connectionstatus: successful")
  51.  
  52. #We only want to be notified about things we need to change to stay in sync with AWS
  53. mqttc.subscribe("$aws/things/" + iotThing + "/shadow/update/delta", qos=1)
  54. elif rc==1:
  55. print ("Subscriber Connection status code: "+str(rc)+" | Connection status: Connection refused")
  56. print ("Subscriber Connection status code: "+str(rc))
  57.  
  58. #called when a topic is successfully subscribed to
  59. def on_subscribe(mqttc, obj, mid, granted_qos):
  60. print("Subscribed: "+str(mid)+" "+str(granted_qos)+"data"+str(obj))
  61. set_lcd_color(0,1,0)
  62. set_lcd_message('Connected!nReady for input')
  63.  
  64. #Let AWS know about the current state of the plate so we can tell us what's out of sync
  65. mqttc.publish("$aws/things/" + iotThing + "/shadow/update", json.dumps(device))
  66.  
  67. #called when a message is received by a topic
  68. #Messages are formatted in JSON
  69. #When working with /update, we might not find all keys all the time, so we need to handle that
  70. def on_message(mqttc, obj, msg):
  71. try:
  72. data = json.loads(msg.payload)
  73. update = data['state']
  74. except:
  75. return
  76.  
  77. #Look for a message in the update. If it's there, we need to update the display
  78. if 'msg' in update.keys():
  79. try:
  80. set_lcd_message(update['msg'])
  81. except:
  82. print("Could not enact message from topic: "+msg.topic+" | QoS: "+str(msg.qos)+" | Data Received: "+str(msg.payload))
  83.  
  84. #Look to see if the status of R, G, or B has changed for the display
  85. if 'color' in update.keys():
  86. try: lcd_r = update['color']['r']
  87. except: lcd_r = device['state']['reported']['color']['r']
  88. try: lcd_g = update['color']['g']
  89. except: lcd_g = device['state']['reported']['color']['g']
  90. try: lcd_b = update['color']['b']
  91. except: lcd_b = device['state']['reported']['color']['b']
  92. set_lcd_color(lcd_r,
  93. lcd_g,
  94. lcd_b)
  95. #Let AWS know we've updated the display
  96. mqttc.publish("$aws/things/Zorua/shadow/update", json.dumps(device))
  97.  
  98. #creating a client with client-id=Zorua
  99. mqttc = mqtt.Client(client_id=clientID)
  100. mqttc.on_connect = on_connect
  101. mqttc.on_reconnect = on_connect
  102. mqttc.on_subscribe = on_subscribe
  103. mqttc.on_message = on_message
  104.  
  105. #Configure network encryption and authentication options. Enables SSL/TLS support.
  106. #adding client-side certificates and enabling tlsv1.2 support as required by aws-iot service
  107. mqttc.tls_set(rootCAPath,
  108. certfile=certFilePath,
  109. keyfile=keyFilePath,
  110. tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
  111.  
  112. #connecting to aws-account-specific-iot-endpoint
  113. print ("About to connect")
  114. mqttc.connect("lettersandnumbers.iot.us-west-2.amazonaws.com", port=8883) #AWS IoT service hostname and portno
  115.  
  116. #automatically handles reconnecting
  117. mqttc.loop_forever()
  118.  
  119. # Default code located inside /etc/rc.local
  120. # Print the IP address
  121.  
  122. _IP=$(hostname -I) || true
  123. if [ "$_IP" ]; then
  124. printf "My IP address is %sn" "$_IP" > /home/pi/cigarbox.log
  125. fi
  126. exit 0
  127.  
  128. ######################################################################
  129.  
  130.  
  131. # After rebooting RPi = no output to log
  132. pi@cigarbox:~ $ cat cigarbox.log
  133.  
  134. # Running /etc/rc.local from the command line
  135. pi@cigarbox:~ $ sh /etc/rc.local
  136.  
  137. # After running /etc/rc.local locally = output to log
  138. pi@cigarbox:~ $ cat cigarbox.log
  139. My IP address is 192.168.0.21
Add Comment
Please, Sign In to add comment