pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
196 stars 167 forks source link

LoPy L01 LoRaWAN blocking socket.send() hangs permanently #105

Open jmarcelino opened 6 years ago

jmarcelino commented 6 years ago

From https://forum.pycom.io/topic/2272/lopy-l01-lorawan-blocking-socket-send-hangs-permanently

Using firmware v1.10.2.b1 on LoPy L01 OEM module

Using the code shown below periodically the socket.send() function call hangs forever. The only way to recover is to enable the watchdog timer.

Code:

import pycom
import network
from network import LoRa
import socket
import machine
import time
import binascii

reset_cause = machine.reset_cause()

def select_subband(lora, subband):
    if (type(subband) is int):
        if ((subband<1) or (subband>8)):
            raise ValueError("subband out of range (1-8)")
    else:
        raise TypeError("subband must be 1-8")

    for channel in range(0, 72):
        lora.remove_channel(channel)

    channel_idx = 0
    for channel in range((subband-1)*8, ((subband-1)*8)+8):
        lora.add_channel(channel_idx, frequency=902300000+channel*200000, dr_min=1, dr_max=3)
        channel_idx += 1

# Initialize LoRa in LORAWAN mode.
print("LoRa init")
lora = LoRa(mode=LoRa.LORAWAN, device_class=LoRa.CLASS_A)

if reset_cause == machine.DEEPSLEEP_RESET:
    lora.nvram_restore()

if reset_cause == machine.DEEPSLEEP_RESET and lora.has_joined():
    print('Skipping LoRaWAN join, previously joined')
else:
    select_subband(lora, 1)

    # create an OTAA authentication parameters
    app_eui = binascii.unhexlify('00 00 00 00 00 00 00 01'.replace(' ',''))
    app_key = binascii.unhexlify('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01'.replace(' ',''))

    # join a network using OTAA (Over the Air Activation)
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

    print('Joining LoRa network')

    # wait until the module has joined the network
    while not lora.has_joined():
        time.sleep(2.5)

    print('Joined!!!')

# create a LoRa socket
print("LoRa socket setup")
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 1)
s.setblocking(False)

print("Init complete, running main loop")

# simulate time delay to detect sensors, etc
time.sleep(10)

while True:
    was_lora_tx_error = False
    try:
        s.setblocking(True)
        print('Trying to send...')
        s.send(bytes(21))
        print('info sent')
    except Exception as e:
        was_lora_tx_error = True
        if e.args[0] == 11:
            print('cannot send just yet, waiting...')
            time.sleep_ms(30)
        else:
            print('error: ')
            print(e.args[0])
            raise    # raise the exception again

    s.setblocking(False)

    if not was_lora_tx_error:
        print('Going to deep sleep')
        time.sleep_ms(10)
        lora.nvram_save()
        lora.power_mode(LoRa.SLEEP)
        machine.deepsleep(10000)
atoy40 commented 6 years ago

Exact same problem with exact same firmware, and a very simple test program (no deepsleep or other "features", just sending unconfirmed lorawan packet every minute). It hangs after around 100 to 400 packets in the send() function.