method to return if lora.nvram is available



  • When a device has rebooted from a deepsleep, is there a simple method to check if lora.nvram_save() was implemented and can be used with lora.nvram_restore()?
    line 3 in the sample code below is what I am trying to achieve, however calling lora.nvram_restore() does not return a value, so it cannot be used.
    Any ideas?

    from network import LoRa
    lora = LoRa(mode=LoRa.LORAWAN)
    if lora.nvram_restore(): #I want to do something like this
        lora.nvram_restore()
        while not lora.has_joined():
            time.sleep(0.5)
    else:
        lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915, adr=False, tx_retries=0, device_class=LoRa.CLASS_A)
        appeui = binascii.unhexlify(app_eui)
        appkey = binascii.unhexlify(app_key)
        lora.join(activation=LoRa.OTAA, auth=(appeui, appkey), timeout=0)
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
    s.setblocking(True)
    s.send("some data")
    s.setblocking(False)
    rev_data = s.recv(64)
    print('recieved data: ', rev_data)
    lora.nvram_save()
    machine.deepsleep(some_time)
    


  • OK @Asb comment got me thinking that everyone seems to be fine with the lora.has_joined() method call.
    So I put a slight delay following the nvram_restore call, and it seems to work ok. 2 seconds seems to be optimal.
    This code seems to work most of the time.

        lora = LoRa(mode=LoRa.LORAWAN)
        lora.nvram_restore():
        time.sleep(2)
        if not lora.has_joined():
            print('failed to join via nvram_restore')
            lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915, adr=False, tx_retries=0, device_class=LoRa.CLASS_A)
            for i in range(16, 65): #AU915 TTN gateways need channels removed
                lora.remove_channel(i)
            for i in range(66, 72):
                lora.remove_channel(i)
            appeui = binascii.unhexlify(app_eui)
            appkey = binascii.unhexlify(app_key)
            lora.join(activation=LoRa.OTAA, auth=(appeui, appkey), timeout=0)
            while not lora.has_joined():
                print('trying to join OTAA')
                time.sleep(0.5)
            print('connected and ready to publish')
    
        s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        s.setblocking(True)
        s.send("some data")
        s.setblocking(False)
        rev_data = s.recv(64)
        print('recieved data: ', rev_data)
        lora.nvram_save()
        time.sleep(2)


  • @Asb thanks for the response.
    As you can see from the code, I am using lora.has_joined() method. However lora.has_joined() takes some time to connect and return True, also the time it takes to connect is not predictable.
    I hoped to call a method that immediately returned a Boolean to reduce time 'wasted' waiting to connect when nvram_save() was never used.



  • If you start by calling nvram_restore, you can check using lora.has_joined(), if lora.navram_save() was successfully saved, lora.has_joined() will return true



Pycom on Twitter