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

    LoPy Nano-Gateway

    Scheduled Pinned Locked Moved Guides
    62 Posts 27 Posters 50.6k Views 31 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.
    • R Offline
      Roberto
      last edited by Roberto

      **NOTE: THIS EXAMPLE HAS BEEN UPDATED AND I RECOMMEND USING THE NEW CODE THAT CONTAINS SEVERAL IMPROVEMENTS

      GO TO https://forum.pycom.io/post/3242

      Here are some code samples to put the LoPy in nano-gateway mode. This is just a code demo and you will need to change it to meet your needs.

      For this demo we are connecting 2 LoPys (nodes) to 1 LoPy in Nano-Gateway mode

      nano_gateway.py

      import socket
      import struct
      from network import LoRa
      
      # A basic package header, B: 1 byte for the deviceId, B: 1 byte for the pkg size, %ds: Formated string for string
      _LORA_PKG_FORMAT = "!BB%ds"
      # A basic ack package, B: 1 byte for the deviceId, B: 1 bytes for the pkg size, B: 1 byte for the Ok (200) or error messages
      _LORA_PKG_ACK_FORMAT = "BBB"
      
      # Open a LoRa Socket, use rx_iq to avoid listening to our own messages
      lora = LoRa(mode=LoRa.LORA, rx_iq=True)
      lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
      lora_sock.setblocking(False)
      
      while (True):
          recv_pkg = lora_sock.recv(512)
          if (len(recv_pkg) > 2):
              recv_pkg_len = recv_pkg[1]
      
              device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
      
      # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
              print('Device: %d - Pkg:  %s' % (device_id, msg))
      
              ack_pkg = struct.pack(_LORA_PKG_ACK_FORMAT, device_id, 1, 200)
              lora_sock.send(ack_pkg)
      

      The _LORA_PKG_FORMAT is used to have a way of identifying the different devices in our network
      The _LORA_PKG_ACK_FORMAT is a simple ack package as response to the nodes package

      node_1.py

      import os
      import socket
      import time
      import struct
      from network import LoRa
      
      # A basic package header, B: 1 byte for the deviceId, B: 1 bytes for the pkg size
      _LORA_PKG_FORMAT = "!BB%ds"
      _LORA_PKG_ACK_FORMAT = "BBB"
      DEVICE_ID = 0x01
      
      
      # Open a Lora Socket, use tx_iq to avoid listening to our own messages
      lora = LoRa(mode=LoRa.LORA, tx_iq=True)
      lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
      lora_sock.setblocking(False)
      
      while(True):
          # Package send containing a simple string
          msg = "Device 1 Here"
          pkg = struct.pack(_LORA_PKG_FORMAT % len(msg), DEVICE_ID, len(msg), msg)
          lora_sock.send(pkg)
          
          # Wait for the response from the gateway. NOTE: For this demo the device does an infinite loop for while waiting the response. Introduce a max_time_waiting for you application
          waiting_ack = True
          while(waiting_ack):
              recv_ack = lora_sock.recv(256)
          
              if (len(recv_ack) > 0):
                  device_id, pkg_len, ack = struct.unpack(_LORA_PKG_ACK_FORMAT, recv_ack)
                  if (device_id == DEVICE_ID):
                      if (ack == 200):
                          waiting_ack = False
                          # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
                          print("ACK")
                      else:
                          waiting_ack = False
                          # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
                          print("Message Failed")
      
          time.sleep(5)
      

      The node is always sending packages and waiting for the ack from the gateway.

      To adapt this code to your needs you might:

      1. Put a max waiting time for the ack to arrive and resend the package or mark it as invalid
      2. Increase the package size changing the _LORA_PKG_FORMAT to "BH%ds" the H will allow to keep 2 bytes for size (for more information about struct format go here)
      3. Reduce the package size with bitwise manipulation
      4. Reduce the message size (for this demo a string) to something more useful for you development
      G B C F 4 Replies Last reply Reply Quote 8
      • K Offline
        KDHeard
        last edited by

        Hi Roberto - thank you very much for the code!!! I am looking forward to using it... comments to follow.

        Thanks again

        Kind regards

        Kevin Heard - Founder
        DLI Systems
        www.dli.systems| Do - Learn - Improve

        1 Reply Last reply Reply Quote 0
        • B Offline
          brotherdust
          last edited by

          @Roberto, thanks so much! I was brainstorming how to do this exact thing this week and you've done it better than I could have. =) Do you think adding a checksum function would be useful here or is the underlying protocol ensuring data integrity?

          R 1 Reply Last reply Reply Quote 0
          • bmarkusB Offline
            bmarkus
            last edited by

            There is a CRC used on the physical leyer.

            1 Reply Last reply Reply Quote 0
            • G Offline
              gertjanvanhethof Pybytes Beta @Roberto
              last edited by

              @Roberto Thanks for the example code!

              Question: Is there a way to use the LoPy as LoRaWAN Gateway (OTAA or ABP)?

              I have a LoRa Node from Elsys (Model ESM5k) which is able to send temperature data.
              I can configure key's on this device using a NFC App on my mobile phone.

              So I want to setup a LoRaWAN connection from that device to the LoPy Gateway.
              How can the LoPy being programmed doing this?

              Is it possible to do this with the LoPy?

              bmarkusB R F 3 Replies Last reply Reply Quote 1
              • bmarkusB Offline
                bmarkus @gertjanvanhethof
                last edited by

                @gertjanvanhethof said in LoPy Nano-Gateway:

                @Roberto Thanks for the example code!

                Question: Is there a way to use the LoPy as LoRaWAN Gateway (OTAA or ABP)?

                I have a LoRa Node from Elsys (Model ESM5k) which is able to send temperature data.
                I can configure key's on this device using a NFC App on my mobile phone.

                So I want to setup a LoRaWAN connection from that device to the LoPy Gateway.
                How can the LoPy being programmed doing this?

                Is it possible to do this with the LoPy?

                You can not make a LoRaWAN GW with LoPy nor any other SX127x cip based bords which can meet the LoRaWAN specification.

                G 1 Reply Last reply Reply Quote 0
                • G Offline
                  gertjanvanhethof Pybytes Beta @bmarkus
                  last edited by

                  @bmarkus

                  Thanks for the answer.

                  Is it possible to read LoRaWAN data on LoRA level (not WAN level)?
                  So building micropython code to get the encrypted data and use the network key to decrypt it and the app key to decrypt the app payload myself.
                  Is this an opportunity?

                  bmarkusB 1 Reply Last reply Reply Quote 0
                  • bmarkusB Offline
                    bmarkus @gertjanvanhethof
                    last edited by

                    @gertjanvanhethof said in LoPy Nano-Gateway:

                    @bmarkus

                    Thanks for the answer.

                    Is it possible to read LoRaWAN data on LoRA level (not WAN level)?
                    So building micropython code to get the encrypted data and use the network key to decrypt it and the app key to decrypt the app payload myself.
                    Is this an opportunity?

                    LoRaWAN specification is public, you can decode LoRaWAN packets and if you know the keys you can decrypt. The main issue is the radio layer, you can listen on single frequency and you can decode only a fixed preselected SF.

                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      brotherdust
                      last edited by

                      @Roberto, both the gateway and node code crash my LoPy's. I did a factory reset on them thinking maybe my code was involved. Nope.

                      Here's the console log:

                      rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
                      configsip: 0, SPIWP:0x00
                      clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
                      mode:QIO, clock div:2
                      load:0x3fff9010,len:8
                      load:0x3fff9018,len:312
                      load:0x40078000,len:2348
                      ho 0 tail 12 room 4
                      load:0x4009f000,len:1364
                      entry 0x4009f2f8
                      ˜øXTAL 40Mep 26 2016 22:32:10
                      frc2_timer_task_hdl:3ffda618, prio:22, stack:2048
                      tcpip_task_hdlxxx : 3ffdede4, prio:18,stack:2048
                      phy_version: 123, Sep 13 2016, 20:01:58, 0
                      pp_task_hdl : 3ffea6b4, prio:23, stack:8192
                      mode : softAP(24:0a:c4:00:b6:41)
                      dhcp server start:(ip: 192.168.4.1, mask: 255.255.255.0, gw: 192.168.4.1)
                      dhcp server start:(ip: 192.168.4.1, mask: 255.255.255.0, gw: 192.168.4.1)
                      MicroPython d78a5a3 on 2016-11-05; LoPy with ESP32
                      Type "help()" for more information.
                      >>>
                      paste mode; Ctrl-C to cancel, Ctrl-D to finish
                      === import socket
                      === import struct
                      === from network import LoRa
                      ===
                      === # A basic package header, B: 1 byte for the deviceId, B: 1 byte for the pkg size, %ds: Formated string for string
                      === _LORA_PKG_FORMAT = "!BB%ds"
                      === # A basic ack package, B: 1 byte for the deviceId, B: 1 bytes for the pkg size, B: 1 byte for the Ok (200) or error messages
                      === _LORA_PKG_ACK_FORMAT = "BBB"
                      ===
                      === # Open a LoRa Socket, use rx_iq to avoid listening to our own messages
                      === lora = LoRa(mode=LoRa.LORA, rx_iq=True)
                      === lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
                      === lora_sock.setblocking(False)
                      ===
                      === while (True):
                      ===     recv_pkg = lora_sock.recv(512)
                      ===     if (len(recv_pkg) > 2):
                      ===         recv_pkg_len = recv_pkg[1]
                      ===
                      ===         device_id, pkg_len, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
                      ===
                      === # If the uart = machine.UART(0, 115200) and os.dupterm(uart) are set in the boot.py this print should appear in the serial port
                      ===         print('Device: %d - Pkg:  %s' % (device_id, msg))
                      ===
                      ===         ack_pkg = struct.pack(_LORA_PKG_ACK_FORMAT, device_id, 1, 200)
                      ===         lora_sock.send(ack_pkg)
                      ===
                      Task watchdog got triggered. The following tasks did not feed the watchdog in time:
                      Tasks currently running:
                      CPU 0: MicroPy
                      Aborting.
                      Guru Meditation Error: Core   0 panic'ed (Unhandled debug exception)
                      Register dump:
                      PC      :  400e7078  PS      :  00060036  A0      :  800f221e  A1      :  3ffd5440
                      A2      :  0000000a  A3      :  0000000a  A4      :  00000004  A5      :  00000000
                      A6      :  00000000  A7      :  fffffff8  A8      :  00000000  A9      :  3ffd53b0
                      A10     :  00000000  A11     :  3ffdd923  A12     :  3ffd537f  A13     :  00000030
                      A14     :  00000000  A15     :  00000200  SAR     :  00000013  EXCCAUSE:  00000001
                      EXCVADDR:  00000000  LBEG    :  4000c46c  LEND    :  4000c477  LCOUNT  :  00000000
                      CPU halted.
                      
                      R 2 Replies Last reply Reply Quote 0
                      • K Offline
                        KDHeard
                        last edited by

                        @Roberto @daniel I have the same issue as brotherdust.

                        Reading up about the 'struct' I found a commen that in ypython the name if the module has changed - refer point 11:

                        https://github.com/micropython/micropython/wiki/Differences

                        I changed the code to reflect 'ustruct' and the LoPys no longer crash and I can still see them with my Pymakr - however they are not connecting..

                        Kind regards

                        Kevin Heard - Founder
                        DLI Systems
                        www.dli.systems| Do - Learn - Improve

                        1 Reply Last reply Reply Quote 0
                        • R Offline
                          Roberto @brotherdust
                          last edited by

                          Hi @brotherdust,

                          A CRC its not required as it is included in the protocol. You can still implement it if you want. I tried this in a project we have running just to make sure and all the checksums are positive

                          1 Reply Last reply Reply Quote 1
                          • R Offline
                            Roberto @gertjanvanhethof
                            last edited by

                            Hi @gertjanvanhethof
                            I'll provide a new code example this week on the blog section that might answer your question.

                            Best,
                            Roberto

                            1 Reply Last reply Reply Quote 0
                            • R Offline
                              Roberto @brotherdust
                              last edited by

                              Hi @brotherdust
                              This issue is not related to the nano-gateway code, it is a known issue regarding the WatchDog and we will be addressing it soon, i'll keep you informed with a fix for this.

                              1 Reply Last reply Reply Quote 0
                              • R Offline
                                Roberto @brotherdust
                                last edited by

                                Hi @brotherdust,

                                The watchdog problem was fixed in the new firamware version, please download it since this is important not only for the nano-gateway but for all the other apps you want to run as well.

                                The update is here:
                                https://www.pycom.io/support/supportdownloads/

                                Please test the nano-gateway code afterwards to make sure that everything is working

                                Best,
                                Roberto

                                1 Reply Last reply Reply Quote 0
                                • K Offline
                                  kgadeshi
                                  last edited by

                                  @Roberto
                                  Is it possible to implement some kind of ranging algorithm in LoRa. So that one can calculate the distance between the nodes to the gateway or something along those lines? Thanks.

                                  1 Reply Last reply Reply Quote 0
                                  • L Offline
                                    livius
                                    last edited by

                                    time measurement should be then in nanoseconds i believe
                                    you can send signal and wait for receive and calc distance divide time by two ;-)

                                    think about people first, then computers

                                    1 Reply Last reply Reply Quote 0
                                    • D Offline
                                      daniel
                                      last edited by

                                      @kgadeshi with the RSSI of the received packet and knowing the amount of power that was used to transmit it, the free space loss formula can be used to estimate the distance between the 2 nodes.

                                      1 Reply Last reply Reply Quote 0
                                      • B Offline
                                        Batilan
                                        last edited by Batilan

                                        You might also have a look at these resources:

                                        • An extensive discussion on the TTN forum
                                        • An article by Linklabs stating the issues especially for LoRa (they do have competing products, but the reasoning seems sound)
                                        1 Reply Last reply Reply Quote 0
                                        • X Offline
                                          Xykon administrators
                                          last edited by

                                          After quite a bit of trial and error I have managed to get the LoPy to "talk" in P2P mode to a Dragino LoRa/GPS hat (which uses the same SX1272 chip that's in the LoPy.

                                          For anyone interested I have posted some more details here and have made the source code for the LoPy and Raspberry Pi available on github.

                                          Christian Ehlers

                                          A 1 Reply Last reply Reply Quote 4
                                          • A Offline
                                            arista @Xykon
                                            last edited by

                                            This is extremely exiting, since I have exactly the same hardware! I hope I'll have some time this month to test your code, @Xykon, and make the bits fly.

                                            1 Reply Last reply Reply Quote 1

                                            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
                                            • 2
                                            • 3
                                            • 4
                                            • 1 / 4
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom