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

    sending packets with Lopy (Lorawan)

    Scheduled Pinned Locked Moved LoPy
    lopylorawan
    5 Posts 2 Posters 3.8k Views 2 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.
    • K Offline
      kots
      last edited by

      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?

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jmarcelino @kots
        last edited by jmarcelino

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

        K 1 Reply Last reply Reply Quote 0
        • K Offline
          kots @jmarcelino
          last edited by

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

          J 1 Reply Last reply Reply Quote 0
          • J Offline
            jmarcelino @kots
            last edited by jmarcelino

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

            K 1 Reply Last reply Reply Quote 3
            • K Offline
              kots @jmarcelino
              last edited by

              @jmarcelino Thank you for your answer!

              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