LTE & MQTT functionality on the GPY (OSError: Avialable Interfaces are down)



  • Hi,
    I'm trying to send MQTT commands to Adafruit via the LTE network on my GPY.
    I've managed to get this LTE code working: LTE code
    And I've managed to send data over wifi to Adafruit using this umqqt/simple.py library: umqtt code.

    My problem is that when I take the umqtt code and add it to the LTE code so that I may now send data using LTE, I get this error:

    Uploading project (main folder)...
    Safe booting device... (see settings for more info)
    Uploading to /flash...
    Reading file status
    No files to upload
    Upload done, resetting board...
    OKets Jun  8 2016 00:22:57
    
    rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff8028,len:8
    load:0x3fff8030,len:1688
    load:0x4009fa00,len:0
    load:0x4009fa00,len:14592
    entry 0x400a059c
    Traceback (most recent call last):
      File "main.py", line 107, in <module>
      File "umqtt.py", line 26, in __init__
    OSError: Avialable Interfaces are down
    Pycom MicroPython 1.18.2.r7 [v1.8.6-849-df9f237] on 2019-05-14; GPy with ESP32
    Type "help()" for more information.```
    

    Here is my code:

    
    
    
    import time
    from machine import RTC
    from network import LTE
    from umqtt import MQTTClient
    import socket
    import ssl
    
    def settimeout(duration):
        pass
    
    def sub_cb(topic,msg):
        print(msg)
    
    NTP_SERVER = "au.pool.ntp.org"
    
    # Need to use global variables.
    # If in each function you delare a new reference, functionality is broken
    lte = LTE()
    rtc = RTC()
    
    # Returns a network.LTE object with an active Internet connection.
    def getLTE():
    
        # If already used, the lte device will have an active connection.
        # If not, need to set up a new connection.
        if lte.isconnected():
            return lte
    
        # Modem does not connect successfully without first being reset.
        print("Resetting LTE modem ... ", end='')
        lte.send_at_cmd('AT^RESET')
        print("OK")
        time.sleep(1)
    
        # While the configuration of the CGDCONT register survives resets,
        # the other configurations don't. So just set them all up every time.
        print("Configuring LTE ", end='')
        lte.send_at_cmd('AT+CGDCONT=1,"IP","telstra.internet"')
        print(".", end='')
        lte.send_at_cmd('AT!="RRC::addscanfreq band=28 dl-earfcn=9410"')
        print(".", end='')
        lte.send_at_cmd('AT+CFUN=1')
        print(" OK")
    
        # If correctly configured for carrier network, attach() should succeed.
        if not lte.isattached():
            print("Attaching to LTE network ", end='')
            lte.attach()
            while(True):
                if lte.isattached():
                    print(" OK")
                    break
                print('.', end='')
                time.sleep(1)
    
        # Once attached, connect() should succeed.
        if not lte.isconnected():
            print("Connecting on LTE network ", end='')
            lte.connect()
            while(True):
                if lte.isconnected():
                    print(" OK")
                    break
                print('.', end='')
                time.sleep(1)
    
        # Once connect() succeeds, any call requiring Internet access will
        # use the active LTE connection.
        return lte
    
    # Clean disconnection of the LTE network is required for future
    # successful connections without a complete power cycle between.
    def endLTE():
    
        print("Disonnecting LTE ... ", end='')
        lte.disconnect()
        print("OK")
        time.sleep(1)
        print("Detaching LTE ... ", end='')
        lte.dettach()
        print("OK")
    
    # Sets the internal real-time clock.
    # Needs LTE for Internet access.
    def setRTC():
    
        # Ensures LTE session is connected before attempting NTP sync.
        lte = getLTE()
    
        print("Updating RTC from {} ".format(NTP_SERVER), end='')
        rtc.ntp_sync(NTP_SERVER)
        while not rtc.synced():
            print('.', end='')
            time.sleep(1)
        print(' OK')
    
    # Only returns an RTC object that has already been synchronised with an NTP server.
    def getRTC():
    
        if not rtc.synced():
            setRTC()
    
        return rtc
    
    # Program starts here.
    client= MQTTClient("GPY","io.adafruit.com",user="hidden",password="hidden",port=1883)
    client.settimeout=settimeout
    client.set_callback(sub_cb)
    client.connect()
    print("connected to broker")
    client.publish(topic="hidden/feeds/test",msg="False")
    print("published")
    time.sleep(10)
    client.subscribe(topic="hidden/feeds/test")
    print("subscribed!") 
    
    
    
    
    
    try:
        print("Initially, the RTC is {}".format("set" if rtc.synced() else "unset"))
        rtc = getRTC()
        while(True):
            #client.check_msg()
            #time.sleep(1)
            print("RTC is {}".format(rtc.now() if rtc.synced() else "unset"))
            time.sleep(5)
    except Exception:
        pass # do nothing on error
    
    finally:
        endLTE()
    
    

    Please note: The device and modem firmware is up to date & I am using VSCode.
    I would much appreciate some guidance on what I should do to have both LTE & MQTT working on the GPy. I would also appreciate some explanation of the error.

    Thank you in advance



  • @DonovanBlackLab If I'm not mistaken, you're starting to use the MQTT client before you connect LTE...

    Make sure LTE is up and running before you start MQTT and that should solve things.


Log in to reply
 

Pycom on Twitter