This commit is contained in:
xyj 2023-12-09 15:16:51 +08:00
parent 5e3f63de37
commit 40431a3c25
6 changed files with 67 additions and 12 deletions

10
MQTT.py
View File

@ -1,7 +1,6 @@
import json import json
import subprocess import os
import time import time
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
@ -20,7 +19,7 @@ def valid(msg, client):
if 'device_name' not in origin_data: if 'device_name' not in origin_data:
client.publish('error', payload='device_name must be supplied', qos=0) client.publish('error', payload='device_name must be supplied', qos=0)
return False return False
if device_name != origin_data['device_name']: if os.getenv('device_name').strip() != origin_data['device_name']:
return False return False
return True return True
@ -70,12 +69,9 @@ def exec_shutdown():
if __name__ == '__main__': if __name__ == '__main__':
p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
device_name = out.decode('utf-8').strip()
while True: while True:
try: try:
client = mqtt.Client(client_id=device_name) client = mqtt.Client(client_id=os.getenv('device_name').strip())
client.username_pw_set("demo", "123456") client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect

Binary file not shown.

View File

@ -21,7 +21,7 @@ times = 3
if __name__ == '__main__': if __name__ == '__main__':
while True: while True:
try: try:
client = mqtt.Client(client_id=os.getenv('device_name')) client = mqtt.Client(client_id=os.getenv('device_name').strip())
client.username_pw_set("demo", "123456") client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect

5
device.py Normal file
View File

@ -0,0 +1,5 @@
import subprocess
p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
device_name = out.decode('utf-8').strip()

View File

@ -6,7 +6,8 @@ import serial
from db.models.lot_data_model import LOT_DATA from db.models.lot_data_model import LOT_DATA
from api import add from api import add
if __name__ == '__main__':
def local():
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 ' # 气压查询指令
@ -20,7 +21,6 @@ if __name__ == '__main__':
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' # 风向查询指令
# 发送的数据转为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)
@ -189,3 +189,32 @@ if __name__ == '__main__':
except Exception as e: except Exception as e:
print(e) print(e)
pass pass
def test():
while True:
data = {'ambient_temperature': 1,
'ambient_humidity': 1,
'carbon_dioxide': 1,
'ambient_air_pressure': 1,
'ambient_lighting': 1,
'soil_moisture': 1,
'soil_temperature': 1,
'soil_conductivity': 1,
'soil_PH': 1,
'soil_potassium_phosphate_nitrogen': 1,
'soil_potassium_phosphate_phosphorus': 1,
'soil_potassium_phosphate_potassium': 1,
'rainfall': 1,
'wind_speed': 1,
'wind_direction': 1,
'create_time': int(time.time()),
'device_name': os.getenv('device_name')
}
t2 = LOT_DATA(**data)
add(t2)
if __name__ == '__main__':
# local()
test()

View File

@ -195,12 +195,37 @@ def t():
pass pass
def test():
while True:
data = {'ambient_temperature': 1,
'ambient_humidity': 1,
'carbon_dioxide': 1,
'ambient_air_pressure': 1,
'ambient_lighting': 1,
'soil_moisture': 1,
'soil_temperature': 1,
'soil_conductivity': 1,
'soil_PH': 1,
'soil_potassium_phosphate_nitrogen': 1,
'soil_potassium_phosphate_phosphorus': 1,
'soil_potassium_phosphate_potassium': 1,
'rainfall': 1,
'wind_speed': 1,
'wind_direction': 1,
'create_time': int(time.time()),
'device_name': os.getenv('device_name')
}
t2 = LOT_DATA(**data)
client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0)
add(t2)
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
threading.Thread(target=t).start() threading.Thread(target=test).start()
if __name__ == '__main__': if __name__ == '__main__':
client = mqtt.Client(client_id=os.getenv('device_name')) client = mqtt.Client(client_id=os.getenv('device_name').strip())
client.username_pw_set("demo", "123456") client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect