Ask Your Question
2

Can we use MQTT in ROS2? [closed]

asked 2021-01-11 08:30:03 -0500

Flash gravatar image

It seems ROS1 had MQTT bridge but nothing specific was mentioned about ROS2 in it. Can we use MQTT bridge for ros2 or is their any other way to do it?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Flash
close date 2021-10-07 15:01:56.651124

Comments

Hi, Have you solved this issue?

flash_27 gravatar image flash_27  ( 2021-06-29 10:44:31 -0500 )edit
3

@Flash For future readers, please add links to what you have found. My guess is you're referring to http://wiki.ros.org/mqtt_bridge as " MQTT bridge".

130s gravatar image 130s  ( 2021-06-29 10:47:17 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
0

answered 2021-06-29 12:36:38 -0500

Flash gravatar image

updated 2021-06-29 12:42:16 -0500

I didn't use the MQTT bridge, I did some workaround to receive information from the webpage I used MQTT subscribe feature but to publish info to the webpage I used the MySQL database instead of MQTT. Might not be the best method

from std_msgs.msg import String
import time
import rclpy
from rclpy.node import Node
from rclpy import qos
from rclpy.qos import QoSProfile
import paho.mqtt.client as mqtt

class SubscribeMqtt(Node):

    def __init__(self):
        super().__init__('subscribe_mqtt')
        State = QoSProfile(durability=qos.QoSDurabilityPolicy.TRANSIENT_LOCAL,
                       reliability=qos.QoSReliabilityPolicy.RELIABLE, history=qos.QoSHistoryPolicy.KEEP_LAST, depth=20)
        Streaming = QoSProfile(durability=qos.QoSDurabilityPolicy.VOLATILE,
                           reliability=qos.QoSReliabilityPolicy.BEST_EFFORT, history=qos.QoSHistoryPolicy.KEEP_LAST, depth=1)
        self.declare_parameters(
            namespace='',
            parameters=[
                ('host', None),
                ('port', None)])
        self.ros2_publish = self.create_publisher(
            String, 'ros2/topic', Streaming)

        self.client = mqtt.Client()
        self.client.on_message = self.topic_get
        self.host = "hostname"
        self.port = "port"
        self.client.connect(self.host, self.port)
        self.client.subscribe("mqtt/topic", qos=1) #to subscribe more than one topic add more subscribe lines
        self.client.loop_forever()

    def topic_get(self, client, userdata, msg):
        send = String()
        topic = msg.topic
        message = msg.payload.decode("utf-8")

        if topic == "mqtt_topic":
            self.ros2_publish.publish(send)



def main(args=None):
    rclpy.init(args=args)
    subscribe_mqtt = SubscribeMqtt()
    rclpy.spin(subscribe_mqtt)

    subscribe_mqtt.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
edit flag offensive delete link more

Comments

1

but to publish info to the webpage I used the MySQL database instead of MQTT

Just curious. Is it because you ran into issues with MQTT publishing, or because you already had SQL stuff set up and decided to go with that?

abhishek47 gravatar image abhishek47  ( 2021-06-30 00:19:15 -0500 )edit

I didn't try publishing using MQTT and also data transmitted to the webpage was too large and publishing continuously was not reliable with memory usage and also flickering behavior on the webpage. Using a database gave the frontend to freely work with incoming data and data sending to the backend was minimal and only specific calls for that only subscriber was required.

Flash gravatar image Flash  ( 2021-06-30 07:53:58 -0500 )edit
2

answered 2021-01-12 04:57:34 -0500

crnewton gravatar image

updated 2021-01-12 04:57:43 -0500

You can just connect to a mqtt broker from your node code.It's not a bridge so you have to add a layer to handle the mqtt messages in ROS. (Or you can migrate the MQTT_bridge to ROS.)

edit flag offensive delete link more
0

answered 2021-06-29 10:50:06 -0500

130s gravatar image

Assuming http://wiki.ros.org/mqtt_bridge is what OP is asking about, there's a ticket on its dev repo https://github.com/groove-x/mqtt_brid... that hasn't got a response yet so I assume the pkg is not tailored yet for ros2.

For general connectivity with mqttwithout using that specific pkg, the answer https://answers.ros.org/question/3692... seems reasonable.

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2021-01-11 08:30:03 -0500

Seen: 1,635 times

Last updated: Jun 29 '21