Has anyone successfully used lora.nvram_save() and restore?



  • Re: How to accomplish lora.nvram_restore() ?

    I have tried many variations, but no luck for me. If anyone has been successful, would you please post your code?



  • @rloro338 I made the following changes to my code and now it works correctly:

    • list item make sure the socket is blocking ( s.setblocking(True) )

    • list item add a time.sleep(2) after sending, before saving and going to deep sleep.



  • I am trying to use lora.nvram_save() and lora.nvram_restore() and not works. I set all parameters (LoRaWAN, adr = True, app_eui, app_key and channel configuration) the first time that I boot the device and send a message Loriot server receive it. Then device go to deepsleep for 5 seconds and restore the join state. If I look the state with lora.has_joined() return True value, but when I trying to send the message it isn't received by the gateway.

    It is curious because when I use the node directly with a command line in Putty the restore and save comands works fine.

    What I am doing wrong??



  • @bmarkus this is the example code I'm testing, based on which share @jalperin, I'm missing something?

    from network import LoRa
    import struct
    import binascii
    import time
    import math
    import pycom
    import socket
    from pysense import Pysense

    py = Pysense()
    loraSaved = pycom.nvs_get('loraSaved')
    print(loraSaved)
    if not loraSaved:
    lora = LoRa(mode=LoRa.LORAWAN)
    freq = 902300000
    for channel in range(0, 72):
    lora.remove_channel(channel)
    for chan in range(0, 8):
    lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3)

    dev_addr = struct.unpack(">l", binascii.unhexlify('************'.replace(' ','')))[0]
    nwk_swkey = binascii.unhexlify('************ 89 F1 74 79 40'.replace(' ',''))
    app_swkey = binascii.unhexlify('************ 3A AA BD E8 A9'.replace(' ',''))
    lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
    while not lora.has_joined():
        machine.idle()
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)
    s.bind(3) #select the port number
    s.setblocking(False)
    s.send('LoRa Up')
    time.sleep(2)
    lora.nvram_save()
    pycom.nvs_set('loraSaved', 1)
    py.setup_sleep(60)
    py.go_to_sleep()
    

    else:
    lora = LoRa(mode=LoRa.LORAWAN)
    lora.nvram_restore()
    freq = 902300000
    for channel in range(0, 72):
    lora.remove_channel(channel)
    for chan in range(0, 8):
    lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3)
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)
    s.bind(3) #select the port number
    s.setblocking(False)
    s.send('LoRa Restored')
    time.sleep(2)
    lora.nvram_save()
    #pycom.nvs_set('loraSaved', 0) # reset for next test
    py.setup_sleep(60)
    py.go_to_sleep()



  • @johncaipa said in Has anyone successfully used lora.nvram_save() and restore?:

    @bmarkus in the documentation says that this value is saved, then I do not know what would be the problem, because in each sending after waking up the packet counters is always in 1 on TTN.
    "lora.nvram_save()
    Save the LoRaWAN state (joined status, network keys, packet counters, etc) in non-volatile memory in order to be able to restore the state when coming out of deepsleep or a power cycle."

    Do you create a new instance when coming out? It resets the counter.



  • @bmarkus in the documentation says that this value is saved, then I do not know what would be the problem, because in each sending after waking up the packet counters is always in 1 on TTN.
    "lora.nvram_save()
    Save the LoRaWAN state (joined status, network keys, packet counters, etc) in non-volatile memory in order to be able to restore the state when coming out of deepsleep or a power cycle."



  • @johncaipa said in Has anyone successfully used lora.nvram_save() and restore?:

    @jalperin @jmarcelino works but the counter is restarted on each send, this value is not stored?

    Unfortunately it is not possible to get or set your own FCnt value, otherwise it would be possible to handle in your code. Just one of the several missing LoRa/LoRaWAN functions.



  • @jalperin @jmarcelino works but the counter is restarted on each send, this value is not stored?



  • @jmarcelino

    Restoring the channel configuration did the trick. Setblocking(True) was not needed here.

    An anomaly that I have seen before showed up. The packets sent after nvram_restore all arrived with "payload not provided". But, if I first send a packet with a payload of a single character, it and all subsequent packets arrive successfully.

    May I suggest that the documentation be amended to detail what is and is not saved by lora.nvram_save().

    Thank you very much for your help.



  • @jalperin

    Try to restore your channel frequency configuration manually again after the nvram_restore, just use your existing code:

    for channel in range(0, 72):
        lora.remove_channel(channel)
    for chan in range(0, 8):
        lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3)
    

    The current nvram_save() may not save this...



  • @jalperin

    Umm so differences I see is I'm using OTAA and on EU868 region while you use ABP in the US915 region... also I do a few app related things before sending data and send several packets.

    Try s.setblocking(True) for your sends and also try more than one send, maybe a loop sending 10.



  • Thank you, jmarcellino. That is, I think, what I am doing, but without success. Below is my code. I get the first message, but not the second. I have check my gateway. It only sees the first message.

    from network import LoRa
    import struct
    import binascii
    import time
    import math
    import pycom
    import socket
    from pysense import Pysense

    py = Pysense()
    loraSaved = pycom.nvs_get('loraSaved')
    if not loraSaved:
    lora = LoRa(mode=LoRa.LORAWAN)
    freq = 902300000
    for channel in range(0, 72):
    lora.remove_channel(channel)
    for chan in range(0, 8):
    lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3)
    dev_addr = struct.unpack(">l", binascii.unhexlify('*****21E27'))[0]
    nwk_swkey = binascii.unhexlify('************4BD956A16FF3DADEB05608C1')
    app_swkey = binascii.unhexlify('*************738855C61661D0FF27')
    lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
    while not lora.has_joined():
    machine.idle()
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)
    s.setblocking(False)
    s.send('LoRa Up')
    time.sleep(2)
    lora.nvram_save()
    pycom.nvs_set('loraSaved', 1)
    py.setup_sleep(60)
    py.go_to_sleep()
    else:
    lora = LoRa(mode=LoRa.LORAWAN)
    lora.nvram_restore()
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)
    s.setblocking(False)
    s.send('LoRa Restored')
    time.sleep(2)
    pycom.nvs_set('loraSaved', 0) # reset for next test



  • Yes it's working and really simple.

    First time you do your normal join (you do need to wait until it has actually joined!) then

    lora.nvram_save()
    

    Afterwards do

    lora = LoRa(mode=LoRa.LORAWAN)
    lora.nvram_restore()
    
    
    s.send(bytes([0x01, 0x02, 0x03]))
    

Log in to reply
 

Pycom on Twitter

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.