Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    method to return if lora.nvram is available

    Scheduled Pinned Locked Moved LoPy
    nvramrestorelorawanlopy
    4 Posts 2 Posters 309 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • philwilkinsonP Offline
      philwilkinson
      last edited by

      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)
      
      1 Reply Last reply Reply Quote 0
      • A Offline
        Asb
        last edited by Asb

        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

        philwilkinsonP 1 Reply Last reply Reply Quote 1
        • philwilkinsonP Offline
          philwilkinson @Asb
          last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • philwilkinsonP Offline
            philwilkinson
            last edited by

            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)
            1 Reply Last reply Reply Quote 1

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom