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

    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)
    s.send(databytes)
    

    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
    print("start")

    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('
    ***********')

    lora.add_channel(0,frequency=868100000,dr_min=0,dr_max=5)
    lora.add_channel(1,frequency=868100000,dr_min=0,dr_max=5)
    lora.add_channel(2,frequency=868100000,dr_min=0,dr_max=5)

    #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():
    pass
    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)

    s.setblocking(False)

    adc=machine.ADC()
    apin=adc.channel(pin='P16')
    value=apin()
    temp=((value*1100)/1024-500)/100

    print("Sending Packet")
    s.send(temp)

    get the packet received (if any)

    data = s.recv(64)
    if data:
    print(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: https://docs.pycom.io/chapter/tutorials/lopy/lorawan-otaa.html

    Fot ABP: https://docs.pycom.io/chapter/tutorials/lopy/lorawan-abp.html

    Finally make sure your LoPy firmware is up to date.


 

Pycom on Twitter