Last active
June 22, 2020 19:25
-
-
Save teshanshanuka/0b6475e2a714e993c0062d3170106b3d to your computer and use it in GitHub Desktop.
ROS mqtt_bridge client for `msgpack` packed payloads
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
# 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