1

I have a Raspberry Pi Pico W that I am trying to get to communicate with AWS IoT, and after about 12-24 hours it seems to lose its connection. I have the keep alive set and I can see it's pinging the server. And then it suddenly stops. I'm going to add some code below to show the connection and stuff, but I'm baffled at this point (I am cross-posting this on other Raspberry Pi sites too). Any thoughts on why I can't keep the connection open?

import machine
import time
import ussl as ssl
from robust import MQTTClient
def sub_cb(btopic, bmsg):
    global led
    led = machine.Pin(15, machine.Pin.OUT)
    topic = btopic.decode()
    msg = bmsg.decode()
    mqtt_message_json = ujson.loads(msg)
    if 'desired' in mqtt_message_json['state']:
        led_status_update = (mqtt_message_json['state']['desired']['led_status'])
        if led.value() != str(led_status_update):
            if led_status_update == '0':
                print("turning off LED")
                led.value(0)
            elif led_status_update == '1':
                print("turning on LED")
                led.value(1)

DeviceID='PicoTestBed03'
PORT=8883
AWS_ENDPOINT={My AWS API End Point}
DISCONNECTED = 0
CONNECTING = 1
CONNECTED = 2
state = DISCONNECTED
KeepAliveSeconds = 60

led = machine.Pin(15, machine.Pin.OUT, value=1)

#Use Websockets
useWebsocket = False

#Create SSL Params object
#Assume the cert, key, and rootCA are all created correctly because my connection is successful
SSL_PARAMS = {'cert': cert, 'key': key, 'server_side': False, "cert_reqs":ssl.CERT_REQUIRED, 'cadata':rootCA}

#Create MQTT Client
client = MQTTClient(DeviceId, AWS_ENDPOINT, port=PORT, keepalive=KeepAliveSeconds, ssl=True, ssl_params=SSL_PARAMS)

#Connect MQTT Client
while state != CONNECTED:
    try:
        state = CONNECTING
        print('AWS TEST: Trying to connect via MQTT...')
        client.connect()
        state = CONNECTED
    except Exception as e:
        print('AWS TEST: Could not establish MQTT connection')
    continue

print('AWS TEST: MQTT LIVE!')

device_shadow_last_checked = time.localtime()

while 1:
    if current_led_status != led.value():
        current_led_status = led.value()
        device_shadow_update_msg = b'{"state":{"reported":{"led_status":%d}}}' %(led.value())
        mqtt_client.publish(led_status_shadow_topic_update, device_shadow_update_msg, qos=0)
        if (time.mktime(time.localtime()) - time.mktime(device_shadow_last_checked)) >=60:
            client.ping()
            device_shadow_last_checked = time.localtime()
        client.check_msg()
michaeka
  • 11
  • 2
  • If your question is "why is the connection dropping" then looking at the [lifecycle events](https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html) generated may help. If your question is why your code stops working when this happens then please let us know which MQTT package you are using; `umqtt.simple` will not auto reconnect (which may explain what you see) whereas ` umqtt.robust` can. – Brits Nov 01 '22 at 22:37
  • Thanks Brits, I will look at the Lifecycle events. I've tried both umqtt.simple and umqtt.robst packages to try and solve my issue. Both exhibit the same behavior. So I assume it's something I'm doing or not doing. I am fairly certain that my code keeps going... I think... it just loses the ability to publish or receive subscribed to AWS IoT topics. I also updated my code with the libraries I'm using. – michaeka Nov 01 '22 at 23:14
  • Whatever the cause of the disconnect you need to expect that it will happen from time to time. My guess will be in code you have not shown us (message handlers etc). – Brits Nov 02 '22 at 00:34
  • oh snap you're right I forgot the callback code! I'll throw that in but really it's not doing much other than turning on and off a GPIO. – michaeka Nov 02 '22 at 00:49
  • If it's always crashing within 24 hours why not just remove all of that code and see if it still fails (that will help you narrow down the root cause). Start with the most basic app (if that still fails then you have a [minimal, reproducible, example](https://stackoverflow.com/help/minimal-reproducible-example) for us, if not you know the problem is elsewhere). – Brits Nov 02 '22 at 00:56
  • And where are you subscribing? As you are not setting `clean_session=False` you will have to resubscribe after each reconnect and I don't think `umqtt.robust` has a mechanism for this (see [this example](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.robust/example_sub_robust.py#L16)). Note I have tried to fix your code formatting but not sure if it's 100% (i.e. the random `continue`). – Brits Nov 02 '22 at 01:17
  • Hmmm, when you mention "resubscribing after reconnect"... I thought if I were sending constant pings the client would NOT disconnect and thus require a reconnect/resubscribing. Did I not understand how that was functioning correctly? If the client is actually doing a disconnect, you're right I would not be resubscribing because I am not capturing the reconnection. – michaeka Nov 02 '22 at 06:58
  • If you want a resilient system you will need to handle connection loss gracefully (the connection will drop from time to time). So maybe fix the reconnection and then, if it's truly an issue, investigate the cause of the disconnections. – Brits Nov 02 '22 at 08:29

0 Answers0