memory problems LoPy Pybytes

  • Hello,

    I am already using a LoPy on the Pybytes platform via LoRa and that is still working well.
    In combination with the DeepSleep shield it works already for over a year.
    Now I want to add an second LoPy with the same code (but with some necessary updates because of the newer software versions).

    But then the message appears that the

    memory allocation failed, allocating 136 bytes

    Another error is giving by rule 13 in the In this rule is reffered to the file.

    The details of the LoPy are

    >>> os.uname()
    (sysname='LoPy', nodename='LoPy', release='1.20.2.rc10', version='v1.11-a159dee on 2020-06-26', machine='LoPy with ESP32', lorawan='1.0.2', pybytes='1.5.1')

    I have read that this is a frequent problem with the first version of the LoPy, but my other LoPy works well!

    Could somebody help me to fix this issue? I read lot over .mpy files but I do not know how the create that.
    Let me know.
    Best regards, Han

    the complete code is

    #!/usr/bin/env python
    # Copyright (c) 2019, Pycom Limited.
    # This software is licensed under the GNU GPL version 3 or any
    # later version, with permitted additional terms. For more information
    # see the Pycom Licence v1.0 document supplied with this file, or
    # available at
        OTAA Node example  as per LoRaWAN EU868 regional specification
        - compatible with the LoPy Nano Gateway and all other LoraWAN gateways
        - tested works with a LoRaServer and TTN servers
    from network import LoRa
    import socket
    import binascii
    import struct
    import time
    import pycom
    import _thread
    from time import sleep
    import uos
    import ujson
    from deepsleep import DeepSleep
    import deepsleep
    from pysense import Pysense
    from SI7006A20 import SI7006A20
    from LTR329ALS01 import LTR329ALS01
    from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE
    from deepsleep import DeepSleep
    py = Pysense()
    mpp = MPL3115A2(py,mode=PRESSURE)
    si = SI7006A20(py)
    lt = LTR329ALS01(py)
    ds = DeepSleep()
    # get the wake reason and the value of the pins during wake up
    wake_s = ds.get_wake_status()
    if wake_s['wake'] == deepsleep.PIN_WAKE:
        print("Pin wake up")
    elif wake_s['wake'] == deepsleep.TIMER_WAKE:
        print("Timer wake up")
    else:  # deepsleep.POWER_ON_WAKE:
        print("Power ON reset")
    LORA_CHANNEL = 0 # zero = random
    LORA_NODE_DR = 4
        utility function to setup the lora channels
    def prepare_channels(lora, channel, data_rate):
        EU868_FREQUENCIES = [
            { "chan": 1, "fq": "868100000" },
            { "chan": 2, "fq": "868300000" },
            { "chan": 3, "fq": "868500000" },
            { "chan": 4, "fq": "867100000" },
            { "chan": 5, "fq": "867300000" },
            { "chan": 6, "fq": "867500000" },
            { "chan": 7, "fq": "867700000" },
            { "chan": 8, "fq": "867900000" },
        if not channel in range(0, 9):
            raise RuntimeError("channels should be in 0-8 for EU868")
        if channel == 0:
            import  uos
            channel = (struct.unpack('B',uos.urandom(1))[0] % 7) + 1
        upstream = (item for item in EU868_FREQUENCIES if item["chan"] == channel).__next__()
        # set the 3 default channels to the same frequency
        lora.add_channel(0, frequency=int(upstream.get('fq')), dr_min=0, dr_max=5)
        lora.add_channel(1, frequency=int(upstream.get('fq')), dr_min=0, dr_max=5)
        lora.add_channel(2, frequency=int(upstream.get('fq')), dr_min=0, dr_max=5)
        for i in range(3, 16):
        return lora
        call back for handling RX packets
    def lora_cb(lora):
        events =
        if events & LoRa.RX_PACKET_EVENT:
            if lora_socket is not None:
                frame, port = lora_socket.recvfrom(512) # longuest frame is +-220
                print(port, frame)
        if events & LoRa.TX_PACKET_EVENT:
            print("tx_time_on_air: {} ms @dr {}", lora.stats().tx_time_on_air, lora.stats().sftx)
        Main operations: this is sample code for LoRaWAN on EU868
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868, device_class=LoRa.CLASS_C)
    # create an OTA authentication params
    dev_eui = binascii.unhexlify('1234567896')
    app_key = binascii.unhexlify('123456789') 
    nwk_key = binascii.unhexlify('123456789qwertryuiop')
    prepare_channels(lora, 1, LORA_NODE_DR)
    # join a network using OTAA
    lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_key, nwk_key), timeout=0,  dr=LORA_NODE_DR) # DR is 2 in v1.1rb but 0 worked for ne
    # wait until the module has joined the network
    print('Over the air network activation ... ', end='')
    while not lora.has_joined():
        print('.', end='')
    # create a LoRa socket
    lora_socket = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    # set the LoRaWAN data rate
    lora_socket.setsockopt(socket.SOL_LORA, socket.SO_DR, LORA_NODE_DR)
    # msg are confirmed at the FMS level
    lora_socket.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, 0)
    # make the socket non blocking y default
    lora.callback(trigger=( LoRa.RX_PACKET_EVENT |
                            LoRa.TX_PACKET_EVENT |
                            LoRa.TX_FAILED_EVENT  ), handler=lora_cb)
    time.sleep(4) # this timer is important and caused me some trouble ...
    for i in range(0, 1000):
       def send_sensor_data():
           while (pybytes):
               pybytes.send_signal(1, si.humidity())
               pybytes.send_signal(2, si.temperature())
               pybytes.send_signal(3, lt.light())
               pybytes.send_signal(4, mpp.pressure())
    _thread.start_new_thread(send_sensor_data, ())
    #ds.enable_pullups('P17')  # can also do ds.enable_pullups(['P17', 'P18'])
    #ds.enable_wake_on_fall('P17') # can also do ds.enable_wake_on_fall(['P17', 'P18'])
    #ds.go_to_sleep(3600)  # go to sleep for 60 seconds

  • Hi,
    As you see, the Lopy1 has 4MB of flash memory. All our newer products sport larger Flash memory as the firmware size is ever increasing with new features. The problem is that the files you are trying to upload will fill up the Flash to its maximum. You can either install one of the older (smaller) firmwares using the firmware updater tool, or do something with the mpy files. That is a pretty advanced procedure where you build your own firmware, there is some discussion around it in multiple places, I link one here:

  • So I checked the memory of the LoPy and this is given by the terminal:

    As I do gc collect and then mem_free there pops up 13712. But the flash and memory status keeps the same size.

Pycom on Twitter