This commit is contained in:
xyj 2023-12-01 18:00:11 +08:00
parent b6aa06f5cd
commit 4397b75fa2
10 changed files with 315 additions and 191 deletions

67
MQTT.py
View File

@ -5,41 +5,38 @@ import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh from tool import push_stream, close_stream, update, exec_sh
# Connection success callback class MQTT:
def on_connect(client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
print(rc) if rc == 0:
if rc == 0: client.subscribe('lot_stream')
client.subscribe('lot_data') # Message receiving callback
else: def on_message(self, client, userdata, msg):
client.publish('conn_error', payload=rc, qos=0) data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream":
# 启动推流视频
# Message receiving callback push_stream()
def on_message(client, userdata, msg): elif data == "close_stream":
data = json.loads(msg.payload.decode('utf-8'))["msg"] # 关闭推流视频
if data == "push_stream": close_stream()
# 启动推流视频 elif data == "exec":
push_stream() # 执行命令
elif data == "close_stream": exec_sh(msg)
# 关闭推流视频 elif data == "update":
close_stream() # git更新项目和配置文件
elif data == "exec": update()
# 执行命令 else:
exec_sh(msg) client.publish('error', payload='No Such Type', qos=0)
elif data == "update":
# git更新项目和配置文件
update()
else:
client.publish('error', payload='No Such Type', qos=0)
def __init__(self):
self.client = mqtt.Client()
self.client.username_pw_set("ceshi", "123456")
# Specify callback function
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
# Establish a connection
self.client.connect('192.168.1.27', 1883)
# Publish a message
self.client.loop_forever(retry_first_connection=True)
if __name__ == '__main__': if __name__ == '__main__':
client = mqtt.Client() mq = MQTT()
client.username_pw_set("ceshi", "123456")
# Specify callback function
client.on_connect = on_connect
client.on_message = on_message
# Establish a connection
client.connect('127.0.0.1', 1883)
# Publish a message
client.loop_forever()

Binary file not shown.

Binary file not shown.

389
ceshi.py
View File

@ -1,151 +1,103 @@
#cd demo/ceshi-1/ # cd demo/ceshi-1/
#python ceshi.py # python ceshi.py
import serial import serial
import time import time
import struct import struct
import json import json
import requests import paho.mqtt.client as mqtt
from api import add
from db.models.log_data_model import LOT_DATA
def hex_to_float(hex_str): def hex_to_float(hex_str):
hex_int = int(hex_str, 16) hex_int = int(hex_str, 16)
return struct.unpack('!f', struct.pack('!I', hex_int))[0] return struct.unpack('!f', struct.pack('!I', hex_int))[0]
def print_json(data): def print_json(data):
print(json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)) print(json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
temp_send = '06 03 01 F4 00 02 85 B2 '#温湿度查询指令
co2_send = '06 03 01 F7 00 02 75 B2 '#二氧化碳查询指令
pressure_send = '06 03 01 F9 00 02 14 71 '#气压查询指令
sun_send = '06 03 01 FA 00 02 E4 71 '#光照查询指令
soil_send = '02 03 00 00 00 04 44 3A'#土壤查询指令 def run_with_client(client):
danlinjia_send ='02 03 00 04 00 03 44 39'#氮磷钾查询指令 temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
sun_send = '06 03 01 FA 00 02 E4 71 ' # 光照查询指令
rainfall_send = '03 03 00 00 00 01 85 E8'#雨量查询指令 soil_send = '02 03 00 00 00 04 44 3A' # 土壤查询指令
danlinjia_send = '02 03 00 04 00 03 44 39' # 氮磷钾查询指令
windspeed_send = '04 03 00 00 00 02 C4 5E'#风速查询指令 rainfall_send = '03 03 00 00 00 01 85 E8' # 雨量查询指令
winddirection_send = '05 03 00 00 00 02 C5 8F'#风向查询指令
windspeed_send = '04 03 00 00 00 02 C4 5E' # 风速查询指令
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)
# print(temp_send)
# print(co2_send)
# print(pressure_send)
# print(sun_send)
# print(soil_send)
# print(danlinjia_send)
# print(rainfall_send)
# print(windspeed_send)
# print(winddirection_send)
# print('{"name":%s,"name1":%s}', temp_send,temp_send)
while True:
# if ser.is_open:
# if __name__ == '__main__':
# data = {
# "aaa": {
# "bbb":
# {
# "vvv": ['541454154', '152125', '151515', {"521515": ""}],
# "ccc": "i5452126541rl",
# "cccd": 10
# }
# },
# "12222": {
# "fhvhgvh": "",
# "nbvghngvc": "",
# "nbvg h": "nvhvhv",
# }
# }
while True:
# if ser.is_open:
print('start')
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
# print(buffer_data, 'buffer_data')
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)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# 对返回的数据进行解析,获取温度和湿度数据 # 对返回的数据进行解析,获取温度和湿度数据
# print("当前湿度为:", int(return_data_hex[6:10], 16)/10) airtemp_data = int(return_data_hex[6:10], 16) / 10
# print("当前温度为:", int(return_data_hex[10:14], 16)/10) airhumi_data = int(return_data_hex[10:14], 16) / 10
airtemp_data=int(return_data_hex[6:10], 16)/10
airhumi_data=int(return_data_hex[10:14], 16)/10
time.sleep(5) time.sleep(5)
# print('send co2 directives')
ser.write(co2_send) ser.write(co2_send)
time.sleep(1) time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9 # 获取返回的缓冲data,获取的是buffer_data的长度 9
co2buffer_data = ser.in_waiting co2buffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if co2buffer_data: if co2buffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(co2buffer_data) return_data = ser.read(co2buffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# 对返回的数据进行解析,获取温度和湿度数据 # 对返回的数据进行解析,获取温度和湿度数据
# print("当前co2为", int(return_data_hex[6:10], 16)) co2_data = int(return_data_hex[6:10], 16)
co2_data=int(return_data_hex[6:10], 16)
time.sleep(5) time.sleep(5)
# print('send pressure directives')
ser.write(pressure_send) ser.write(pressure_send)
time.sleep(1) time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9 # 获取返回的缓冲data,获取的是buffer_data的长度 9
pressurebuffer_data = ser.in_waiting pressurebuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if pressurebuffer_data: if pressurebuffer_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(pressurebuffer_data) return_data = ser.read(pressurebuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# 对返回的数据进行解析,获取温度和湿度数据 # 对返回的数据进行解析,获取温度和湿度数据
# print("当前气压为:", int(return_data_hex[6:10], 16)/10) pressure_data = int(return_data_hex[6:10], 16) / 10
pressure_data=int(return_data_hex[6:10], 16)/10
time.sleep(5) time.sleep(5)
# print('send sun directives')
ser.write(sun_send) ser.write(sun_send)
time.sleep(1) time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9 # 获取返回的缓冲data,获取的是buffer_data的长度 9
sunbuffer_data = ser.in_waiting sunbuffer_data = ser.in_waiting
# print(buffer_data, 'buffer_data')
if sunbuffer_data: if sunbuffer_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(sunbuffer_data) return_data = ser.read(sunbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex) sun_data = int(return_data_hex[6:14], 16)
# sun=round(hex_to_float(return_data_hex[6:14])*6, 2)
# print("当前光照为:", sun)#单位lux
# print("当前光照为:", int(return_data_hex[6:14], 16))
sun_data=int(return_data_hex[6:14], 16)
time.sleep(5) time.sleep(5)
# print('send soil directives') # print('send soil directives')
@ -157,22 +109,15 @@ while True:
if soilbuffer_data: if soilbuffer_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(soilbuffer_data) return_data = ser.read(soilbuffer_data)
# print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# 对返回的数据进行解析,获取温度和湿度数据 # 对返回的数据进行解析,获取温度和湿度数据
# print("当前湿度值为:", int(return_data_hex[6:10], 16)/10) humidity_data = int(return_data_hex[6:10], 16) / 10
# print("当前温度值为:", int(return_data_hex[10:14], 16)/10) temperature_data = int(return_data_hex[10:14], 16) / 10
# print("当前电导率为:", int(return_data_hex[14:18], 16)/10) electrical_data = int(return_data_hex[14:18], 16) / 10
# print("当前PH值为", int(return_data_hex[18:22], 16)/10) PH_data = int(return_data_hex[18:22], 16) / 10
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) time.sleep(5)
# print('send danlinjia directives') # print('send danlinjia directives')
ser.write(danlinjia_send) ser.write(danlinjia_send)
time.sleep(1) time.sleep(1)
@ -185,14 +130,10 @@ while True:
# print('返回的数据2进制:', return_data) # print('返回的数据2进制:', return_data)
# 二进制转换为16进制010304010802307b79 # 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex)
# # 对返回的数据进行解析,获取温度和湿度数据 # # 对返回的数据进行解析,获取温度和湿度数据
# print("当前氮含量为:", int(return_data_hex[6:10], 16)/10) dan_data = int(return_data_hex[6:10], 16) / 10
# print("当前磷含量为:", int(return_data_hex[10:14], 16)/10) lin_data = int(return_data_hex[10:14], 16) / 10
# print("当前钾含量为:", int(return_data_hex[14:18], 16)/10) jia_data = int(return_data_hex[14:18], 16) / 10
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) time.sleep(5)
# print('send rainfall directives') # print('send rainfall directives')
@ -210,29 +151,9 @@ while True:
# print('返回的数据转换为16进制:', return_data_hex) # print('返回的数据转换为16进制:', return_data_hex)
# # 对返回的数据进行解析,获取温度和湿度数据 # # 对返回的数据进行解析,获取温度和湿度数据
# print("当前雨量值为:", int(return_data_hex[6:10], 16)/10)#单位mm # print("当前雨量值为:", int(return_data_hex[6:10], 16)/10)#单位mm
rainfall_data=int(return_data_hex[6:10], 16)/10 rainfall_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5) time.sleep(5)
# print('send sun directives')
# ser.write(sun_send)
# time.sleep(1)
# # 获取返回的缓冲data,获取的是buffer_data的长度 9
# sunbuffer_data = ser.in_waiting
# # print(buffer_data, 'buffer_data')
# if sunbuffer_data:
# # 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
# return_data = ser.read(sunbuffer_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:14], 16))#单位lux
# sun=hex_to_float(return_data_hex[6:14])
# print("当前光照强度为为:", sun)#单位m/s
# time.sleep(5)
# print('send windspeed directives')
ser.write(windspeed_send) ser.write(windspeed_send)
time.sleep(1) time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9 # 获取返回的缓冲data,获取的是buffer_data的长度 9
@ -246,7 +167,7 @@ while True:
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex) # print('返回的数据转换为16进制:', return_data_hex)
# print("当前风速为:", int(return_data_hex[6:10], 16)/10)#单位mm # print("当前风速为:", int(return_data_hex[6:10], 16)/10)#单位mm
speedwind_data=int(return_data_hex[6:10], 16)/10 speedwind_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5) time.sleep(5)
# print('send winddirection directives') # print('send winddirection directives')
@ -263,7 +184,7 @@ while True:
return_data_hex = str(return_data.hex()) return_data_hex = str(return_data.hex())
# print('返回的数据转换为16进制:', return_data_hex) # print('返回的数据转换为16进制:', return_data_hex)
# print("当前风向为:", int(return_data_hex[10:14], 16))#单位mm # print("当前风向为:", int(return_data_hex[10:14], 16))#单位mm
winddirection_data=int(return_data_hex[10:14], 16) winddirection_data = int(return_data_hex[10:14], 16)
time.sleep(5) time.sleep(5)
# print('{"name":"%d","name1":"%d"}', 123,456) # print('{"name":"%d","name1":"%d"}', 123,456)
# data = [{'ngvhgv': airtemp_data}, {'nvjgvjvj':airhumi_data}] # data = [{'ngvhgv': airtemp_data}, {'nvjgvjvj':airhumi_data}]
@ -282,12 +203,214 @@ while True:
'rainfall': rainfall_data, 'rainfall': rainfall_data,
'wind_speed': speedwind_data, 'wind_speed': speedwind_data,
'wind_direction': winddirection_data} 'wind_direction': winddirection_data}
# print('stop')
# print("请求:") data = LOT_DATA(**data, create_time=int(time.time()))
print_json(data) # TODO 判断数据是否正常
# requests.post("http://192.168.1.27:8000/add/lot_data/123", json=data) add(data)
r=requests.post("http://192.168.1.27:8000/add/lot_data/123", json=data) # 发送给服务器
print('返回的数据结果:', r) client.publish('lot_data', payload=data, qos=0)
def run_no_client():
temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
sun_send = '06 03 01 FA 00 02 E4 71 ' # 光照查询指令
soil_send = '02 03 00 00 00 04 44 3A' # 土壤查询指令
danlinjia_send = '02 03 00 04 00 03 44 39' # 氮磷钾查询指令
rainfall_send = '03 03 00 00 00 01 85 E8' # 雨量查询指令
windspeed_send = '04 03 00 00 00 02 C4 5E' # 风速查询指令
winddirection_send = '05 03 00 00 00 02 C5 8F' # 风向查询指令
ser = serial.Serial("/dev/ttyS2", 9600)
# 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b'
temp_send = bytes.fromhex(temp_send)
co2_send = bytes.fromhex(co2_send)
pressure_send = bytes.fromhex(pressure_send)
sun_send = bytes.fromhex(sun_send)
soil_send = bytes.fromhex(soil_send)
danlinjia_send = bytes.fromhex(danlinjia_send)
rainfall_send = bytes.fromhex(rainfall_send)
windspeed_send = bytes.fromhex(windspeed_send)
winddirection_send = bytes.fromhex(winddirection_send)
while True:
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:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
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)
ser.write(soil_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
soilbuffer_data = ser.in_waiting
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)
ser.write(danlinjia_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
danlinjiabuffer_data = ser.in_waiting
if danlinjiabuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(danlinjiabuffer_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)
ser.write(rainfall_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
rainfallbuffer_data = ser.in_waiting
if rainfallbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(rainfallbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
# # 对返回的数据进行解析,获取温度和湿度数据
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
if windspeedbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(windspeedbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
speedwind_data = int(return_data_hex[6:10], 16) / 10
time.sleep(5)
ser.write(winddirection_send)
time.sleep(1)
# 获取返回的缓冲data,获取的是buffer_data的长度 9
winddirectionbuffer_data = ser.in_waiting
if winddirectionbuffer_data:
# 返回的数据为2进制b'\x01\x03\x04\x01\x08\x022\xfa\xb8'
return_data = ser.read(winddirectionbuffer_data)
# 二进制转换为16进制010304010802307b79
return_data_hex = str(return_data.hex())
winddirection_data = int(return_data_hex[10:14], 16)
time.sleep(5)
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}
data = LOT_DATA(**data, create_time=int(time.time()))
# TODO 判断数据是否正常
add(data)
class UPLOAD:
def __init__(self):
self.client = mqtt.Client()
self.client.username_pw_set("ceshi", "123456")
# Specify callback function
self.client.on_connect = self.on_connect
# Establish a connection
self.client.connect('192.168.1.27', 1883)
# Publish a message
self.client.loop_forever()
self.time = 3
def on_connect(self, client, userdata, flags, rc):
if rc == 0:
print("连接成功,执行数据推送和本地存储")
self.t1()
else:
if self.time != 0:
self.time -= 1
client.reconnect()
else:
print("3次失败执行本地存储")
self.t2()
# Message receiving callback
def t1(self):
for i in range(3):
self.client.publish('lot_data', payload='1', qos=0)
def t2(self):
print("1")
if __name__ == '__main__':
up = UPLOAD()

View File

@ -1,9 +1,9 @@
[program:test2] [program:data_upload]
directory=/home/lihai/PycharmProjects/pythonProject/lot_manager directory=/home/lihai/PycharmProjects/pythonProject/lot_manager
command=/home/lihai/anaconda3/envs/chatchat/bin/python test.py command=/home/lihai/anaconda3/envs/chatchat/bin/python ceshi.py
user=lihai user=lihai
;是否随开机自启 或者reload自启动 ;是否随开机自启 或者reload自启动
autostart=false autostart=true
;失败重启 ;失败重启
autorestart=true autorestart=true
;重启次数 ;重启次数

View File

@ -7,7 +7,7 @@ from db.session import with_session
@with_session @with_session
def add_kb_to_db(session, data: LOT_DATA): def add_kb_to_db(session, data: LOT_DATA):
# 创建知识库实例 # 创建知识库实例
kb = LOT_DATA_MODEL(**data.__dict__, create_time=int(time.time())) kb = LOT_DATA_MODEL(**data.__dict__)
session.add(kb) session.add(kb)

BIN
lot_data.db Normal file

Binary file not shown.

View File

@ -3,6 +3,7 @@ import time
import requests import requests
while (True): while (True):
print(time.time()) print(time.time())
time.sleep(10.0) time.sleep(10.0)
@ -23,4 +24,7 @@ while (True):
# "soil_potassium_phosphate_phosphorus": 5, # "soil_potassium_phosphate_phosphorus": 5,
# "soil_potassium_phosphate_potassium": 86 # "soil_potassium_phosphate_potassium": 86
# } # }
# data = LOT_DATA(**data, create_time=int(time.time()))
# add(data)
# print(data)
# requests.post("http://192.168.1.27:8000/add/lot_data", json=data) # requests.post("http://192.168.1.27:8000/add/lot_data", json=data)