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 Comments & Feedback
    lopy
    4 Posts 2 Posters 1.2k 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.
    • M Offline
      MJ
      last edited by MJ

      Hi, currently i am using the code from https://docs.pycom.io/chapter/tutorials/lopy/lora-mac-nano-gateway.html and i have adding in some code to suit my application which is measure the time of receiving the ACK from the gateway by adding the Timer and send back the time value back to gateway. Unfortunately, i face the problems. Sorry for the unprofessional coding way since i am still new in programming.

      Gateway code :

      0_1520137639596_gateway.JPG

      For Node 1:
      0_1520137665050_node 1.JPG

      0_1520138182432_node 2.JPG

      • the coding for node 1 and node 2 is the same instead of the Device ID.

      Any help from others would appreciate, thank you.

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @MJ
        last edited by

        @mj The code is hard to see the way you post it. if you enclose it in lines with three backticks each (```) you can paste you code directly in the message.

        M 1 Reply Last reply Reply Quote 0
        • M Offline
          MJ @robert-hh
          last edited by MJ

          Hi, @robert-hh sorry for the inconvenient caused. Here is the code:

          Gateway :

          import socket
          import struct
          import time
          import machine
          from network import LoRa
          from machine import Timer
          # A basic package header, B: 1 byte for the deviceId, B: 1 byte for the pkg size, %ds: Formatted string for string`
          _LORA_PKG_FORMAT = "!BB%dsf"
          # A basic ack package, B: 1 byte for the deviceId, B: 1 byte 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`
          `# Please pick the region that matches where you are using the device:
          # Asia = LoRa.AS923
          # Australia = LoRa.AU915
          # Europe = LoRa.EU868
          # United States = LoRa.US915
          chrono = Timer.Chrono()
          lora = LoRa(mode=LoRa.LORA, frequency = 915000000)
          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[2]
          
                  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)
          

          Node :

          import os
          import socket
          import time
          import struct
          import machine
          import network
          from network import LoRa
          from machine import Timer
          
          # A basic package header, B: 1 byte for the deviceId, B: 1 byte for the pkg size
          _LORA_PKG_FORMAT = "BB%dsf"
          _LORA_PKG_ACK_FORMAT = "BBB"
          DEVICE_ID = 0x01
          
          # Open a Lora Socket, use tx_iq to avoid listening to our own messages
          # Please pick the region that matches where you are using the device:
          # Asia = LoRa.AS923
          # Australia = LoRa.AU915
          # Europe = LoRa.EU868
          # United States = LoRa.US915
          chrono = Timer.Chrono()
          lora = LoRa(mode=LoRa.LORA, frequency = 915000000)
          lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
          lora_sock.setblocking(False)
          
          while(True):
              # Package send containing a simple string
              msg1 = "Device 1 Here"
              msg2 = "Time difference %f total"
              msg = "format_string % (msg1,msg2)"
              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)
                  chrono.start()
          
                  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):
                              chrono.stop()
                              total = chrono.read_us()
                              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 problem that i face is the : "buffer too small " , "cannot convert str to int", "3 more values to unpack" . I have no idea on how it works since i am still new in python programming, any help would appreciate. Thank you.

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @MJ
            last edited by

            @mj I do not know what you intention was, but may something like this, which just changes the message content and initializes total for the first run.

            import os
            import socket
            import time
            import struct
            import machine
            import network
            from network import LoRa
            from machine import Timer
            
            # A basic package header, B: 1 byte for the deviceId, B: 1 byte 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
            # Please pick the region that matches where you are using the device:
            # Asia = LoRa.AS923
            # Australia = LoRa.AU915
            # Europe = LoRa.EU868
            # United States = LoRa.US915
            chrono = Timer.Chrono()
            lora = LoRa(mode=LoRa.LORA, frequency = 915000000)
            lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
            lora_sock.setblocking(False)
            total = 0
            
            while(True):
                # Package send containing a simple string
                msg = "Device 1 Here, Time difference %d total" % total
                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)
                    chrono.start()
            
                    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):
                                chrono.stop()
                                total = chrono.read_us()
                                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)
            
            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