Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active August 29, 2015 14:23
Show Gist options
  • Save stephenlb/b4ea0034839ccbc2aa03 to your computer and use it in GitHub Desktop.
Save stephenlb/b4ea0034839ccbc2aa03 to your computer and use it in GitHub Desktop.
MQTT PubNub Demo
#!/usr/bin/python
from mosquitto import Mosquitto
publish_key = "demo"
subscribe_key = "demo"
channel_name = "F"
client_uuid = "2fb96def5"
mqtt_hostname = "mqtt.pubnub.com"
mqtt_connect = publish_key + "/" + subscribe_key + "/" + client_uuid
mqtt_topic = publish_key + "/" + subscribe_key + "/" + channel_name
mosq_object = Mosquitto(mqtt_connect)
def on_message( mosq, obj, msg ):
print( msg.payload, msg.topic )
mosq_object.on_message = on_message
mosq_object.connect(mqtt_hostname)
mosq_object.publish( mqtt_topic, "Hello World!" )
mosq_object.subscribe(mqtt_topic)
mosq_object.loop_forever()
@stephenlb
Copy link
Author

Execution Example:

Install Dependencies

pip install mosquitto
curl -Ls https://gist.githubusercontent.com/stephenlb/b4ea0034839ccbc2aa03/raw/df081c8a501284aada19730e910dc1c6da61a780/mqtt.py | python

@WilliamKinaan
Copy link

Kindly where the documentation for the constructer Mosquitto("some string here")

what does the parameter for that constructor mean? because in the mqtt for paho project, the constructor takes 4 parameters, and the one that is close to your case is the client id. Thus, do you represent your client s using that kind of contacination ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment