From 40431a3c2578a5fdb5eb0d63a8a507af9e0fb059 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Sat, 9 Dec 2023 15:16:51 +0800 Subject: [PATCH] update --- MQTT.py | 10 +++------- __pycache__/tool.cpython-310.pyc | Bin 0 -> 2491 bytes data_upload.py | 2 +- device.py | 5 +++++ sensor_to_local.py | 33 +++++++++++++++++++++++++++++-- sensor_to_server.py | 29 +++++++++++++++++++++++++-- 6 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 __pycache__/tool.cpython-310.pyc create mode 100644 device.py 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 0000000000000000000000000000000000000000..6f81b1e31101a748306236464b5ad49710cde211 GIT binary patch literal 2491 zcmbVNOK%)S5T5RN?CkD1ahx}Kun-X0fIT2ZB0Ll!MOZ>YmL+_$kcRPeXLmNc)06HV zD_+f>KtddR<&=Yc;NTMy2QJ+B8GQv=aOM_?Kvl0_SqTzlM)Orarn{@FzN!*)b1es+ z`F+V7ZO8cyJ0~9soloFXUjZmGBN_l>jeGoJ+@1J+<6 z$Odb&7RZpbSp>4l=GZBaEq0pCgKRVMh0{IrC!FG}jH9?OMm#R_cvRlb2o4@RKF9#R zd+`1BIe>NsgpnR$6k7IU?zT4}HQ8}@oiFKxPTZRAc+A@+6R&m#pqUN^joM?N+csq( zYi_#)?gZzzR>pVYjl76AQq}JqDKQi}8Kp(qK=N~cmMWu^Lw(vqj6CDmWsbB(Y1 zd^j|L(o9s^N-pISztZ;?@0fKXBYx7*P>Gi-aWjC!$vM4h&?d;o*ZP9MVqx3LWk25FiuX zaT(c#cS0Gh=_ZjEYcjAwfB|qwtQux7J3vI2H6BoP17!Gs$~Td8&~8HeBfG8z;c>7| z`-7l%f!Gn7+on^39*;q*fgd{sWcO-sC$wbuCqeB`b(%D$J3?4A>js7yko|;C-7_oJ zdd6d^PK^(&5ORDTJDpLbbZ3KiR8^Kh9(HWj@f8^3ax+&#W_s9BT9ln689mfGNygWo zos`@L%JKZU*?DBUb>DhZa_r>wD2PU=-p1QT$lILN>`1p&G=r4($ljjglBg`Y5 zL0CYz0MHGMJ5oJ)3EHMDWZuh*#9r3TMoeDD|6W13f^ZdKhAIvIh-ZoF;|~hSvj8la z2a+En_AZf#Sl+w^zdWDvGgbf_=}N7=G@n~Nu7ST6wo7{{tF$|algiU&(s5vNda z@`CY4@%? zp-!W(aFawr(w6Kl>}?$uQb<2f7d-hMj69Qi@cKuorw^Uk)MHZ%Bp>Efjyem{g>-Vw z2ET^^4^{4y=rEH5jEB`xz6~>AfAott5P2anAY_d2EcYDnbcY~1`csSD!f2vcwjr@Z*tS)sWvs<||Y*gfM}ZdNg#yW*CMGVIyos;lHfhL|Xs= literal 0 HcmV?d00001 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