Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import time
  2. import RPi.GPIO as GPIO
  3. import os
  4. from Adafruit_IO import *
  5. from Adafruit_IO import MQTTClient
  6.  
  7. clear = "CLEAR"
  8.  
  9. ADAFRUIT_IO_USERNAME = "mrbreadwater"
  10. ADAFRUIT_IO_KEY = "9d5a4ef654b34db7873fefc77ff9863b"
  11.  
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setwarnings(False)
  14. GPIO.setup(18,GPIO.OUT)
  15. GPIO.setup(17,GPIO.OUT)
  16. GPIO.setup(23,GPIO.OUT)
  17. GPIO.setup(24,GPIO.OUT)
  18. GPIO.setup(25,GPIO.OUT)
  19.  
  20. def connected(client):
  21. client.subscribe('welcome-feed') #may need to change this feedname later
  22. print "Connected!"
  23. client.publish('welcome-feed', clear)
  24. client.publish('welcome-feed', clear)
  25. time.sleep(1.5)
  26.  
  27. def disconnected(client):
  28. print 'Lost connection to server!'
  29. def message(client, feed_id, payload):
  30. if payload == "LIGHT" :
  31. print "LIGHT command received from IFTTT."
  32. print "LIGHTS TOGGLED!"
  33. time.sleep(0.2)
  34. GPIO.output(17,GPIO.HIGH)
  35. time.sleep(0.1)
  36. GPIO.output(17,GPIO.LOW)
  37. elif payload == "FAN_LOW" :
  38. print "FAN SPEED SET TO LOW!"
  39. time.sleep(0.2)
  40. GPIO.output(18, GPIO.HIGH)
  41. time.sleep(0.1)
  42. GPIO.output(18, GPIO.LOW)
  43.  
  44. elif payload == "FAN_MED" :
  45. print "FAN SPEED SET TO MEDIUM!"
  46. time.sleep(0.2)
  47. GPIO.output(23,GPIO.HIGH)
  48. time.sleep(0.1)
  49. GPIO.output(23,GPIO.LOW)
  50. elif payload == "FAN_HIGH" :
  51. print "FAN SPEED SET TO HIGH!"
  52. time.sleep(0.2)
  53. GPIO.output(24, GPIO.HIGH)
  54. time.sleep(0.1)
  55. GPIO.output(24, GPIO.LOW)
  56.  
  57. elif payload == "FAN_OFF" :
  58. print "FAN IS OFF"
  59. time.sleep(0.2)
  60. GPIO.output(25, GPIO.HIGH)
  61. time.sleep(0.1)
  62. GPIO.output(25, GPIO.LOW)
  63. else:
  64. print"Message from IFTTT received: %s" % payload
  65.  
  66. client = MQTTClient (ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
  67.  
  68. # Setup the callback functions defined above.
  69. # English: If it connects, go to connected, if it gets a msg go to message
  70.  
  71. client.on_connect = connected
  72. client.on_message = message
  73. client.on_disconnect = disconnected
  74.  
  75. print 'Attempting a connection to the server...'
  76. client.connect()
  77. client.loop_background() # loop in background
  78.  
  79.  
  80. while 1 == 1:
  81. time.sleep(120)
  82. client.publish('welcome-feed', "PING")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement