デバイスからメッセージを発行および受信するコードがあります。問題は、デバイスが突然接続を失った場合、デバイスの接続が切断されてからわずか 5 分後に lwt が到着することです。ライフタイム30秒。test.py:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("test/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
imei = msg.topic.split('test/')[1]
data = msg.payload.decode()
print(imei)
print(data)
publish(client, imei)
def publish(client,imei):
topic = 'test/'+ imei
client.publish(topic,'hello')
print('SEND')
client = mqtt.Client()
user = 'test'
passw = '1111'
client.username_pw_set(user,passw)
client.on_connect = on_connect
client.on_message = on_message
client.enable_bridge_mode()
client.connect("localhost", 1883, 30)
client.loop_forever()
mosquitto.conf:
persistence true
persistence_location /var/lib/mosquitto/
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
include_dir /etc/mosquitto/conf.d
max_keepalive 30
これは、クライアントが LWT を送信する方法です。
modem_setWillMessage(0,0,topic,"no connect")
ここで、最初の 0-qos、2 番目の 0-retained メッセージ
lwt時間を60秒に設定する設定が見つかりません