Receiving Payload string from the gateway to the LoPy



  • Does anyone know if its possible to receive data on the LoPy from the gateway?

    I'm hoping to send a string to my LoPy from the gateway and then print it on the screen.

    using the standard piece of code with s.send() commented out.

    from network import LoRa
    import socket
    import binascii
    import struct
    
    # create an OTAA authentication parameters
    app_eui = binascii.unhexlify('AD A4 DA E3 AC 12 67 6B'.replace(' ',''))
    app_key = binascii.unhexlify('11 B0 28 2A 18 9B 75 B0 B4 D2 D8 C7 FA 38 54 8B'.replace(' ',''))
    
    # join a network using OTAA (Over the Air Activation)
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
    
    # wait until the module has joined the network
    while not lora.has_joined():
        time.sleep(2.5)
        print('Not yet joined...')
    
    # Initialize LoRa in LORAWAN mode.
    lora = LoRa(mode=LoRa.LORAWAN, adr=False, public=True)
    
    lora.add_channel(0, frequency=868000000, dr_min=0, dr_max=5)
    
    # join a network using OTAA
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key))
    
    # 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)
    
    # make the socket non-blocking
    s.setblocking(False)
    
    # send some data
    # commented out to receive come from network-server.
    # s.send(bytes([0x01, 0x02, 0x03]))
    
    # get any data received...
    data = s.recv(512)
    print(data)
    
    

    Then I'm sending a [msg.payload]: string, containing "Hello08152017"



  • @neilh93
    Great.

    Yes I use a similar thing for my commercial product. But remember that nodes may miss (due to RF issues, power) the "key change" message and still use the old one.

    What I did was to clone the existing device on the server backend but with the new keys. Then when a transmission arrives on the new device (which means the node has updated the keys) the old device is deleted.

    If the next transmission still arrives on the old device it means the node has not picked it up so a new key change message is scheduled for retransmission - this is repeated until it's finally picked up.



  • @jmarcelino okay sorry for that. The App Key and the App Eui are the "keys" i'm looking to update.

    Currently, thanks to your help, I have it working where i can send an up-link to the gateway and receive the desired down-link with my new App Key. I need to use a LoRaWAN Class A network and not "raw" LoRa.



  • @neilh93
    Sorry you'll have to explain a bit more. What keys are you trying to update, the LoRaWAN App Key ?

    I wonder if you really need to use LoRaWAN (with a network server etc) or can use "raw" LoRa with point-to-point connections?

    If you do need LoRaWAN you'll usually - unless you're operating a power hungry LoRaWAN Class C network - need to send a message first (can be anything) from the node in order to receive.

    That is how the bidirectionally works in Class A, the sent packet provides the initial timing reference for when the node will be listening.



  • @jmarcelino sorry for the long wait to reply. I'm trying to update the keys on the device so i would like to make use of the bidirectional communication to send a new key to the device by way of a message from field gateway.



  • @neilh93
    Yes, if you have something queued for downlink from your network server you should get it when you do an uplink (send). But your post is short on details. what does your gateway connect to?



  • @jmarcelino okay do If I set up send from the device to the gateway then send my string it should be able to be received that way?



  • @neilh93
    In LoRaWAN Class A you have to send something first before you are able to receive.



Pycom on Twitter