From b498146609f861a71f7c3d0231554b0050407a40 Mon Sep 17 00:00:00 2001 From: tony <58881058+RangeOfGlitching@users.noreply.github.com> Date: Mon, 30 May 2022 16:26:14 +0800 Subject: [PATCH] Create command.py --- Stream/localRosData/command.py | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Stream/localRosData/command.py diff --git a/Stream/localRosData/command.py b/Stream/localRosData/command.py new file mode 100644 index 0000000..9fa8ff4 --- /dev/null +++ b/Stream/localRosData/command.py @@ -0,0 +1,58 @@ +# #!/usr/bin/env python3 +# #coding:utf-8 +# # license removed for brevity +# import paho.mqtt.client as mqtt +# import ssl +# import rospy +from std_msgs.msg import String +import paho.mqtt.client as mqtt +import json + +# # Ros +# def ros_pub(dataJson): +# global publisher, rate +# # data = json.loads(dataJson) +# publisher.publish(dataJson) #將date字串發布到topic +# rate.sleep() +# # print(f"publish data {data}") + +# MQTT +def on_connect(self, userdata, flags, rc): + print("Connected with result code " + str(rc)) + client.subscribe(topicBroadcast) + + +def on_message(self, userdata, msg): + msg = msg.payload.decode('utf-8') + print(f"msg.topic {msg}") + ros_pub(msg.payload.decode('utf-8')) + + + +def initialise_clients(clientName): + # callback assignment + initialise_client = mqtt.Client(clientName, False) + initialise_client.topic_ack = [] + return initialise_client + +if __name__ == '__main__': + # Mqtt + mqtt_config = {"host": "192.168.100.157", "port": 1883, "topic": "cmd/broadcast"} + client = initialise_clients("receiver") + client.on_connect = on_connect + client.on_message = on_message + client.connect(mqtt_config["host"], mqtt_config["port"], 60) + + + + # # Ros + # Mqtt_Node = 'publisher_py' + # rospy.init_node("cmd_receiver") + # # initialize Ros node + # topicName = 'uav_command' + # publisher = rospy.Publisher(topicName,String,queue_size=10) + # rate = rospy.Rate(10) + + + + client.loop_forever()