Environment
- Ubuntu 18.04
- Python 2.7.15
- ROS: melodic
Quick start
Installation
$ sudo apt install ros-melodic-rosbridge-server
prepare MQTT broker and client
$ sudo apt-get install mosquitto mosquitto-clients
Install python modules
$ pip install -r requirements.txt
launch node
$ roslaunch mqtt_bridge demo.launch
# with tls options
$ roslaunch mqtt_bridge demo.launch use_tls:=True
Publish to /ping
,
$ rostopic pub /ping std_msgs/Bool "data: true"
and see response to /pong
.
$ rostopic echo /pong
data: True
---
Publish "hello" to /echo
,
$ rostopic pub /echo std_msgs/String "data: 'hello'"
and see response to /back
.
$ rostopic echo /back
data: hello
---
You can also see MQTT messages using mosquitto_sub
$ mosquitto_sub -t '#'
Usage
parameter file (config.yaml):
mqtt:
client:
protocol: 4 # MQTTv311
connection:
host: localhost
port: 1883
keepalive: 60
bridge:
# ping pong
- factory: mqtt_bridge.bridge:RosToMqttBridge
msg_type: std_msgs.msg:Bool
topic_from: /ping
topic_to: ping
- factory: mqtt_bridge.bridge:MqttToRosBridge
msg_type: std_msgs.msg:Bool
topic_from: ping
topic_to: /pong
launch file:
<launch>
<node name="mqtt_bridge" pkg="mqtt_bridge" type="mqtt_bridge_node.py" output="screen">
<rosparam file="/path/to/config.yaml" command="load" />
</node>
</launch>
Configuration
mqtt
Parameters under mqtt
section are used for creating paho's mqtt.Client
and its configuration.
subsections
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.
mqtt private path
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".
serializer and deserializer
mqtt_bridge
uses json
as a serializer in default. But you can also configure other serializers. For example, if you want to use messagepack for serialization, add following configuration.
serializer: msgpack:dumps
deserializer: msgpack:loads
bridges
You can list ROS <--> MQTT tranfer specifications in following format.
bridge:
# ping pong
- factory: mqtt_bridge.bridge:RosToMqttBridge
msg_type: std_msgs.msg:Bool
topic_from: /ping
topic_to: ping
- factory: mqtt_bridge.bridge:MqttToRosBridge
msg_type: std_msgs.msg:Bool
topic_from: ping
topic_to: /pong
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
.
License
This software is released under the MIT License, see LICENSE.txt.