sending packets with Lopy (Lorawan)

  • Hello!

    I am trying to send values from my TMP 36 sensor through the lopy to my gateway. I am using the loriot network server.

    The problem is that when i am trying to send the float data to my gateway i see this message :0_1501260739566_upload-29a7f69c-deca-42c1-82f2-cc33034eef73

    The command i use is : s.send(datafromtmp).

    I know that loriot shows any message i send in a hex form. I tried to use the binascii.hexlify method but it didnt work.

    Any thoughts?

  • @jmarcelino Thank you for your answer!

  • @kots

    The problem is a socket only understand bytes, it doesn't know what do with integers or floating point values such as your temp.

    You need to convert your temp into bytes ( and then back on the other side). The suggested way is using struct.pack() like this:

    import struct
    databytes = struct.pack('d', temp)

    this is for Python doubles (64 bits floating points), if you want C style 32 bit floats (losing precision but using less bytes) use f instead of d. See the struct docs for more options.

  • @jmarcelino sure!
    here is the code:
    from network import LoRa
    import time
    import socket
    import binascii
    import struct
    import machine
    #import pycom

    #pycom.heartbeat(False) #it turns the pycoms led off
    #starting the procedure

    lora = LoRa(mode=LoRa.LORAWAN)

    print("Dev EUI: ", binascii.hexlify(lora.mac()).upper().decode('utf-8'))

    #ABP configuration

    create an ABP authentication params

    dev_addr = struct.unpack(">l", binascii.unhexlify(''))[0]
    nwk_swkey = binascii.unhexlify('
    app_swkey = binascii.unhexlify('


    #joining the network
    lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))

    wait until the network is joined

    while not lora.has_joined():
    print("Joined LoRa Network")

    #remove all the non-default channels

    create a raw LoRa socket

    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)



    print("Sending Packet")

    get the packet received (if any)

    data = s.recv(64)
    if data:

  • @kots

    Can you post the part of your code where you create the LoRa socket and join the network?

    Alternatively follow the examples:

    For OTAA:

    Fot ABP:

    Finally make sure your LoPy firmware is up to date.

Log in to reply

Pycom on Twitter

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.