LoRa API (with socket abstraction layer)



  • 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)
    


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



  • @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!



  • 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 ?


Log in to reply
 

Pycom on Twitter