Properly detecting and reconnecting to Pybytes



  • I have code that's working "fine" on one FiPy device over a Wifi connection - it's been online pretty consistently for months (Although it says "Offline" in pybytes, the signals post fine every time. I mirrored the exact same code to a second device (It has a unique pybytes configuration on the same account) and that device is inconsistently unable to post pybytes signals despite it reporting pybytes.isconnected as True.

    Is there some tried and true method to query the connection status of the pybytes mqtt server and reconnect when it's offline?

    When I set

    import pycom; pycom.nvs_set('pybytes_debug', 99)
    

    The one difference between working and not is that

    [         53.341] [1073565772] This is pack_pybytes_message_variable(5, 1, b'70.63')
    [         53.353] [1073565772] __pack_message: b'0e050137302e3633'
    [         53.361] [1073565772] MQTT Protocol
    

    is followed shortly after by

    [        57.731] [1073656844] Packet sent. (Length: 48)
    

    when it's actually sending. When it's not properly connecting, the "Packet sent" message doesn't show, but no additional errors are displayed.

    The startup looks like this (With credentials replaced with ***)

    [          1.651] [1073517588] {'wifi': {'ssid': '*****', 'password': '*****'}, 'pybytes_autostart': True, 'username': '*****', 'cfg_msg':'Pybytes configuration read from /flash/pybytes_config.json', 'server': 'mqtt.pybytes.pycom.io', 'lte': {'apn': None, 'cid': None, 'reset': False, 'carrier': None, 'band': None, 'type': None}, 'ssl': False, 'device_id': '*****', 'network_preferences': ['wifi'], 'dump_ca': False, 'wlan_antenna': 0, 'ota_server': {'port': 443, 'domain': 'software.pycom.io'}}
    [          1.699] [1073517588]  ssl_params={'ca_certs': '/flash/cert/pycom-ca.pem'}
    [          5.344] [1073517588] LIS2HH12 not imported
    [          6.736] [1073517588]  ssl_params={'ca_certs': '/flash/cert/pycom-ca.pe                                                       m'}
    WMAC: 840D8E******
    Firmware: 1.20.2.r6
    Pybytes: 1.7.1
    [          6.752] [1073517588] {'wifi': {'ssid': '*****', 'password': '*****'}, 'pybytes_autostart': True, 'username': '*****', 'cfg_msg': 'Pybytes configuration read from /flash/pybytes_config.json', 'server': 'mqtt.pybytes.pycom.io', 'lte': {'apn': None, 'cid': None, 'reset': False, 'carrier': None,'band': None, 'type': None}, 'ssl': False, 'device_id': '*****', 'network_preferences': ['wifi'], 'dump_ca': False, 'wlan_antenna': 0, 'ota_server': {'port': 443, 'domain': 'software.pycom.io'}}
    [          6.858] [1073517588] Attempting to connect with network wifi
    Initialized watchdog for WiFi and LTE connection with timeout 1260000 ms
    [          6.998] [1073517588] WLAN connected? False
    [          7.006] [1073517588] Wifi connection attempt: 1
    [          7.015] [1073517588] WLAN connected? False
    [          9.135] [1073517588] (ssid='*****', bssid=b"\xc0V'\xd7\xd1y", sec=3, channel=1, rssi=-57)
    [          9.141] [1073517588] (ssid='*****-guest', bssid=b'\xf6\x92\xbf\x95\x88]', sec=0, channel=6, rssi=-59)
    [          9.147] [1073517588] (ssid='*****', bssid=b'\xf4\x92\xbf\xa5\x88]', sec=3, channel=6, rssi=-59)
    [          9.156] [1073517588] (ssid='*****-guest', bssid=b'n\xd7\x9a\xc7^\x8a', sec=0, channel=6, rssi=-64)
    [          9.166] [1073517588] (ssid='*****', bssid=b'h\xd7\x9a\xc7^\x8a', sec=3, channel=6, rssi=-65)
    [          9.175] [1073517588] (ssid='*****', bssid=b'$ZLk\x9e\x00', sec=3, channel=11, rssi=-68)
    [          9.183] [1073517588] (ssid='*****-guest', bssid=b'&ZL\x1b\x9e\x00', sec=0, channel=11, rssi=-68)
    [          9.192] [1073517588] (ssid='*****', bssid=b'h\xd7\x9a\xc1\xb4\x8a', sec=3, channel=6, rssi=-85)
    [          9.201] [1073517588] (ssid='*****-guest', bssid=b'n\xd7\x9a\xc1\xb4\x8a', sec=0, channel=6, rssi=-86)
    [         10.217] [1073517588] Connecting with ***** and *****
    WiFi connection established
    [         12.126] [1073517588] MQTT Protocol
    [         12.132] [1073517588] starting new MsgHandler
    [         12.276] [1073517588] Packet sent. (Length: 110)
    [         12.431] [1073517588] This is PybytesProtocol.start_MQTT
    [         13.431] [1073646604] Packet sent. (Length: 44)
    Connected to MQTT mqtt.pybytes.pycom.io
    Pybytes connected successfully (using the built-in pybytes library)
    [         13.665] [1073517588] This is pack_info_message()
    [         13.681] [1073517588] pybytes
    [         13.690] [1073517588] __pack_message: b'010504a00586000000'
    [         13.699] [1073517588] MQTT Protocol
    [         13.708] [1073517588] Packet sent. (Length: 50)
    Pybytes configuration read from /flash/pybytes_config.json
    Pybytes is connected, sending signals to Pybytes
    [         16.397] [1073517588]  ssl_params={'ca_certs': '/flash/cert/pycom-ca.pem'}```

Log in to reply
 

Pycom on Twitter