MicroPython resets to prompt--no error, no warning offered



  • I'm seeing MicroPython crashing...it's fine when it's alone on LoRa, but when it receives a packet it bounces straight out to the init prompt. The code below has two units sending each other location info, and each one will run for hours on its own. However, they promptly crash when they hear from the other one. The diagnostic prints below seem to narrow it down to...the break statement? I'm pretty new to any Python, Micro or not, so sorry if it's a rookie error.

    import machine
    import math
    import network
    from network import LoRa
    import socket
    import os
    import pycom
    import time
    import utime
    import gc
    import ujson
    from machine import RTC
    from machine import SD
    from L76GLNSV4 import L76GNSS
    #from L76GNSS import L76GNSS
    from pytrack import Pytrack
    
    time.sleep(2)
    gc.enable()
    pycom.heartbeat(False)
    pycom.rgbled(0x0f0300)
    
    rtc = machine.RTC()
    py = Pytrack()
    l76 = L76GNSS(py, timeout=30)
    #print("Trying GPS time")
    gpsTime = l76.getUTCDateTimeTuple()
    #print("Waiting for GPS time")
    while gpsTime == None:
        gpsTime = l76.getUTCDateTimeTuple()
    #print("Refining GPS time")
    gpsTime = l76.getUTCDateTimeTuple()
    #print("Setting GPS time")
    rtc.init(gpsTime)
    #print(l76.getUTCDateTimeTuple())
    pycom.rgbled(0x00000f)
    lora = LoRa(mode=LoRa.LORA, region=LoRa.US915)
    #lora = LoRa(mode=LoRa.LORA,region=LoRa.US915,frequency=915000000,tx_power=20,bandwidth=LoRa.BW_500KHZ,sf=12,preamble=8,coding_rate=LoRa.CODING_4_8,power_mode=LoRa.ALWAYS_ON, tx_iq=False, rx_iq=False, adr=False, public=True, tx_retries=1)
    #print('LoRa active')
    # create a raw LoRa socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    #print('Socket set')
    
    locLoc = {}
    remLoc = {'a': 0, 'o': 0}
    s.settimeout(1)
    #print('Timeout set')
    
    while (True):
        # Get our location
        coord = l76.coordinates()
        #print('Got coords')
        # send to other unit
        locLoc["a"] = coord[0]
        locLoc["o"] = coord[1]
        if type(locLoc["a"]) != 'float':
            #coord1 = (0,0)
            locLoc = {'a': 0, 'o': 0}
        enc = ujson.dumps(locLoc)
        gc.collect()
        #print(enc)
        s.setblocking(True)
        #print('Ready to send')
        pycom.rgbled(0x0f0000)
        s.send(enc)
        pycom.rgbled(0x000000)
        #print('Location sent')
        # See if we get an answer
        s.setblocking(False)
        #print('Blocking off')
        pycom.rgbled(0x000f00)
        print('LED green')
        coord2 = s.recv(64)
        print('Stats coming...')
        lora.stats()
        print('Message received: {}'.format(coord2))
        try:
            print("Trying to decode")
            remLoc = ujson.loads(coord2)
            print("Restoring to tuple")
            coord2 = (remLoc["a"],remLoc["o"])
            if type(remLoc["a"]) != 'float':
                remLoc = {'a': 0, 'o': 0}
            print("Success, I think...")
            break
        except ValueError:
            remLoc = {'a': 0, 'o': 0}
        print('Message processed')
    
        pycom.rgbled(0x000000)
        time.sleep(1)
        print("{} - {} - {}".format(coord, coord2, math.atan2((locLoc["o"]-remLoc["o"])*math.cos(locLoc["a"]),remLoc["a"]-locLoc["a"])*180/3.1415926535))
    

    And the result:

    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10828, -88.28986) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10836, -88.28994) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10838, -88.28993) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10838, -88.28994) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10839, -88.28994) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b''
    Trying to decode
    Message processed
    (42.10839, -88.28994) - b'' - 0.0
    LED green
    Stats coming...
    Message received: b'{"a": 0, "o": 0}'
    Trying to decode
    Restoring to tuple
    Success, I think...
    Pycom MicroPython 1.18.2.r3 [v1.8.6-849-a1641ca] on 2019-02-28; LoPy4 with ESP32
    Type "help()" for more information.
    

    The successful iterations are when it was the only kid on the LoRa block. The crash occurs when I activate the other module, which is running identical code. I guess I'm doing something so outrageous it can't even dignify it with an error message, but the only logical statement between the last print and the crash is "break." Again, I'm new to Python in general as well as to PyCom, so forgive me if it's obvious to everyone else.



  • @crumble Okay, I'll give that a try. I saw it in an example of exception trapping, and took it to be what would get me out of the try statement, like a C case statement. Apparently I misinterpreted the example code.



  • @aliubi

    Yes, break will stop your while loop.

    your code receives messages in a loop until it receives a message that can be parsed by json and contains an "a" and "o" element.

    checking a for type float will not work the way you like. numbers without a decimal point will be parsed to int and not to float. If you send 0.0 instead of 0, it will be parsed to type float


Log in to reply
 

Pycom on Twitter