Skip to content

Instantly share code, notes, and snippets.

@li2hub
Last active April 2, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save li2hub/40374bacb23f28f6eae7dea64a3f2934 to your computer and use it in GitHub Desktop.
Save li2hub/40374bacb23f28f6eae7dea64a3f2934 to your computer and use it in GitHub Desktop.
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