Skip to content

Instantly share code, notes, and snippets.

@AliYmn
Created February 18, 2017 23:23
Show Gist options
  • Save AliYmn/ec31f94301ac00d351d39ed114d65d4e to your computer and use it in GitHub Desktop.
Save AliYmn/ec31f94301ac00d351d39ed114d65d4e to your computer and use it in GitHub Desktop.
import paho.mqtt.client as mqtt
# This is the Publisher
client = mqtt.Client()
client.connect("ip_Adress",1883,60)
client.publish("temp/random", 5)
client.disconnect()
# -------------------------------
import paho.mqtt.client as mqtt
# This is the Subscriber
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("temp/random")
def on_message(client, userdata, msg):
if msg.payload.decode() == "Hello world!":
print("Yes!")
client.disconnect()
client = mqtt.Client()
client.connect("server_ip", 1883, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
# Source = http://www.ev3dev.org/docs/tutorials/sending-and-receiving-messages-with-mqtt/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment