|
|
|
|
@ -1,55 +1,36 @@
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#coding:utf-8
|
|
|
|
|
# license removed for brevity
|
|
|
|
|
import ssl
|
|
|
|
|
import rospy
|
|
|
|
|
from std_msgs.msg import String
|
|
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
mqtt_config = {"host": "192.168.0.48", "port": 1883, "topic": "data/pub"}
|
|
|
|
|
# Ros
|
|
|
|
|
def ros_pub(dataJson):
|
|
|
|
|
global publisher, rate
|
|
|
|
|
# data = json.loads(dataJson)
|
|
|
|
|
publisher.publish(dataJson) #將date字串發布到topic
|
|
|
|
|
rate.sleep()
|
|
|
|
|
# print(f"publish data {data}")
|
|
|
|
|
|
|
|
|
|
def on_connect(self, userdata, flags, rc):
|
|
|
|
|
print("Connected with result code " + str(rc))
|
|
|
|
|
client.subscribe(topic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_message(self, userdata, msg):
|
|
|
|
|
print(f"msg.topic {msg.payload.decode('utf-8')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# MQTT
|
|
|
|
|
def initialise_clients(clientname):
|
|
|
|
|
def initialise_clients(cname):
|
|
|
|
|
# callback assignment
|
|
|
|
|
initialise_client = mqtt.Client(clientname, False)
|
|
|
|
|
initialise_client.topic_ack = []
|
|
|
|
|
initialise_client = mqtt.Client(cname, True) # don't use clean session
|
|
|
|
|
return initialise_client
|
|
|
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
|
print("Connected with result code " + str(rc))
|
|
|
|
|
client.subscribe(mqtt_config["topic"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
|
|
|
|
print(msg.payload.decode('utf-8'))
|
|
|
|
|
ros_pub(msg.payload.decode('utf-8'))
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# Mqtt
|
|
|
|
|
client = initialise_clients("nano")
|
|
|
|
|
client.on_connect = on_connect
|
|
|
|
|
client.on_message = on_message
|
|
|
|
|
client.connect(mqtt_config["host"], mqtt_config["port"], 60)
|
|
|
|
|
# client.loop_start()
|
|
|
|
|
|
|
|
|
|
# Ros
|
|
|
|
|
Mqtt_Node = 'publisher_py'
|
|
|
|
|
rospy.init_node(Mqtt_Node)
|
|
|
|
|
# initialize Ros node
|
|
|
|
|
topicName = 'mqtt_msg'
|
|
|
|
|
publisher = rospy.Publisher(topicName,String,queue_size=10)
|
|
|
|
|
rate = rospy.Rate(10)
|
|
|
|
|
|
|
|
|
|
client.loop_forever()
|
|
|
|
|
|
|
|
|
|
host = "192.168.0.48"
|
|
|
|
|
port = 1883
|
|
|
|
|
topic = "data/pub"
|
|
|
|
|
|
|
|
|
|
client = initialise_clients("python_sub")
|
|
|
|
|
|
|
|
|
|
client.on_connect = on_connect
|
|
|
|
|
|
|
|
|
|
client.on_message = on_message
|
|
|
|
|
|
|
|
|
|
client.connect(host, port, 60)
|
|
|
|
|
|
|
|
|
|
client.loop_forever()
|
|
|
|
|
|
|
|
|
|
# mqtt connect code list
|
|
|
|
|
# 0: Connection successful
|
|
|
|
|
|