Lopy & Pytrack sending data by Lora KPN network



  • Hello everybody,

    I am using a Lopy in combination with a Pytrack.
    I want to send the data from the accelerometer by the KPN to mij cloud.

    But that do not work!

    First if I start up the module and upload the code there pops up a error message.
    It looks like this:
    File "main.py", line 66, in <module>
    FIle "/flash/lib/L76GNSS.py", line 56, in coordinates
    MemoryError: memory allocation failed, allocating 4009 bytes

    After a few times startup the module it will sometimes go to work, mostly not.
    But if it works then it sens one package with data over the KPN network.
    If I write a code with static data like "A1, B2, C3" there is no problem but when I want to send data from the accelerometer id doesn`t work.

    Does somebody know how I can solve both problems?



  • Hello everybody,
    I have tested the Lopy with the gc.collect function. It isn`t giving a error message anymore!
    Thanks for the help.



  • @han said in Lopy & Pytrack sending data by Lora KPN network:

    Where you set the empty list, you free a lot of memory. place the collect() after this statement:

    for i in range (200):
        mqData = {}
        gc.collect()
        #don't know if you have to wait for the gc at this point
        mqData["data"]["loc"]["lat"] = locTuple[0]
        mqData["data"]["loc"]["lon"] = locTuple[1]
        enc = ujson.dumps(mqData)
        gc.collect()
        s.send(enc)
    #end of loop
    

    a second good place may be between the json encoding and the send, so you clean up the json part which may have created a lot of temporary objects.

    If this will not help, build the json string by your own. This may not be as convenient as the dump of the data structure, but it will have a much less memory footprint. If this is still too much, you have to preallocate a buffer as early as possible and override only the variable part in it. This will be really unconvenient but it will do the trick. If you do not need json on the receiving machine, you can send the data in a binary format.



  • @crumble thanks for tip help.
    I am pretty new with Micropython. Where is de exactly place where I should place the gc.collect()?

    gc.collect()
    for i in range (200):
    mqData = {}

    mqData["data"]["loc"]["lat"] = locTuple[0]
    mqData["data"]["loc"]["lon"] = locTuple[1]
    enc = ujson.dumps(mqData)

    s.send(enc)
    gc.collect()?



  • @han said in Lopy & Pytrack sending data by Lora KPN network:

    your LoPy has around 30KByte of free RAM *). Your loop creates a lot of objects, so the 30KB are filled and fragmented very fast. Call gc.collect() at the start of the loop and before enc. this may help.

    There will be two other solutions.

    1. Building the json string manualy will use less memory/fragmentation
    2. Buying a LoPy 4 with plenty of RAM

    *) Sometimes the LoPy has up to 60KB free. But if you calculate with this number, your program will crash when the LoPy has only 30KB free RAM after reboot.



  • @han
    200 * something maybe it is too big...

    but above error message point to line 56 which is brake line.
    Go to device by ftp and look like file looks like - maybe it is not synchronized properly..



  • Is it possible that my message is to big?

    Now i am using this part
    for i in range (200):
    mqData = {}
    mqData["data"] = {}
    mqData["data"]["mov"] = {}
    mqData["data"]["mov"]["acc"] = {}
    accTuple = li.acceleration()
    mqData["data"]["mov"]["acc"]["x"] = accTuple[0]
    mqData["data"]["mov"]["acc"]["y"] = accTuple[1]
    mqData["data"]["mov"]["acc"]["z"] = accTuple[2]
    mqData["data"]["mov"]["rol"] = li.roll()
    mqData["data"]["mov"]["pit"] = li.pitch()
    mqData["data"]["loc"] = {}
    locTuple = l76.coordinates()
    time.sleep(0.5)
    mqData["data"]["loc"]["lat"] = locTuple[0]
    mqData["data"]["loc"]["lon"] = locTuple[1]
    enc = ujson.dumps(mqData)



  • @livius It looks like I have the newest version of the L76GNSS library




 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter