mqtt 测试ok
This commit is contained in:
parent
1cace9abd7
commit
ec994fd2c1
|
|
@ -0,0 +1,94 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
@project: mqtt
|
||||||
|
@file: publisher.py
|
||||||
|
@version: python3.10
|
||||||
|
@Author: jcq
|
||||||
|
@Date : 2024/12/11
|
||||||
|
@
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
import json
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
from simulation import generate_fight_data ,generate_data
|
||||||
|
|
||||||
|
|
||||||
|
# 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("logs/mqtt.log",rotation="1 MB",retention='5 days')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 创建客户端
|
||||||
|
client = mqtt.Client()
|
||||||
|
|
||||||
|
# 连接到 Broker
|
||||||
|
client.connect(broker, port)
|
||||||
|
|
||||||
|
# 发布消息
|
||||||
|
|
||||||
|
#发布假数据做测试用
|
||||||
|
try:
|
||||||
|
|
||||||
|
# 设置初始经纬度
|
||||||
|
initial_latitude = 31.8250608
|
||||||
|
initial_longitude = 118.7630802
|
||||||
|
|
||||||
|
delta = 0.5
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 发布消息
|
||||||
|
try:
|
||||||
|
|
||||||
|
|
||||||
|
# 设置初始经纬度
|
||||||
|
initial_latitude = 31.8250608
|
||||||
|
initial_longitude = 118.7630802
|
||||||
|
|
||||||
|
|
||||||
|
delta_latitude = 0.5
|
||||||
|
delta_longitude = 0.5
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(100):
|
||||||
|
message = f"Hello MQTT {i}"
|
||||||
|
|
||||||
|
|
||||||
|
# 开始生成数据
|
||||||
|
flight_data_json = generate_fight_data(initial_latitude, initial_longitude)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
initial_latitude += delta_latitude
|
||||||
|
initial_longitude += delta_longitude
|
||||||
|
|
||||||
|
messa = json.dumps(flight_data_json)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
client.publish(topic, messa)
|
||||||
|
print(f"Published: {message}")
|
||||||
|
print(f"Published: {messa}")
|
||||||
|
time.sleep(0.5)
|
||||||
|
finally:
|
||||||
|
client.disconnect()
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from math import cos
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
logger.add(sys.stderr,format = "{time}-{level}-{message}",level = "INFO")
|
||||||
|
logger.add("logs/sim_flight.log",rotation = "1 MB",retention = '7 days')
|
||||||
|
|
||||||
|
|
||||||
|
def generate_fight_data(inital_lat,initial_lon,sat_count):
|
||||||
|
|
||||||
|
# 飞机初始状态
|
||||||
|
altitude = 3.256 # 初始高度
|
||||||
|
horizontal_speed = 0.86 *100 # 初始水平速度
|
||||||
|
vertical_speed = 0.04447911 *100 # 初始竖直速度
|
||||||
|
yaw = random.uniform(0, 360) # 随机偏航角
|
||||||
|
roll = random.uniform(-1, 1) # 随机滚转角
|
||||||
|
pitch = random.uniform(-1, 1) # 随机俯仰角
|
||||||
|
armed = "false" # 解锁状态
|
||||||
|
mode = "stabilize" # 飞机模式
|
||||||
|
dist_to_home = random.uniform(0, 10) # 随机距离家
|
||||||
|
mileage = 0 # 飞行总距离
|
||||||
|
gps_signal = 4 # GPS状态
|
||||||
|
sat_count = 29 # GPS获取卫星数
|
||||||
|
voltage = round(random.uniform(24, 26), 3) # 随机电压
|
||||||
|
landing_target_x = 0 # 视觉误差x轴偏差值
|
||||||
|
landing_target_y = 0 # 视觉误差y轴偏差值
|
||||||
|
landing_target_z = 0 # 视觉误差z轴偏差值
|
||||||
|
|
||||||
|
timestamp = int(datetime.utcnow().timestamp() * 1000)
|
||||||
|
|
||||||
|
|
||||||
|
# 生成数据字典
|
||||||
|
flight_data = {
|
||||||
|
# "lon": round(initial_lon, 7),
|
||||||
|
# "lat": round(inital_lat, 7),
|
||||||
|
|
||||||
|
"lon": inital_lat,
|
||||||
|
"lat": initial_lon,
|
||||||
|
|
||||||
|
"gpssingal": gps_signal,
|
||||||
|
"satcount": sat_count,
|
||||||
|
"alt": round(altitude, 3),
|
||||||
|
"hspeed": round(horizontal_speed, 2),
|
||||||
|
"vspeed": round(vertical_speed, 8),
|
||||||
|
"ysingal": 0,
|
||||||
|
"tsingal": 0,
|
||||||
|
"voltage": round(voltage, 3),
|
||||||
|
"flytime": 0,
|
||||||
|
"datetime": timestamp,
|
||||||
|
"yaw": round(yaw, 6),
|
||||||
|
"roll": round(roll, 8),
|
||||||
|
"pitch": round(pitch, 8),
|
||||||
|
"armed": armed,
|
||||||
|
"mode": mode,
|
||||||
|
"distToHome": round(dist_to_home, 6),
|
||||||
|
"deviceid": "THJSQ03A2302KSPYGJ2G",
|
||||||
|
"mileage": str(mileage),
|
||||||
|
"altasl": round(altitude + 18, 2), # 模拟GPS1绝对高度
|
||||||
|
"altasl2": round(altitude - 24, 2), # 模拟GPS2绝对高度
|
||||||
|
"landing_target_x": landing_target_x,
|
||||||
|
"landing_target_y": landing_target_y,
|
||||||
|
"landing_target_z": landing_target_z,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#输出JSON格式数据
|
||||||
|
print(json.dumps(flight_data,indent=4))
|
||||||
|
logger.info("模拟飞行数据" + str(flight_data))
|
||||||
|
|
||||||
|
|
||||||
|
return flight_data
|
||||||
|
|
||||||
|
|
||||||
|
def generate_data():
|
||||||
|
while True:
|
||||||
|
# 生成一个随机数,范围在 0 到 100 之间
|
||||||
|
data = random.uniform(0, 100)
|
||||||
|
print(f"Generated data: {data:.2f}")
|
||||||
|
|
||||||
|
# 暂停 0.5 秒
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
generate_data()
|
||||||
|
|
||||||
|
|
||||||
|
# # 设置初始经纬度
|
||||||
|
# initial_latitude = 31.8250608
|
||||||
|
# initial_longitude = 118.7630802
|
||||||
|
|
||||||
|
# # 开始生成数据
|
||||||
|
# flight_data_json = generate_fight_data(initial_latitude, initial_longitude)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,119 @@
|
||||||
|
|
||||||
|
# -*- 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()
|
||||||
Loading…
Reference in New Issue