lora.nvram_restore() is not working



  • Hi Everyone !
    I tried to implement lora.nvram_save() and lora.nvram_restore() in my program but lora.nvram_save() is working properly but I have some problem with the lora.nvram_restore(). My program stops after lora.nvram_restore() command and then it's not sending uplink. I am using lopy as a repeater. If someone knows, please help me out. I will post my program here.

    from network import LoRa
    import socket
    import time
    import binascii
    store =["26012FF8", "f82f0126", "f82f0136" ]
    
    
    a = time.localtime()
    print(a)
    hour = a[1]
    minutes = a[2]
    
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
    
    #create an OTAA authentication connection
    app_eui = binascii.unhexlify('0004A30000000000')
    app_key = binascii.unhexlify('DE71C5CA71E05F92F000000000000000')
    
    
    #Join a network using Lora
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
    
    #wait until it joins
    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 the lorawan datarate
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
    
    #make the socket blocking
    #(waits for the data to be sent and for the 2 receive windows to expire)
    s.setblocking(True)
    
    #time.sleep(3000)
    s.send(bytes([0x01, 0x02, 0x03]))
    
    time.sleep(3)
    s.send(bytes([0x01, 0x02, 0x03]))
    
    lora.nvram_save()
    print('saved')
    
    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')
    
    
    
    
    	if hour == 5 and minutes == 0:
    	    print('{0}:{1}'.format(hour, minutes))
    
            lora = LoRa(mode=LoRa.LORAWAN)
            lora.nvram_restore()
    
            # 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)
    


  • @robert-hh Thank you so much for your time. I made some changes and now my program works fine.



  • @harish-kumar Hello Harish Kumar,

    First of all, I try to understand what you want to achieve. As far as I can guess form the code and the board history of your questions, you want to implement a repeater between your nodes and a LoRaWAN gateway.

    a) I assume, that these nodes are configured in Raw LoRa mode.

    b) I assume, that you keep a list of known nodes in the repeater, and that this can be extended through downlink messages from the server.

    c) For the LoRa Server, all nodes appear under the ID of the repeater, and the ID of the nodes is part of the payload, which is supplied.

    I have a few comments to you code:

    a) Join has to be performed only once, if you keep the transaction parameters. That is to be done with nvram_save() and nvram_restore().

    b) You switch between LoRa Raw and LoRa WAN mode. I do not know, whether the WAN parameters are kept when switching to RAW mode. So better do the following:

    • call nvram_save() before switching to Raw mode (like at line 48), and
    • nvram_restore() like after line 57. The problem is, that this will wear our flash after a while. But once the code works, you may consider better strategies for save and restore.

    c) the s.recv() will block until you receive a packet. I do not know whether that is intentional. You could also set a timeout and check, whether you received something.

    d) the code block after "if hour == 5 and minutes == 0:" wil most likely never executed. First of all, you get the time at the start of your script. Unless you start that at exactly 5:00, the number are wrong. Secondly, even if you get the actual time before the if, since you wait forever in the s.recv(), it is highly unlikely that this will happen at the right time. And last not least, the mechanism is useless. The LoRaWAN server will send scheduled downlink messages after any uplink message. So it is much easier to check after any uplink message, if a downlink message arrives with configuration data. Still, you back-end system may provide this information once a day, but it dan be sent down at any time.
    And since downlink messages may get lost, you either sent a bunch of them and implement some mechanism to avoid duplicates, or implement another protocol for confirmation of receipt.



Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter