LoPy4 stops receiving LoRa messages
Device A: LoPy4 (the one experiencing the problem)
Device B: LoPy4 (works fine)
The system works as follows:
- A transmits a message to B
- A then waits for acknowledgement from B
- B waits for message from A
- B then transmits an acknowledgement to A
The system works fine for some time, but eventually device A becomes unable to receive messages. Device B is definitely transmitting the acknowledgement message (confirmed by listening using another device), but device A behaves as if it is not receiving any messages.
I then set device A to perform a hard reset (with machine.reset()) once it gets to this stage of being unable to receive, but once it boots back up and continues the operation it is still unable to receive anything. The only way get it to start working is to physically disconnect and reconnect the power. The issue occurs very unpredictably; sometimes the system works for a long time before it happens, other times its quite quick.
I am unsure if a soft reset will work, because Im not sure how to get the program running again afterwards.
The issue is not the device hardware because I tied using a different LoPy4 but the issue persisted.
Im using different channels to transmit and receive for other parts of the system, therefore every time the device switches between transmitting and receiving for the various different functions it recalls the LoRa() constructor and defines all the parameters (including the power mode as always on). Additionally, the socket is opened and closed for every transmission or reception. (Maybe this has an effect?)
Glad you (sort of) fixed your issue. Ill ask internally about this. I dont see why it should fail with both Wifi + Lora running.
Ok so I never figured out why it happens but the problem occurs when you remain connected to Wi-Fi and continue to perform LoRa communication. The workaround for this was to disconnect Wi-Fi before doing LoRa comms, and then reconnecting to do the Internet stuff.
Ok, so I've discovered that the issue doesnt occur if I remove all of the code regarding connecting to Wi-Fi and the sockets for communicating with the database, meaning that all thats running is the LoRa comms between the devices. This was confirmed by allowing the cleaned up (no Wi-FI or DB) system to run for over 500 iterations. The issue usually presents itself from anywhere between 5 to 100 iterations. If I simply add back the code for connecting to Wi-Fi, then continue with the rest of the program (not doing anything with the internet connection like opening INET sockets) the issue occurs, whereby eventually Node A stops being able to receive any LoRa messages. I have the following code in boot.py for connecting to Wi-Fi, and It is this code that seems to be causing the issue, but I havent yet discovered how to solve or isolate it further.
import machine from network import WLAN wifi_pwd = wifi_ssid = wlan = WLAN() if machine.reset_cause() != machine.SOFT_RESET: wlan.init(mode=WLAN.STA) wlan.antenna(WLAN.INT_ANT) wlan.ifconfig(config=('192.168.0.144', '255.255.255.0', '192.168.0.1', '192.168.0.1')) if not wlan.isconnected(): wlan.connect(wifi_ssid,auth=(WLAN.WPA2,wifi_pwd)) while not wlan.isconnected(): machine.idle() # save power while waiting print("Connected to Wifi\n") print(wlan.ifconfig()) machine.main('main0.py')
The following code comes from the relevant parts of Node A.
_BANDWIDTH = LoRa.BW_250KHZ _SF = 7 _RSSiTHRES = -100 _TXPOWER = 14 _PR = 8 _CR = LoRa.CODING_4_8 _PM = LoRa.ALWAYS_ON _CH1 = 867875000 _CH2 = 868875000 while (1): # RF1 try : # TRANSMIT TRIGGER lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, sf=_SF, frequency=_CH1, bandwidth=_BANDWIDTH, preamble=_PR, coding_rate=_CR, tx_iq=False, rx_iq=True, public=False, power_mode=_PM,tx_power = _TXPOWER) try: lora_sock = usocket.socket(usocket.AF_LORA, usocket.SOCK_RAW) except Exception as e: print(e) lora_sock.settimeout(10) msg = "BS2RF1" pkg = struct.pack(_LATERAL_DL_FORMAT % len(msg),RF1,DEVICE_ID,len(msg),1,measCount, msg) while(lora.ischannel_free(_RSSiTHRES) == False): print("__________________Channel is busy____________________") lora_sock.send(pkg) print('SENT RF1 TRIGGER') pycom.rgbled(0x0F0F00) # RECEIVE CONFIRMATION lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, sf=_SF, frequency=_CH1, bandwidth=_BANDWIDTH, preamble=_PR, coding_rate=_CR, tx_iq=False, rx_iq=True, public=False, power_mode=_PM,tx_power = _TXPOWER) try: lora_sock = usocket.socket(usocket.AF_LORA, usocket.SOCK_RAW) except Exception as e: print(e) lora_sock.settimeout(TT) while (True): received_pkg = lora_sock.recv(64) if (len(received_pkg) > 2): if (received_pkg == DEVICE_ID) and (received_pkg == RF1): received_msg_len = received_pkg dest, src, _,cd, rx_msg = struct.unpack(_LATERAL_UL_FORMAT % received_msg_len, received_pkg) if ((received_pkg == 3) or (rx_msg == b'RF12BS')): print(src,' sends: ',rx_msg) break time.sleep(0.02) print("Done receiving RF1 CONFIRMATION") pycom.rgbled(0x00FFFF) time.sleep(0.1) break except usocket.timeout as err: print("----------------timed out rf1 reception")
The Node is able to transmit the trigger (as I can see that it gets received on Node B), but when trying to receive it doesnt detect any messages, and therefore gets stuck in an infinite loop. Ive tried breaking it from the loop once it gets stuck and continuing with the other TX and RX operations but its meets the same issue of not being able to receive.
@jokr2 I think you'll have to share the relevant parts of your code so that other people are able to reproduce your issue.