mqtt_bridge provides a functionality to bridge between ROS and MQTT in bidirectional.
mqtt_bridge
uses ROS message as its protocol. Messages from ROS are serialized by json (or messagepack) for MQTT, and messages from MQTT are deserialized for ROS topic. So MQTT messages should be ROS message compatible. (We use rosbridge_library.internal.message_conversion
for message conversion.)
This limitation can be overcome by defining custom bridge class, though.
$ sudo apt install python3-pip
$ sudo apt install ros-foxy-rosbridge-library
$ sudo apt install mosquitto mosquitto-clients
$ pip3 install -r requirements.txt
$ ros2 launch mqtt_bridge demo.launch.py
Publish to /ping
,
$ ros2 topic pub /ping std_msgs/Bool "data: true"
and see response to /pong
.
$ ros2 topic echo /pong
data: True
---
Publish "hello" to /echo
,
$ ros2 topic pub /echo std_msgs/String "data: 'hello'"
and see response to /back
.
$ ros2 topic echo /back
data: hello
---
You can also see MQTT messages using mosquitto_sub
$ mosquitto_sub -t '#'
parameter file (config.yaml):
mqtt:
client:
protocol: 4 # MQTTv311
connection:
host: localhost
port: 1883
keepalive: 60
n_bridge: 2 # specifying number of bridges required
bridge:
# factory, msg_type, topic_from, topic_to
bridge1: ["mqtt_bridge.bridge:RosToMqttBridge","std_msgs.msg:Bool","/ping","ping"]
bridge2: ["mqtt_bridge.bridge:MqttToRosBridge","std_msgs.msg:Bool","ping","/pong"]
# ros2 parser cannot parse the earlier config type and therefore it has to be changed as above
you can use any msg types like sensor_msgs.msg:Imu
.
launch file:
# to configure node configs
config = os.path.join(
get_package_share_directory('mqtt_bridge'),
'config',
'demo_params.yaml'
)
Parameters under mqtt
section are used for creating paho's mqtt.Client
and its configuration.
client
: used formqtt.Client
constructortls
: used for tls configurationaccount
: used for username and password configurationmessage
: used for MQTT message configurationuserdata
: used for MQTT userdata configurationwill
: used for MQTT's will configuration
See mqtt_bridge.mqtt_client
for detail.
If mqtt/private_path
parameter is set, leading ~/
in MQTT topic path will be replaced by this value. For example, if mqtt/pivate_path
is set as "device/001", MQTT path "~/value" will be converted to "device/001/value".
mqtt_bridge
uses msgpack
as a serializer by default. But you can also configure other serializers. For example, if you want to use json for serialization, add following configuration.
serializer: json:dumps
deserializer: json:loads
You can list ROS <--> MQTT tranfer specifications in following format.
n_bridge: 2 # specifying number of bridges required
bridge:
# factory, msg_type, topic_from, topic_to
bridge1: ["mqtt_bridge.bridge:RosToMqttBridge","std_msgs.msg:Bool","/ping","ping"]
bridge2: ["mqtt_bridge.bridge:MqttToRosBridge","std_msgs.msg:Bool","ping","/pong"]
# ros2 parser cannot parse the earlier config type and therefore it has to be changed as above
factory
: bridge class for transfering message from ROS to MQTT, and vise versa.msg_type
: ROS Message type transfering through the bridge.topic_from
: topic incoming from (ROS or MQTT)topic_to
: topic outgoing to (ROS or MQTT)
Also, you can create custom bridge class by inheriting mqtt_brige.bridge.Bridge
.
This software is released under the MIT License, see LICENSE.txt.