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



  • Hi Everyone !
    I am confused with using lora.nvram_save and restore. I am using in my program but I am not sure where and how to use. Can someone guide me in this. I will put my code here.
    Thank you in Advance.

    from network import LoRa
    import socket
    import time
    import ubinascii
    store =["26012FF8", "26012FF7", "f82f0126" ]
    #creating a OTAA connection
    app_eui = ubinascii.unhexlify('0004A30B00000000')
    app_key = ubinascii.unhexlify('DE71C5CA71E05F92FE3B000000000')

    Join a network with OTAA

    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

    #wait until the module to join
    while not lora.has_joined():
    time.sleep(2.5)
    print('Not yet joined...')

    #create a lora socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

    #set lorawan datarate
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

    #make the socket blocking
    s.setblocking(True)

    time.sleep(3000)
    s.send(bytes([0x01, 0x02, 0x03]))

    time.sleep(3000)
    s.send(bytes([0x01, 0x02, 0x03]))

    lora.nvram_save()

    while True:

    lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, sf = 7, frequency = 868500000)
    
    # create a raw LoRa socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setblocking(True)
    rx_data = s.recv(64)
    dev_addr = rx_data[1:5]
    print("Dev-Addr: ", binascii.hexlify(dev_addr))
    x = 0
    for x in range(0, 3):
        if(binascii.unhexlify(store[x]) == dev_addr):
            print('OK') 
            # get any data received...
            #rx_data = s.recv(64)
            print(rx_data)
            # sending data to server with sf = 12
            lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868,  sf = 12)        
            # send some data
            s.send(rx_data)
            
            print('data sent')
        else:
            print('Not OK')
    

    lora = LoRa(mode=LoRa.LORAWAN)
    lora.nvram_restore()

    #join a network using OTAA
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

    #create a lora socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

    #doing uplink
    s.send(bytes([0x01, 0x02, 0x03]))

    #doing downlink
    data = s.recv(64)
    first4 = data[0:4]
    store.append(first4)
    second4 = data[5:9]
    store.append(second4)
    print(data)

    And here, I am trying to save my OTAA connection and at last I am restoring those keys to do uplink again to get some downlinks.



  • @jcaron oh thank you. I will do that. And I need a small help from you. In this code, I am trying to start my downlink once in a day. So I am using this code.

    import time

    a = time.localtime()

    hour = a.tm_hour
    minutes = a.tm_min

    if hour == 10 and minutes == 40:
    print('{0}:{1}'.format(hour, minutes))

    But it's not working properly. Do I have to change some conditions ?



  • @harish-kumar you only need to use nvram_save/restore if you use deep sleep (or cut power to the LoPy), which is not the case here.

    Just keep the same Lora object and socket and don’t join again...



Pycom on Twitter