ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I didn't used the MQTT bridge, I did some workaorund. Might not be the best method import rclpy from rclpy.node import Node from rclpy import qos from rclpy.qos import QoSProfile import paho.mqtt.client as mqtt

from std_msgs.msg import String
import time

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()

I didn't used the MQTT bridge, I did some workaorund. workaround. Might not be the best method import rclpy from rclpy.node import Node from rclpy import qos from rclpy.qos import QoSProfile import paho.mqtt.client as mqtt

from std_msgs.msg import String
import time

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()

I didn't used the MQTT bridge, I did some workaround. Might not be the best method import rclpy from rclpy.node import Node from rclpy import qos from rclpy.qos import QoSProfile import paho.mqtt.client as mqttmethod

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()

I didn't used use the MQTT bridge, I did some workaround. 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()