This commit is contained in:
xyj 2023-12-02 10:34:03 +08:00
parent f80f71e3a2
commit 8c62d078cc
5 changed files with 54 additions and 60 deletions

View File

@ -8,7 +8,7 @@ from tool import push_stream, close_stream, update, exec_sh
class MQTT: class MQTT:
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
if rc == 0: if rc == 0:
client.subscribe('lot_stream') client.subscribe('lot_mqtt')
# Message receiving callback # Message receiving callback
def on_message(self, client, userdata, msg): def on_message(self, client, userdata, msg):
@ -34,12 +34,12 @@ class MQTT:
def __init__(self): def __init__(self):
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.username_pw_set("ceshi", "123456") self.client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect
self.client.on_message = self.on_message self.client.on_message = self.on_message
# Establish a connection # Establish a connection
self.client.connect('60.204.152.17', 1883) self.client.connect('ceshi-mqtt.lihaink.cn', 1883)
# Publish a message # Publish a message
self.client.loop_forever(retry_first_connection=True) self.client.loop_forever(retry_first_connection=True)

Binary file not shown.

Binary file not shown.

107
ceshi.py
View File

@ -376,66 +376,59 @@ def run_no_client():
'wind_direction': winddirection_data} 'wind_direction': winddirection_data}
data = LOT_DATA(**data, create_time=int(time.time())) data = LOT_DATA(**data, create_time=int(time.time()))
# TODO 判断数据是否正常 # TODO 判断数据是否正常
r = add(data) add(data)
def t1(): class UPLOAD:
import time def __init__(self):
while (1): self.times = 3
time.sleep(2) self.client = mqtt.Client(transport="websockets")
data = { self.client.username_pw_set("demo", "123456")
"wind_speed": 1, # Specify callback function
"wind_direction": 6, self.client.on_connect = self.on_connect
"ambient_temperature": 66, # Establish a connection
"ambient_humidity": 78, self.client.connect('ceshi-mqtt.lihaink.cn', 8083)
"carbon_dioxide": 94, # Publish a message
"ambient_air_pressure": 48, self.client.loop_forever()
"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)
def on_connect(self, client, userdata, flags, rc):
def t2(): from threading import Thread
print("1") if rc == 0:
print("连接成功,执行数据推送和本地存储")
nt1 = Thread(target=self.t1)
time = 3 nt1.start()
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()
else: else:
print("3次失败执行本地存储") if self.times != 0:
nt2 = Thread(target=t2) self.times -= 1
nt2.start() client.reconnect()
else:
print("3次失败执行本地存储")
nt2 = Thread(target=self.t2)
nt2.start()
def t2(self):
print("1")
client = mqtt.Client(transport="websockets") def t1(self):
client.username_pw_set("demo", "123456") import time
# Specify callback function while (1):
client.on_connect = on_connect time.sleep(60)
# Establish a connection data = {
client.connect('ceshi-mqtt.lihaink.cn', 8083) "wind_speed": 1,
# Publish a message "wind_direction": 6,
client.loop_forever() "ambient_temperature": 66,
"ambient_humidity": 78,
# Message receiving callback "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)

View File

@ -5,4 +5,5 @@ git pull origin master
# 配置文件复制到supervisor管理 # 配置文件复制到supervisor管理
cp -r conf/*.conf /etc/supervisor/conf.d/ cp -r conf/*.conf /etc/supervisor/conf.d/
# 更新配置文件 # 更新配置文件
supervisorctl reread
supervisorctl update supervisorctl update