Last active
April 2, 2019 11:11
-
-
Save li2hub/40374bacb23f28f6eae7dea64a3f2934 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Adafruit_IO import MQTTClient | |
import RPi.GPIO as IO | |
ADAFRUIT_IO_USERNAME = "xxxxxxxxxx" | |
ADAFRUIT_IO_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
IO.setwarnings(False) | |
IO.setmode(IO.BOARD) | |
IO.setup(11,IO.OUT) | |
def connected(client): | |
print("Subscribing") | |
client.subscribe('Relay1') # or change to whatever name you used | |
# this gets called every time a message is received | |
def message(client, feed_id, payload): | |
if payload == "1": | |
print ("Turning ON the light") | |
IO.output(11,IO.HIGH) | |
elif payload == "0": | |
print ("Turning OFF the light") | |
IO.output(11,IO.LOW) | |
else: | |
print ("Message from Adafruit: %s" % payload) | |
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) | |
# Setup the callback functions defined above. | |
client.on_connect = connected | |
client.on_message = message | |
client.connect() | |
print("Connected to Adafruit IO") | |
client.loop_blocking() # block forever on client loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment