diff --git a/MQTT.py b/MQTT.py index e5b9a40..501de0f 100644 --- a/MQTT.py +++ b/MQTT.py @@ -8,7 +8,7 @@ from tool import push_stream, close_stream, update, exec_sh class MQTT: def on_connect(self, client, userdata, flags, rc): if rc == 0: - client.subscribe('lot_stream') + client.subscribe('lot_mqtt') # Message receiving callback def on_message(self, client, userdata, msg): @@ -34,12 +34,12 @@ class MQTT: def __init__(self): self.client = mqtt.Client() - self.client.username_pw_set("ceshi", "123456") + self.client.username_pw_set("demo", "123456") # Specify callback function self.client.on_connect = self.on_connect self.client.on_message = self.on_message # Establish a connection - self.client.connect('60.204.152.17', 1883) + self.client.connect('ceshi-mqtt.lihaink.cn', 1883) # Publish a message self.client.loop_forever(retry_first_connection=True) diff --git a/__pycache__/api.cpython-310.pyc b/__pycache__/api.cpython-310.pyc index 61c0787..de2f028 100644 Binary files a/__pycache__/api.cpython-310.pyc and b/__pycache__/api.cpython-310.pyc differ diff --git a/__pycache__/tool.cpython-310.pyc b/__pycache__/tool.cpython-310.pyc index f7a1fa9..f549089 100644 Binary files a/__pycache__/tool.cpython-310.pyc and b/__pycache__/tool.cpython-310.pyc differ diff --git a/ceshi.py b/ceshi.py index 99a1e41..df94ff7 100644 --- a/ceshi.py +++ b/ceshi.py @@ -376,66 +376,59 @@ def run_no_client(): 'wind_direction': winddirection_data} data = LOT_DATA(**data, create_time=int(time.time())) # TODO 判断数据是否正常 - r = add(data) + add(data) -def t1(): - import time - while (1): - time.sleep(2) - data = { - "wind_speed": 1, - "wind_direction": 6, - "ambient_temperature": 66, - "ambient_humidity": 78, - "carbon_dioxide": 94, - "ambient_air_pressure": 48, - "rainfall": 3, - "ambient_lighting": 59, - "soil_temperature": 71, - "soil_moisture": 19, - "soil_conductivity": 62, - "soil_PH": 49, - "soil_potassium_phosphate_nitrogen": 5, - "soil_potassium_phosphate_phosphorus": 5, - "soil_potassium_phosphate_potassium": 86, - "create_time": int(time.time()) - } - client.publish('lot_data', payload=json.dumps(data, ensure_ascii=False), qos=0) +class UPLOAD: + def __init__(self): + self.times = 3 + self.client = mqtt.Client(transport="websockets") + self.client.username_pw_set("demo", "123456") + # Specify callback function + self.client.on_connect = self.on_connect + # Establish a connection + self.client.connect('ceshi-mqtt.lihaink.cn', 8083) + # Publish a message + self.client.loop_forever() - -def t2(): - print("1") - - -time = 3 - -from threading import Thread - - -def on_connect(client, userdata, flags, rc): - global time - if rc == 0: - print("连接成功,执行数据推送和本地存储") - nt1 = Thread(target=t1) - nt1.start() - else: - if time != 0: - time -= 1 - client.reconnect() + def on_connect(self, client, userdata, flags, rc): + from threading import Thread + if rc == 0: + print("连接成功,执行数据推送和本地存储") + nt1 = Thread(target=self.t1) + nt1.start() else: - print("3次失败,执行本地存储") - nt2 = Thread(target=t2) - nt2.start() + if self.times != 0: + self.times -= 1 + client.reconnect() + else: + print("3次失败,执行本地存储") + nt2 = Thread(target=self.t2) + nt2.start() + def t2(self): + print("1") -client = mqtt.Client(transport="websockets") -client.username_pw_set("demo", "123456") -# Specify callback function -client.on_connect = on_connect -# Establish a connection -client.connect('ceshi-mqtt.lihaink.cn', 8083) -# Publish a message -client.loop_forever() - -# Message receiving callback + def t1(self): + import time + while (1): + time.sleep(60) + data = { + "wind_speed": 1, + "wind_direction": 6, + "ambient_temperature": 66, + "ambient_humidity": 78, + "carbon_dioxide": 94, + "ambient_air_pressure": 48, + "rainfall": 3, + "ambient_lighting": 59, + "soil_temperature": 71, + "soil_moisture": 19, + "soil_conductivity": 62, + "soil_PH": 49, + "soil_potassium_phosphate_nitrogen": 5, + "soil_potassium_phosphate_phosphorus": 5, + "soil_potassium_phosphate_potassium": 86, + "create_time": int(time.time()) + } + self.client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0) diff --git a/update.sh b/update.sh index 2861bc0..c6ac1a9 100644 --- a/update.sh +++ b/update.sh @@ -5,4 +5,5 @@ git pull origin master # 配置文件复制到supervisor管理 cp -r conf/*.conf /etc/supervisor/conf.d/ # 更新配置文件 +supervisorctl reread supervisorctl update