LORA NanoGateway (RAW) and MQTT publish ? disconnected
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 # 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 :)
livius last edited by
simple use e.g.:
try: s.send(data) except: connectToRouter()
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.
livius last edited by
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.