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:
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)

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}
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)

View File

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