120 lines
2.3 KiB
Python
120 lines
2.3 KiB
Python
|
|
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
'''
|
|||
|
|
@project: mqtt
|
|||
|
|
@file: subscriber.py
|
|||
|
|
@version: python3.10
|
|||
|
|
@Author: jcq
|
|||
|
|
@Date : 2024/12/11
|
|||
|
|
@
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
import json
|
|||
|
|
from loguru import logger
|
|||
|
|
import paho.mqtt.client as mqtt
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 101.133.163.127
|
|||
|
|
# 1883
|
|||
|
|
# admin
|
|||
|
|
# admin##123
|
|||
|
|
|
|||
|
|
# MQTT Broker 地址和端口
|
|||
|
|
broker = "101.133.163.127" # 在远程服务器上可改为对应的 IP 地址
|
|||
|
|
|
|||
|
|
# broker = "localhost" # 在远程服务器上可改为对应的 IP 地址
|
|||
|
|
port = 1883
|
|||
|
|
topic = "test/topic"
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
logger.add(sys.stderr,format = "{time}-{level}-{message}",level="INFO")
|
|||
|
|
logger.add("log/mqtt_subscriber.log",rotation="1 MB",retention='5 days')
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
def on_connect(client,userdata,flags,rc):
|
|||
|
|
print("rc:",rc)
|
|||
|
|
logger.info("connected with result code" + str(rc))
|
|||
|
|
client.subscribe(topic)
|
|||
|
|
print(topic)
|
|||
|
|
|
|||
|
|
|
|||
|
|
# # 消息回调函数
|
|||
|
|
# def on_message(client, userdata, message):
|
|||
|
|
|
|||
|
|
# print('message')
|
|||
|
|
# print(message)
|
|||
|
|
|
|||
|
|
|
|||
|
|
# print(f"Received message: {str(message.payload.decode('utf-8'))} on topic: {message.topic}")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 当接收到MQTT消息时,回调函数
|
|||
|
|
def on_message(client, userdata, data):
|
|||
|
|
# # 将消息解码为JSON格式
|
|||
|
|
# payload = msg.payload.decode('utf-8')
|
|||
|
|
# data = json.loads(payload)
|
|||
|
|
|
|||
|
|
print("line58",data)
|
|||
|
|
|
|||
|
|
# 解析位姿信息
|
|||
|
|
lon = data.get("lon")
|
|||
|
|
lat = data.get("lat")
|
|||
|
|
alt = data.get("alt")
|
|||
|
|
yaw = data.get("yaw")
|
|||
|
|
pitch = data.get("pitch")
|
|||
|
|
roll = data.get("roll")
|
|||
|
|
|
|||
|
|
# 打印无人机的位姿信息
|
|||
|
|
print(f"Longitude: {lon}, Latitude: {lat}, Altitude: {alt}")
|
|||
|
|
print(f"Yaw: {yaw}, Pitch: {pitch}, Roll: {roll}")
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 创建客户端
|
|||
|
|
client = mqtt.Client()
|
|||
|
|
|
|||
|
|
print("line80")
|
|||
|
|
client.on_connect = on_connect
|
|||
|
|
|
|||
|
|
print("line83")
|
|||
|
|
|
|||
|
|
# 设置回调函数
|
|||
|
|
client.on_message = on_message
|
|||
|
|
print("line87")
|
|||
|
|
# 连接到 Broker
|
|||
|
|
client.connect(broker, port)
|
|||
|
|
print("line90")
|
|||
|
|
# client.loop_forever()
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 保持订阅状态
|
|||
|
|
client.loop_forever()
|
|||
|
|
|
|||
|
|
print("line98")
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
# # 订阅主题
|
|||
|
|
# client.subscribe(topic)
|
|||
|
|
|
|||
|
|
# client.loop_start()
|
|||
|
|
|
|||
|
|
# try:
|
|||
|
|
|
|||
|
|
# while True:
|
|||
|
|
# pass
|
|||
|
|
# except KeyboardInterrupt:
|
|||
|
|
# logger.info("subscriber stopped by user")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# finally:
|
|||
|
|
# client.loop_stop()
|
|||
|
|
# client.disconnect()
|