lora.nvram_restore() OSError: [Errno 122] EMSGSIZE



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

    Hi, I'm using a Fipy (firm 1.20.3.b3) on a Pytrack 2.0X to send frame using LoRaWAN in EU868 with OTAA to connect with TTN. And , I want to use the lora methods nvram_save() and nvram_restore() to save my LoRaWAN state before going into deepsleep and restore them.

    And when I wake up after a deepsleep, I use the method restore and check with the method has_joined if I've already joined LoRaWAN before. And then, I try to send a new frame to TTN, I got an error :
    Traceback (most recent call last):
    File "main.py", line 47, in <module>
    OSError: [Errno 122] EMSGSIZE

    My code is bellow :

    from network import LoRa
    import socket
    import time
    import machine
    import ubinascii
    
    # set parameters for sensor function
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
    lora.nvram_restore()
    if not lora.has_joined():
        print("Firstly join")
        #-------------------- use OTAA to connect with TTN ------------------------------#
        app_eui = ubinascii.unhexlify('xxxxxxxxxxxxxxx')
        app_key = ubinascii.unhexlify('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
        dev_eui = ubinascii.unhexlify('xxxxxxxxxxxxxxx')
        lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=10000) # join a network using OTAA (Over the Air Activation)
        while not lora.has_joined():    # wait until the module has joined the network
            time.sleep(2.5)
            print('Not yet joined...')
        # join in
        print('Successfully join in!')
        #---------- Set LoRa parameters ----------------#
        # create a LoRa socket
        s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        # set the LoRaWAN data rate
        s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        #---------- Send uplink & Stroe uplink data ----------------#
        uplink_payload = bytes([0x01, 0x02, 0x03])
        s.setblocking(True)
        s.send(uplink_payload)
        s.setblocking(False)
        #time.sleep(1)
        print("")
        lora.nvram_save()   # store in flush to judge next time
        print("####################################")
    
    #-------------------------------- if already join -------------------------------------#
    else:
       
        print("Join already")
        #---------------------------Set LoRa parameters-------------------------------------#
        s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        #---------- Send uplink empty ----------------#
        uplink_payload = bytes([0x04, 0x05, 0x06])
        s.setblocking(True)
        s.send(uplink_payload)
        s.setblocking(False)
        print("Send frame after a restore")
        # time.sleep(5)
        lora.nvram_save()
        print("####################################")
    
    print("Deep sleep begin!")
    machine.deepsleep(10000)
    

    If someone knows why I got this error it will be very helpful.



  • @Etienne-DAL-PRA Thank you very much. This fixed it :)



  • @John-Baxter hi ! Yes of course. I'm sorry for the delay.
    I used the Pycom Firmware Updater (you can download it there https://docs.pycom.io/updatefirmware/device/).
    You need to close Atom or VS Code (the Pymakr console) before doing the upgrade. After you can do the upgrade with the Pycom Firmware Updater, you just need to select the Port and the type of your board.
    Then, I chose as type : Pybytes and as version the lastest : 1.20.2.r4. And selected the options :

    • Erase during update
    • CONFIG partition
    • NVS partition

    And clicked on Continue.

    I hope this helps you if you are still stuck :)



  • @Etienne-DAL-PRA Please can you advise the steps you have taken? I am having a very similar problem and I'm really stuck. Thank you :)



  • @jcaron I've found ! This problem comes from the fact that I flashed the card in "development" mode instead of "pybytes" and I put the version 1.20.2 r0 and it works well.



  • @Gijs Hi, I've already look at the example and it doesn't work too for me.

    The line 47 is the line "s.send(uplink_payload).

    I don't understand why this error appears, is the message size is saved when the nvram_save method is used ?

    @jcaron yes this my actual code to test the nvram_save and nvram_restore methods. I don't think 3 bytes will be out of the LoRaWAN limits.

    I will try without setting the data rate.

    Yes I changed the firmware this week but I didn't try to use these methods before. I will check in that direction too.

    Thank you for your responses :)



  • @Etienne-DAL-PRA is the code above the actual code you are using, especially the data being sent? There are limits on LoRaWAN packets which vary based on region, data rate, and more, but 3 bytes should always be possible, especially in the EU868 region, and even more so at DR5.

    Can you try without setting the data rate, or setting it to different values?

    One thing which may have an influence is the need to send MAC commands (or more likely, responses), but I think it’s quite difficult to get to a point where it would not even allow for 3 bytes of payload (also, I believe payload has precedence over MAC commands).

    Did you change firmware release recently? If so, did you join since then? Not quite sure the format of the LoRaWAN state in NVRAM has remained consistent across versions, it could get the stack in a weird state if not.


  • Global Moderator

    Have a look at the example for nvram here: https://docs.pycom.io/tutorials/networks/lora/nvram/

    Though I believe your issue has nothing to do with the nvram, but rather with the message size (either too big or too small). I cannot find exactly which line 'line 47' is in the code you attached, but perhaps you can find it yourself :)



Pycom on Twitter