Created
February 16, 2019 20:02
-
-
Save iayanpahwa/3fffd15d5c683b86e4a5fa493ad99453 to your computer and use it in GitHub Desktop.
MQTT Subscriber Rpi
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
sudo pip install paho-mqtt | |
sudo apt-get update | |
########### PYTHON 3 ############# | |
from gpiozero import LED | |
from time import sleep | |
import paho.mqtt.client as mqtt | |
import sys | |
red=LED(22) | |
yellow=LED(27) | |
green=LED(17) | |
def on_connect(client, userdata, flags, rc): | |
print("[STATUS] connected to broker "+str(rc)) | |
client.subscribe("bambootraffic") | |
def on_message(client, userdata, msg): | |
payload = str(msg.payload) | |
print("Payload is: "+ payload) | |
if payload == "b'RED1'": | |
red.on() | |
if payload == "b'RED0'": | |
red.off() | |
if payload == "b'YELLOW1'": | |
yellow.on() | |
if payload == "b'YELLOW0'": | |
yellow.off() | |
if payload == "b'GREEN1'": | |
green.on() | |
if payload == "b'GREEN0'": | |
green.off() | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect("iot.eclipse.org", 1883, 60) | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment