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

    LORA NanoGateway (RAW) and MQTT publish ? disconnected

    Scheduled Pinned Locked Moved LoPy
    lora lopydisconnectedmqttpublishwifi fails
    4 Posts 2 Posters 1.1k 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.
    • S Offline
      siejak
      last edited by

      HI

      I would like to use my Lopy as NanoGateway with Lora RAW and send all the received packages over MQTT to an online Broker.

      import time
      import pycom
      import socket
      import struct
      from network import LoRa
      from network import WLAN
      
      import ufun
      
      # A basic package header
      # B: 1 byte for the deviceId
      # B: 1 byte for the pkg size
      # B: 1 byte for the messageId
      # %ds: Formated string for string
      _LORA_PKG_FORMAT = "!BBB%ds"
      
      # A basic ack package
      # B: 1 byte for the deviceId
      # B: 1 byte for the pkg size
      # B: 1 byte for the messageId
      # B: 1 byte for the Ok (200) or error messages
      _LORA_PKG_ACK_FORMAT = "BBBB"
      
      # Open a Lora Socket, use rx_iq to avoid listening to our own messages
      lora = LoRa(mode=LoRa.LORA, rx_iq=True, frequency=864500000, sf=12)
      lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
      lora_sock.setblocking(False)
      
      
      
      broker_addr = "m23.cloudmqtt.com" 
      #broker_addr = "test.mosquitto.org"
      MYDEVID = "PMdev"
      
      wlan = WLAN(mode=WLAN.STA)
      wlan.antenna(WLAN.INT_ANT)
      wlan.connect("wifiwasdeleted", auth=(WLAN.WPA2, "passwordwasdeleted"), timeout=5000)
      
      def settimeout(duration):
         pass
      
      while not wlan.isconnected():
           machine.idle()
      
      print("connected to WIFI")
      
      client = MQTTClient(MYDEVID, broker_addr, user="userwasdeleted", password="passwordwasdeleted", port=portwaschanged) 
      client.settimeout = settimeout
      client.connect()
      
      
      
      print('Sending messages...')
      while True:
          
          #LORA SETUP
          # Since the maximum body size in the protocol is 255 the request is limited to 512 bytes
          recv_pkg = lora_sock.recv(512)
      
      
          # If at least a message with the header is received process it
          if (len(recv_pkg) > 3):
              print("Package received")
              recv_pkg_len = recv_pkg[1]
      
              # If message is corrupted should not continue processing
              if (not len(recv_pkg) == recv_pkg_len + 3):
                  #continue
                  break
      
              else:
                  # Unpack the message based on the protocol definition
                  device_id, pkg_len, msg_id, msg = struct.unpack(_LORA_PKG_FORMAT % recv_pkg_len, recv_pkg)
                  print(device_id)
                  print(msg)
                  # Respond to the device with an acknoledge package
                  time.sleep(0.50)
                  ack_pkg = struct.pack(_LORA_PKG_ACK_FORMAT, device_id, 1, msg_id, 200)
                  #print(ack_pkg)
                  lora_sock.send(ack_pkg)
                  # publishing the data
                  client.publish(MYDEVID+'/value', str(msg))
                  print("sent ack")
      
      

      It's all working quite well but after about 4 hours the gateway is not forwarding any data to Mqtt broker. Is there any possibility to reconnect to Wifi after it has been disconnected? Thanks for help :)

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

        @siejak said in LORA NanoGateway (RAW) and MQTT publish ? disconnected:

        Is there any possibility to reconnect to Wifi after it has been disconnected?

        Simply do what you have done at start of the script - connect once again.
        I make connection and configuration of wifi as procedure it then is simply as e.g. connectToRouter() and if i got socket send error then i try reconnect to wifi by run the proc.

        think about people first, then computers

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          siejak @livius
          last edited by

          @livius Hi

          Thanks for this quick response. Im quite new to micro-python :) How do I get the socket send error ? Could you give me a quick example what I need to write. Thank you.

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

            @siejak
            simple use e.g.:

            try:
                 s.send(data)
            except:
                connectToRouter()

            think about people first, then computers

            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