From 9bf94dd68d2f7d651c7b04530c4e69af88b982ec Mon Sep 17 00:00:00 2001 From: tony <58881058+RangeOfGlitching@users.noreply.github.com> Date: Mon, 30 May 2022 17:40:19 +0800 Subject: [PATCH] Update command.py --- 1toN/command.py | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/1toN/command.py b/1toN/command.py index 9fa8ff4..d595f9a 100644 --- a/1toN/command.py +++ b/1toN/command.py @@ -1,25 +1,26 @@ # #!/usr/bin/env python3 # #coding:utf-8 -# # license removed for brevity -# import paho.mqtt.client as mqtt -# import ssl -# import rospy +# 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}") + + # 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) + client.subscribe("cmd/broadcast") def on_message(self, userdata, msg): @@ -28,31 +29,28 @@ def on_message(self, userdata, 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"} + mqtt_config = {"host": "192.168.50.109", "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) - - + # 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() +