Skip to content

Instantly share code, notes, and snippets.

@teshanshanuka
Last active June 22, 2020 19:25
Show Gist options
  • Save teshanshanuka/0b6475e2a714e993c0062d3170106b3d to your computer and use it in GitHub Desktop.
Save teshanshanuka/0b6475e2a714e993c0062d3170106b3d to your computer and use it in GitHub Desktop.
ROS mqtt_bridge client for `msgpack` packed payloads
# Author: Teshan Liyanage <[email protected]>
# Original mqtt client code from https://www.eclipse.org/paho/clients/python/docs/
import paho.mqtt.client as mqtt
import logging
import msgpack
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("ping")
def on_message(client, userdata, msg):
try:
print(msg.topic+" "+str(msg.payload))
except UnicodeDecodeError:
print(msg.topic+" "+str(msgpack.loads(msg.payload)))
client = mqtt.Client("", True, None, mqtt.MQTTv31)
client.on_connect = on_connect
client.on_message = on_message
client.enable_logger(log)
client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment