Skip to content

Instantly share code, notes, and snippets.

@PhirePhly
Created June 24, 2016 04: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 PhirePhly/aa425e909696a3bb35f8b177ac08bc08 to your computer and use it in GitHub Desktop.
Save PhirePhly/aa425e909696a3bb35f8b177ac08bc08 to your computer and use it in GitHub Desktop.
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
def on_connect(client, userdata, rc):
print ("Connected with rc: " + str(rc))
client.subscribe("kwf/demo/led")
def on_message(client, userdata, msg):
print ("Topic: "+ msg.topic+"\nMessage: "+str(msg.payload))
if "green" in msg.payload:
print(" Green on!")
GPIO.output(11, True)
else:
print(" Green off!")
GPIO.output(11, False)
if "yellow" in msg.payload:
print(" Yellow on!")
GPIO.output(12, True)
else:
print(" Yellow off!")
GPIO.output(12, False)
if "red" in msg.payload:
print(" Red on!")
GPIO.output(13, True)
else:
print(" Red off!")
GPIO.output(13, False)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment