Mapping The Things Network with a LoPy (2). Programming the LoPy

The most interesting part is definitely the LoRa node for which i use a LoPy. The software described here is based on the work of PiAir found here on GitHub and taylored to my needs.

Before going into the software a few lessons learned regarding the use of the LoPy:

  • Memory constraints.

It is not possible to just load any python code and libraries. It won’t fit in the memory of the device. A solution for this is to load cross compiled python modules (aka “Frozen bytecode).  The only disadvantage is that debugging is more time consuming. A ready to use cross compiler is provided by @robert-hh on his github pages (look for mpy-cross_pycom.exe if you’re on Windows).

Another memory constraint is RAM. Performing a so called “Garbage collection” frees up memory and could avoid hard to track errors. In my case i used to get a timeout error on sending a LoRa packet after some 50 packets (which are still being catched just in case). This behaviour was not seen again after doing a gc.collect() call every sending loop.

  • Take care not to assign pins twice

This may sound a no brainer but when using 4 ports (UART0 for terminal, LoRa module on SPI at pin p5, p6 and p7), I2C for the display on p8, p9 and UART1 for the GPS on pin p11, p12) an error is easily made.

  • Do not search for internet in STA mode

Normally one would try to login to the home WiFi in the module. This is nice when developing but out in the car/on the bike there is little chance that the WiFi could be found which would stall the boot process.

  • Uploading using ampy (adafruit)

How to upload code then? Having played around with Atom and its Pymakr plugin i got an “unable to upload” too often so a reliable serial uploader was searched. Adafruit provides “ampy“, a commandline uploader which never failed (provided you close any open Putty sessions to the same port). Ampy provides basic commands like “ls”, “put” etc and is invoked like: C:>ampy –port COM4 put

  • Prefer ABP over OTAA ?

Actually this still hasn’t been decided on. While driving around it might take a while for the tracker to be able to join the LoRaWAN network. Leaving from home this is no problem as i run my own gateway, but anywhere else this could take longer than the patience permits. ABP should have the node joined right away (did not try this mode yet) but requires storing the message counter. Anyway, follow the latest here

  • Use cold Reboot (soft restart will not restart the LoRa module)

Soft booting the LoPy (Ctrl-D in REPL terminal) will have you waiting for a LoRaWAN join forever. The thing just won’t transmit any packet at all. A cold restart will, but in my experience it may take a try or two to have it start the (probably due to this). Will ask around how to have this more predictable. Follow progress here.

The software can be found on my github pages. Instead of uploading the .py versions grab the .mpy files from the obj directory to save memory. Note that if both the .py and .mpy files are found it will still try to compile and waste memory.

Before uploading change the to your needs. (optional) WiFi settings, ports used and most important: the  APP_EUI  and APP_KEY as found in your The Things Network console when drilling down to the particular device you’ve registered.


Following steps will be performed after reboot:

  1. Led falshes RED while waiting for WiFi (if configured)
  2. Display initializes at all “0”
  3. Led flashes YELLOW while waiting for OTAA registration on the LoRa network. Depending on nearby gateway availability this may take a while
  4. Led flashes BLUE while waiting for GPS fix. Depending on the skyview this might take a while.
  5. Led flashes GREEN. Yes! You’re there. The location will be sent roughly every 10 seconds. While sending and waiting for a reply from the network the led will be PURPLE
  6. Received data will be shown on the display and the time should show the actual UTC time. During LoRa transmit/receive the time will stop for a few seconds.



This entry was posted in LoRa and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s