diff --git a/MQTT.py b/MQTT.py index f28d6d3..f369b46 100755 --- a/MQTT.py +++ b/MQTT.py @@ -1,7 +1,6 @@ import json -import subprocess +import os import time - import paho.mqtt.client as mqtt 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: client.publish('error', payload='device_name must be supplied', qos=0) return False - if device_name != origin_data['device_name']: + if os.getenv('device_name').strip() != origin_data['device_name']: return False return True @@ -70,12 +69,9 @@ def exec_shutdown(): 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: try: - client = mqtt.Client(client_id=device_name) + client = mqtt.Client(client_id=os.getenv('device_name').strip()) client.username_pw_set("demo", "123456") # Specify callback function client.on_connect = on_connect diff --git a/__pycache__/tool.cpython-310.pyc b/__pycache__/tool.cpython-310.pyc new file mode 100644 index 0000000..6f81b1e Binary files /dev/null and b/__pycache__/tool.cpython-310.pyc differ diff --git a/data_upload.py b/data_upload.py index 2d878a7..46b86c8 100755 --- a/data_upload.py +++ b/data_upload.py @@ -21,7 +21,7 @@ times = 3 if __name__ == '__main__': while True: 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") # Specify callback function client.on_connect = on_connect diff --git a/device.py b/device.py new file mode 100644 index 0000000..fa05591 --- /dev/null +++ b/device.py @@ -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() diff --git a/sensor_to_local.py b/sensor_to_local.py index 932a868..d1703d0 100755 --- a/sensor_to_local.py +++ b/sensor_to_local.py @@ -6,7 +6,8 @@ import serial from db.models.lot_data_model import LOT_DATA from api import add -if __name__ == '__main__': + +def local(): 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 ' # 气压查询指令 @@ -20,7 +21,6 @@ if __name__ == '__main__': windspeed_send = '04 03 00 00 00 02 C4 5E' # 风速查询指令 winddirection_send = '05 03 00 00 00 02 C5 8F' # 风向查询指令 - # 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b' temp_send = bytes.fromhex(temp_send) co2_send = bytes.fromhex(co2_send) @@ -189,3 +189,32 @@ if __name__ == '__main__': except Exception as e: print(e) 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() diff --git a/sensor_to_server.py b/sensor_to_server.py index d0cb285..8ba691f 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -195,12 +195,37 @@ def t(): 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): - threading.Thread(target=t).start() + threading.Thread(target=test).start() 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") # Specify callback function client.on_connect = on_connect