0

There is a code in which I publish and receive messages from the device. The problem is that if the device suddenly loses connection, the lwt arrives only 5 minutes after the device's connection is broken. Life time 30 sec. 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

this is how the client send LWT:

modem_setWillMessage(0,0,topic,"no connect")

where first 0-qos,second 0-retained messages

I just can't find the setting that set the lwt time to 60 seconds

Kate12345
  • 11
  • 1
  • 6
  • `max_keepalive` only applies to MQTT V5 connections (`paho.mqtt.client` defaults to MQTT v3.11). The [keepalive interval](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349237) is set by the client (in your case that is "the device") when it connects. – Brits Dec 09 '21 at 20:40
  • Please edit the question to include the client logs that show when it sends the LWT (and notices the client has connected). @Brits the client is also passing a 30 second keepalive as part of the connection. – hardillb Dec 09 '21 at 21:24
  • @hardillb I had taken "code in which I publish and receive messages from the device", and the absence of a call to `will_set`, to mean that the code shown is receiving messages sent from the device (so receiving the LWT). That assumption may be incorrect - as you point out the logs will address this. – Brits Dec 09 '21 at 22:42
  • @hardillb ,I added to the code – Kate12345 Dec 10 '21 at 06:47
  • That doesn't help, we need to set it in context with the rest of the code, also we need to broker logs – hardillb Dec 10 '21 at 07:58
  • where can i find brokers logs? – Kate12345 Dec 10 '21 at 08:09
  • @hardillb ,when the device is connected to the broker, the function is immediately formed modem_setWillMessage(). – Kate12345 Dec 10 '21 at 08:11
  • @Kate12345 with your config (`log_dest topic`) logs go to the MQTT topic `'$SYS/broker/log/'` (you could view using `mosquitto_sub` or similar). See the [Mosquitto docs](https://mosquitto.org/man/mosquitto-conf-5.html) (search `log_dest` and `log_type`) for info on other options. I'm not sure which library `modem_setWillMessage()` is from - please provide details and the full code to initiate the connection. – Brits Dec 11 '21 at 00:00

0 Answers0