This commit is contained in:
xyj 2023-12-05 18:30:33 +08:00
parent d1dee12232
commit aa476caddb
2 changed files with 175 additions and 173 deletions

View File

@ -7,180 +7,180 @@ from db.models.log_data_model import LOT_DATA
if __name__ == '__main__': if __name__ == '__main__':
# temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令 temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
# co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令 co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
# pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令 pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
# sun_send = '06 03 01 FA 00 02 E4 71 ' # 光照查询指令 sun_send = '06 03 01 FA 00 02 E4 71 ' # 光照查询指令
#
# soil_send = '02 03 00 00 00 04 44 3A' # 土壤查询指令 soil_send = '02 03 00 00 00 04 44 3A' # 土壤查询指令
# danlinjia_send = '02 03 00 04 00 03 44 39' # 氮磷钾查询指令 danlinjia_send = '02 03 00 04 00 03 44 39' # 氮磷钾查询指令
#
# rainfall_send = '03 03 00 00 00 01 85 E8' # 雨量查询指令 rainfall_send = '03 03 00 00 00 01 85 E8' # 雨量查询指令
#
# windspeed_send = '04 03 00 00 00 02 C4 5E' # 风速查询指令 windspeed_send = '04 03 00 00 00 02 C4 5E' # 风速查询指令
# winddirection_send = '05 03 00 00 00 02 C5 8F' # 风向查询指令 winddirection_send = '05 03 00 00 00 02 C5 8F' # 风向查询指令
# ser = serial.Serial("/dev/ttyS2", 9600) ser = serial.Serial("/dev/ttyS2", 9600)
#
# # 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b' # 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b'
# temp_send = bytes.fromhex(temp_send) temp_send = bytes.fromhex(temp_send)
# co2_send = bytes.fromhex(co2_send) co2_send = bytes.fromhex(co2_send)
# pressure_send = bytes.fromhex(pressure_send) pressure_send = bytes.fromhex(pressure_send)
# sun_send = bytes.fromhex(sun_send) sun_send = bytes.fromhex(sun_send)
# soil_send = bytes.fromhex(soil_send) soil_send = bytes.fromhex(soil_send)
# danlinjia_send = bytes.fromhex(danlinjia_send) danlinjia_send = bytes.fromhex(danlinjia_send)
# rainfall_send = bytes.fromhex(rainfall_send) rainfall_send = bytes.fromhex(rainfall_send)
# windspeed_send = bytes.fromhex(windspeed_send) windspeed_send = bytes.fromhex(windspeed_send)
# winddirection_send = bytes.fromhex(winddirection_send) winddirection_send = bytes.fromhex(winddirection_send)
while True: while True:
# # if ser.is_open: # if ser.is_open:
# ser.write(temp_send) ser.write(temp_send)
# time.sleep(1) time.sleep(1)
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 # 获取返回的缓冲data,获取的是buffer_data的长度 9
# tempbuffer_data = ser.in_waiting tempbuffer_data = ser.in_waiting
# if tempbuffer_data: if tempbuffer_data:
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8' # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
# return_data = ser.read(tempbuffer_data) return_data = ser.read(tempbuffer_data)
# # 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
# return_data_hex = str(return_data.hex()) 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())
# # 对返回的数据进行解析,获取温度和湿度数据 # # 对返回的数据进行解析,获取温度和湿度数据
# airtemp_data = int(return_data_hex[6:10], 16) / 10 dan_data = int(return_data_hex[6:10], 16) / 10
# airhumi_data = int(return_data_hex[10:14], 16) / 10 lin_data = int(return_data_hex[10:14], 16) / 10
# time.sleep(5) jia_data = int(return_data_hex[14:18], 16) / 10
# ser.write(co2_send) time.sleep(5)
# time.sleep(1) # print('send rainfall directives')
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 ser.write(rainfall_send)
# co2buffer_data = ser.in_waiting time.sleep(1)
# if co2buffer_data: # 获取返回的缓冲data,获取的是buffer_data的长度 9
# return_data = ser.read(co2buffer_data) rainfallbuffer_data = ser.in_waiting
# # 二进制转换为16进制010304010802307b79 # print(buffer_data, 'buffer_data')
# return_data_hex = str(return_data.hex()) 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)
# # 对返回的数据进行解析,获取温度和湿度数据 # # 对返回的数据进行解析,获取温度和湿度数据
# co2_data = int(return_data_hex[6:10], 16) # print("当前雨量值为:", int(return_data_hex[6:10], 16)/10)#单位mm
# time.sleep(5) rainfall_data = int(return_data_hex[6:10], 16) / 10
# ser.write(pressure_send) time.sleep(5)
# time.sleep(1) ser.write(windspeed_send)
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 time.sleep(1)
# pressurebuffer_data = ser.in_waiting # 获取返回的缓冲data,获取的是buffer_data的长度 9
# if pressurebuffer_data: windspeedbuffer_data = ser.in_waiting
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8' # print(buffer_data, 'buffer_data')
# return_data = ser.read(pressurebuffer_data) if windspeedbuffer_data:
# # 二进制转换为16进制010304010802307b79 # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
# return_data_hex = str(return_data.hex()) return_data = ser.read(windspeedbuffer_data)
# # 对返回的数据进行解析,获取温度和湿度数据 # print('返回的数据2进制:', return_data)
# pressure_data = int(return_data_hex[6:10], 16) / 10 # 二进制转换为16进制010304010802307b79
# time.sleep(5) return_data_hex = str(return_data.hex())
# ser.write(sun_send) # print('返回的数据转换为16进制:', return_data_hex)
# time.sleep(1) # print("当前风速为:", int(return_data_hex[6:10], 16)/10)#单位mm
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 speedwind_data = int(return_data_hex[6:10], 16) / 10
# sunbuffer_data = ser.in_waiting time.sleep(5)
# if sunbuffer_data: # print('send winddirection directives')
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8' ser.write(winddirection_send)
# return_data = ser.read(sunbuffer_data) time.sleep(1)
# # 二进制转换为16进制010304010802307b79 # 获取返回的缓冲data,获取的是buffer_data的长度 9
# return_data_hex = str(return_data.hex()) winddirectionbuffer_data = ser.in_waiting
# sun_data = int(return_data_hex[6:14], 16) # print(buffer_data, 'buffer_data')
# time.sleep(5) if winddirectionbuffer_data:
# # print('send soil directives') # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
# ser.write(soil_send) return_data = ser.read(winddirectionbuffer_data)
# time.sleep(1) # print('返回的数据2进制:', return_data)
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 # 二进制转换为16进制010304010802307b79
# soilbuffer_data = ser.in_waiting return_data_hex = str(return_data.hex())
# # print(buffer_data, 'buffer_data') # print('返回的数据转换为16进制:', return_data_hex)
# if soilbuffer_data: # print("当前风向为:", int(return_data_hex[10:14], 16))#单位mm
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8' winddirection_data = int(return_data_hex[10:14], 16)
# return_data = ser.read(soilbuffer_data) time.sleep(5)
# # 二进制转换为16进制010304010802307b79 # print('{"name":"%d","name1":"%d"}', 123,456)
# return_data_hex = str(return_data.hex()) # data = [{'ngvhgv': airtemp_data}, {'nvjgvjvj':airhumi_data}]
# # 对返回的数据进行解析,获取温度和湿度数据 data = {'ambient_temperature': airtemp_data,
# humidity_data = int(return_data_hex[6:10], 16) / 10 'ambient_humidity': airhumi_data,
# temperature_data = int(return_data_hex[10:14], 16) / 10 'carbon_dioxide': co2_data,
# electrical_data = int(return_data_hex[14:18], 16) / 10 'ambient_air_pressure': pressure_data,
# PH_data = int(return_data_hex[18:22], 16) / 10 'ambient_lighting': sun_data,
# time.sleep(5) 'soil_moisture': humidity_data,
# # print('send danlinjia directives') 'soil_temperature': temperature_data,
# ser.write(danlinjia_send) 'soil_conductivity': electrical_data,
# time.sleep(1) 'soil_PH': PH_data,
# # 获取返回的缓冲data,获取的是buffer_data的长度 9 'soil_potassium_phosphate_nitrogen': dan_data,
# danlinjiabuffer_data = ser.in_waiting 'soil_potassium_phosphate_phosphorus': lin_data,
# # print(buffer_data, 'buffer_data') 'soil_potassium_phosphate_potassium': jia_data,
# if danlinjiabuffer_data: 'rainfall': rainfall_data,
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8' 'wind_speed': speedwind_data,
# return_data = ser.read(danlinjiabuffer_data) 'wind_direction': winddirection_data,
# # print('返回的数据2进制:', return_data) 'create_time': int(time.time())
# # 二进制转换为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())
# }
data = {'ambient_temperature': 1, data = {'ambient_temperature': 1,
'ambient_humidity': 1, 'ambient_humidity': 1,
'carbon_dioxide': 1, 'carbon_dioxide': 1,

2
test.py Normal file
View File

@ -0,0 +1,2 @@
from db.base import Base, engine
Base.metadata.create_all(bind=engine)