Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import RPi.GPIO as GPIO
- import os
- from Adafruit_IO import *
- from Adafruit_IO import MQTTClient
- clear = "CLEAR"
- ADAFRUIT_IO_USERNAME = "mrbreadwater"
- ADAFRUIT_IO_KEY = "9d5a4ef654b34db7873fefc77ff9863b"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(18,GPIO.OUT)
- GPIO.setup(17,GPIO.OUT)
- GPIO.setup(23,GPIO.OUT)
- GPIO.setup(24,GPIO.OUT)
- GPIO.setup(25,GPIO.OUT)
- def connected(client):
- client.subscribe('welcome-feed') #may need to change this feedname later
- print "Connected!"
- client.publish('welcome-feed', clear)
- client.publish('welcome-feed', clear)
- time.sleep(1.5)
- def disconnected(client):
- print 'Lost connection to server!'
- def message(client, feed_id, payload):
- if payload == "LIGHT" :
- print "LIGHT command received from IFTTT."
- print "LIGHTS TOGGLED!"
- time.sleep(0.2)
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.1)
- GPIO.output(17,GPIO.LOW)
- elif payload == "FAN_LOW" :
- print "FAN SPEED SET TO LOW!"
- time.sleep(0.2)
- GPIO.output(18, GPIO.HIGH)
- time.sleep(0.1)
- GPIO.output(18, GPIO.LOW)
- elif payload == "FAN_MED" :
- print "FAN SPEED SET TO MEDIUM!"
- time.sleep(0.2)
- GPIO.output(23,GPIO.HIGH)
- time.sleep(0.1)
- GPIO.output(23,GPIO.LOW)
- elif payload == "FAN_HIGH" :
- print "FAN SPEED SET TO HIGH!"
- time.sleep(0.2)
- GPIO.output(24, GPIO.HIGH)
- time.sleep(0.1)
- GPIO.output(24, GPIO.LOW)
- elif payload == "FAN_OFF" :
- print "FAN IS OFF"
- time.sleep(0.2)
- GPIO.output(25, GPIO.HIGH)
- time.sleep(0.1)
- GPIO.output(25, GPIO.LOW)
- else:
- print"Message from IFTTT received: %s" % payload
- client = MQTTClient (ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
- # Setup the callback functions defined above.
- # English: If it connects, go to connected, if it gets a msg go to message
- client.on_connect = connected
- client.on_message = message
- client.on_disconnect = disconnected
- print 'Attempting a connection to the server...'
- client.connect()
- client.loop_background() # loop in background
- while 1 == 1:
- time.sleep(120)
- client.publish('welcome-feed', "PING")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement