Black Lives Matter - Action and Equality.
0

Raspberry Pi LWT (Last Will) not triggered?
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Raspberry Pi LWT (Last Will) not triggered?

by tpsk on Mon May 04, 2020 1:02 pm

Hello,

I wrote an LWT test program but I dont seem to get my 'Last Will" to be triggered.

Code: Select all | TOGGLE FULL SIZE
import time
import paho.mqtt.client as mqtt
import ssl

conn_flag = False

def on_connect(client, userdata, flags, rc):
    global conn_flag
    print("Connected")
    conn_flag = True

def on_disconnect(client):
    global conn_flag
    print("Disconnected")
    conn_flag = False

aio_username = 'AIO-MYUSERNAME-DELETED'
aio_password = 'aio_AIO-KEY-DELETED'
aio_feed = aio_username + '/feeds/connection'

client = mqtt.Client("Hello LWT world")
client.on_connect = on_connect
client.on_disconnect = on_disconnect

client.tls_set()
client.tls_insecure_set(True)

client.will_set(aio_feed, "dead", 1, True);

client.username_pw_set(aio_username, aio_password)
client.connect("io.adafruit.com", 8883, 10)
while True:
    while not conn_flag:
        client.loop_start()
        print("Waiting to connect...")
        time.sleep(1)

    print("publishing...")
    client.publish(aio_feed, "alive5")
    time.sleep(10)
    client.loop_start()

The feed is updated to 'alive5' every 10 seconds. That works fine. I then do a 'killall -9 python3' or just pull the plug from my RPI.
I expect the feed to get updated to 'dead' in the AIO Dashboard but it stays on 'alive5' forever.

I tried playing around with the QoS flags and retainer flag but no luck.

regards,

-t

tpsk
 
Posts: 1
Joined: Mon May 04, 2020 12:29 pm

Please be positive and constructive with your questions and comments.