Skip to content

Instantly share code, notes, and snippets.

@yazdipour
Created March 27, 2020 22:58
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 yazdipour/a5706e8bf904435a80d558e053fb79d1 to your computer and use it in GitHub Desktop.
Save yazdipour/a5706e8bf904435a80d558e053fb79d1 to your computer and use it in GitHub Desktop.
Python MQTT Pub/Sub
import paho.mqtt.client as mqtt #pip install paho.mqtt
client = mqtt.Client()
client.username_pw_set(username="testuser",password="testpass")
client.connect("192.168.0.101", 1883)
# PUBLISH
client.publish("Topic", "hello server")
# SUBSCRIBE
def on_message(client, userdata, message):
print("Message Recieved: " + message.payload.decode())
client.on_message = on_message
client.subscribe("test1", qos=1)
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment