This commit is contained in:
xyj 2023-12-07 09:27:37 +08:00
parent 28cdd733bc
commit 24be9201aa
2 changed files with 184 additions and 177 deletions

53
MQTT.py
View File

@ -13,31 +13,34 @@ def on_connect(client, userdata, flags, rc):
# Message receiving callback
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream":
# 启动推流视频
push_stream(client)
elif data == "close_stream":
# 关闭推流视频
close_stream(client)
elif data == "exec":
# 执行命令
exec_sh(msg, client)
elif data == "update":
# git更新项目和配置文件
update(client)
elif data == "record_list":
# 查看录像列表
get_list_record()
elif data == "record":
# 获取录像
get_record(msg, client)
elif data == "status":
# 查看运行状态
get_status(client)
else:
# 错误类型
client.publish('success', payload='No Such Type', qos=0)
try:
data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream":
# 启动推流视频
push_stream(client)
elif data == "close_stream":
# 关闭推流视频
close_stream(client)
elif data == "exec":
# 执行命令
exec_sh(msg, client)
elif data == "update":
# git更新项目和配置文件
update(client)
elif data == "record_list":
# 查看录像列表
get_list_record()
elif data == "record":
# 获取录像
get_record(msg, client)
elif data == "status":
# 查看运行状态
get_status(client)
else:
# 错误类型
client.publish('success', payload='No Such Type', qos=0)
except Exception as e:
pass
times = 120

View File

@ -35,158 +35,162 @@ def t():
windspeed_send = bytes.fromhex(windspeed_send)
winddirection_send = bytes.fromhex(winddirection_send)
while True:
# if ser.is_open:
ser.write(temp_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
tempbuffer_data = ser.in_waiting
if tempbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(tempbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
airtemp_data = int(return_data_hex[6:10], 16) / 10
airhumi_data = int(return_data_hex[10:14], 16) / 10
time.sleep(5)
ser.write(co2_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
co2buffer_data = ser.in_waiting
if co2buffer_data:
return_data = ser.read(co2buffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
co2_data = int(return_data_hex[6:10], 16)
time.sleep(5)
ser.write(pressure_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
pressurebuffer_data = ser.in_waiting
if pressurebuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(pressurebuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
pressure_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
ser.write(sun_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
sunbuffer_data = ser.in_waiting
if sunbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(sunbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
sun_data = int(return_data_hex[6:14], 16)
time.sleep(5)
# print('send soil directives')
ser.write(soil_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
soilbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if soilbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(soilbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
humidity_data = int(return_data_hex[6:10], 16) / 10
temperature_data = int(return_data_hex[10:14], 16) / 10
electrical_data = int(return_data_hex[14:18], 16) / 10
PH_data = int(return_data_hex[18:22], 16) / 10
time.sleep(5)
# print('send danlinjia directives')
ser.write(danlinjia_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
danlinjiabuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if danlinjiabuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(danlinjiabuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# # 对返回的数据进行解析,获取温度和湿度数据
dan_data = int(return_data_hex[6:10], 16) / 10
lin_data = int(return_data_hex[10:14], 16) / 10
jia_data = int(return_data_hex[14:18], 16) / 10
time.sleep(5)
# print('send rainfall directives')
ser.write(rainfall_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
rainfallbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if rainfallbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(rainfallbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# # 对返回的数据进行解析,获取温度和湿度数据
# print("当前雨量值为:", int(return_data_hex[6:10], 16)/10)#单位mm
rainfall_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
ser.write(windspeed_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
windspeedbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if windspeedbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(windspeedbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# print("当前风速为:", int(return_data_hex[6:10], 16)/10)#单位mm
speedwind_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
# print('send winddirection directives')
ser.write(winddirection_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
winddirectionbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if winddirectionbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(winddirectionbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# print("当前风向为:", int(return_data_hex[10:14], 16))#单位mm
winddirection_data = int(return_data_hex[10:14], 16)
time.sleep(5)
# print('{"name":"%d","name1":"%d"}', 123,456)
# data = [{'ngvhgv': airtemp_data}, {'nvjgvjvj':airhumi_data}]
data = {'ambient_temperature': airtemp_data,
'ambient_humidity': airhumi_data,
'carbon_dioxide': co2_data,
'ambient_air_pressure': pressure_data,
'ambient_lighting': sun_data,
'soil_moisture': humidity_data,
'soil_temperature': temperature_data,
'soil_conductivity': electrical_data,
'soil_PH': PH_data,
'soil_potassium_phosphate_nitrogen': dan_data,
'soil_potassium_phosphate_phosphorus': lin_data,
'soil_potassium_phosphate_potassium': jia_data,
'rainfall': rainfall_data,
'wind_speed': speedwind_data,
'wind_direction': winddirection_data,
'create_time': int(time.time())
}
t2 = LOT_DATA(**data)
client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0)
add(t2)
try:
# if ser.is_open:
ser.write(temp_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
tempbuffer_data = ser.in_waiting
if tempbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(tempbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
airtemp_data = int(return_data_hex[6:10], 16) / 10
airhumi_data = int(return_data_hex[10:14], 16) / 10
time.sleep(5)
ser.write(co2_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
co2buffer_data = ser.in_waiting
if co2buffer_data:
return_data = ser.read(co2buffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
co2_data = int(return_data_hex[6:10], 16)
time.sleep(5)
ser.write(pressure_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
pressurebuffer_data = ser.in_waiting
if pressurebuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(pressurebuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
pressure_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
ser.write(sun_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
sunbuffer_data = ser.in_waiting
if sunbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(sunbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
sun_data = int(return_data_hex[6:14], 16)
time.sleep(5)
# print('send soil directives')
ser.write(soil_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
soilbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if soilbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(soilbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# 对返回的数据进行解析,获取温度和湿度数据
humidity_data = int(return_data_hex[6:10], 16) / 10
temperature_data = int(return_data_hex[10:14], 16) / 10
electrical_data = int(return_data_hex[14:18], 16) / 10
PH_data = int(return_data_hex[18:22], 16) / 10
time.sleep(5)
# print('send danlinjia directives')
ser.write(danlinjia_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
danlinjiabuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if danlinjiabuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(danlinjiabuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# # 对返回的数据进行解析,获取温度和湿度数据
dan_data = int(return_data_hex[6:10], 16) / 10
lin_data = int(return_data_hex[10:14], 16) / 10
jia_data = int(return_data_hex[14:18], 16) / 10
time.sleep(5)
# print('send rainfall directives')
ser.write(rainfall_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
rainfallbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if rainfallbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(rainfallbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# # 对返回的数据进行解析,获取温度和湿度数据
# print("当前雨量值为:", int(return_data_hex[6:10], 16)/10)#单位mm
rainfall_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
ser.write(windspeed_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
windspeedbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if windspeedbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(windspeedbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# print("当前风速为:", int(return_data_hex[6:10], 16)/10)#单位mm
speedwind_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
# print('send winddirection directives')
ser.write(winddirection_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
winddirectionbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if winddirectionbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(winddirectionbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# print("当前风向为:", int(return_data_hex[10:14], 16))#单位mm
winddirection_data = int(return_data_hex[10:14], 16)
time.sleep(5)
# print('{"name":"%d","name1":"%d"}', 123,456)
# data = [{'ngvhgv': airtemp_data}, {'nvjgvjvj':airhumi_data}]
data = {'ambient_temperature': airtemp_data,
'ambient_humidity': airhumi_data,
'carbon_dioxide': co2_data,
'ambient_air_pressure': pressure_data,
'ambient_lighting': sun_data,
'soil_moisture': humidity_data,
'soil_temperature': temperature_data,
'soil_conductivity': electrical_data,
'soil_PH': PH_data,
'soil_potassium_phosphate_nitrogen': dan_data,
'soil_potassium_phosphate_phosphorus': lin_data,
'soil_potassium_phosphate_potassium': jia_data,
'rainfall': rainfall_data,
'wind_speed': speedwind_data,
'wind_direction': winddirection_data,
'create_time': int(time.time())
}
t2 = LOT_DATA(**data)
client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0)
add(t2)
except Exception as e:
print(e)
pass
def on_connect(client, userdata, flags, rc):