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

    LoRa API (with socket abstraction layer)

    Scheduled Pinned Locked Moved Announcements & News
    4 Posts 4 Posters 3.2k Views 5 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.
    • D Offline
      daniel
      last edited by abilio

      Here, some example code that shows how to use the LoRa module in the LoPy. Sending and receiving is done via sockets which is nice and makes code easier to port.

      For LoRa mode:

      from network import LoRa
      import socket
      
      # Initialize LoRa in LORA mode more params can be given, like frequency, tx power, spreading factor, etc
      lora = LoRa(mode=LoRa.LORA) 
      
      # create a raw LoRa socket
      s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
      s.setblocking(False)
      
      # send some data
      s.send('Hello from the LoPy')
      
      # get any data received...
      data = s.recv(64)
      print(data)
      

      For LoRaWAN mode:

      from network import LoRa
      import socket
      
      # Initialize LoRa in LORA mode more params can be given, like frequency, tx power, spreading factor, etc
      lora = LoRa(mode=LoRa.LORAWAN) 
      
      # create an OTAA authentication tuple (AppKey, AppEUI, DevEUI)
      auth = (bytes([0,1,2,3,4,5,6,7,8,9 ,A,B,C,D,E,F]), bytes([1,2,3,4,5,6,7,8]), bytes([8,7,6,5,4,3,2,1]))
      
      # join a network using OTAA (Over the Air Activation)
      lora.join(activation=LoRa.OTAA, auth=auth, timeout=0)
      
      # wait until the network is joined
      while not lora.has_joined():
          pass
      
      # create a raw LoRa socket
      s = socket.socket(socket.AF_LORAWAN, socket.SOCK_RAW)
      s.setblocking(False)
      
      # send some data
      s.send('Hello from the LoPy')
      
      # get any data received...
      data = s.recv(64)
      print(data)
      
      1 Reply Last reply Reply Quote 1
      • S Offline
        slock
        last edited by

        if there are multiple devices connected to a particular network, is there a way of determining where (i.e. which device) the received bytes have come from ?

        1 Reply Last reply Reply Quote 0
        • B Offline
          brotherdust
          last edited by

          @daniel : Thanks for the examples, but I am somewhat confused and am hoping to get a few things cleared up:

          • Doesn't there need to be a LoRaWAN gateway present for APB or OTAA mode to work?
          • Can you provide code examples for setting up a LoPy-based nano-gateway that will accept APB or OTAA requests?
          • Is "plain" LoRa mode encrypted? If so, does the API expose the keying functions somewhere? If not, I assume it's up to the developer to implement crypto.

          Thanks much!

          1 Reply Last reply Reply Quote 3
          • W Offline
            wstallwood
            last edited by

            Sorry for the late reply,

            The quick and simple way of implementing this is to deal with it in your packet string, define an ID field and run it as an include, have your build assign that file uniquely to each of your devices and then just have it as an element in the packet string.

            Then it's easy enough to unpack the string when you process it and filter by device.

            1 Reply Last reply Reply Quote 0

            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