Skip to content

Getting started with the PyCom LoPy

Alex Lennon edited this page Aug 16, 2018 · 11 revisions

Overview

This piece is going to take you through sourcing and setting up your PyCom LoPy to send and receive data via The Things Network.

Sourcing the LoPy

First off, find yourself a LoPy. The spec. for the device here. Essentially it's a newer flavour of the ESP8266, the ESP32, running Micropython with support for LoRa, WiFi and Bluetooth BLE.

At the time of writing there are a couple of devices available on eBay e.g. here (£20). There are notes on the LoPy site which seem to indicate you can damage your LoPy LoRa radio if you operate it without an antenna so you may wish to ensure you purchase this at the same time. Kit with antenna is available here although at £85 it is overpriced so hunt around.

Connecting things up

This page over at The Things Network is a good starting point.

Whilst there are a various pages out there telling you to connect up your antenna it's harder to find the details on where exactly you have to connect it (!). There are two connectors on the LoPy and the LoRa antenna connector is beside the RGB LED and the button. The other connector is for the WiFi antenna.

LoPy Antennae

Upgrading the Firmware

LoRa is a sub-GHz radio technology based on Direct Sequence Spread Spectrum (DSSS). It operates on different frequencies in different parts of the world. In the UK we're predominantly on 868MHz for The Things Network although other frequencies such as 433MHz may be supported in the future. For TTN road map see here.

LoPy firmware is built for the appropriate frequency in your location, which is why the firmware updater asks you for your location. As this is the TTN-Liv Wiki we're focussed on the EU and thus 868MHz.

Take a look at the Getting Started guide over at PyCom for details on how to upgrade the firmware.

Simple MicroPython tests

With your LoPy firmware updated, and with it connected to your PC via the USB serial cable, you can get started with MicroPython and run a few simple tests.

A good place to start is with this example which flashes the on board LED in different colours.

It's worth installing PyMakr IDE which simplifies running your MicroPython code on the LoPy.

Setting up your Things Network account

Next head over to The Things Network here and, if you haven't already got one, set up an account for yourself.

Once logged in, head to "console" (link in the top right) and "applications". Then add an application. Give it a name and a description. You'll see an "App Key" in the overview for your application. Take note of this.

Next register a device, give it a name, and make sure that you enter the device EUI correctly. With an incorrect device EUI you won't be able to connect...

To obtain the device EUI for your LoPy run up PyMakr or another serial terminal tool and enter.

import binascii

binascii.hexlify(network.LoRa().mac())

The value that gives you is the value you need to enter for the device EUI when you register the device.

Running the OTAA LoRa Example code

The code in the git repo here is based on the PyCom example code here

This shows how to make use of the OTAA join method to connect to a TTN LoRaWAN gateway.

The beautiful thing about this is that you don't have to care about the specifics of the gateway you are connecting to. You only care about the application you are connecting to, and wherever you place your LoRa IoT device it will connect to available gateways and "just work" (as long as you're operating on the appropriate frequency).

You'll see in the code there are two parameters you need to modify

app_eui = binascii.unhexlify('AD A4 DA E3 AC 12 67 6B'.replace(' ',''))

app_key = binascii.unhexlify('11 B0 28 2A 18 9B 75 B0 B4 D2 D8 C7 FA 38 54 8B'.replace(' ',''))

Replace these with the application EUI and application key from the overview page of your registered device.

Next run up the code and you should see that it successfully joins the LoRa network and publishes a message! You can see activations and upstream messaging on the "data" tab of your application

Subscribing to Messages with MQtt

Over at DoES Liverpool we love MQtt and in particular using MQtt with node-red. So lets get ourselves setup to subscribe via MQtt to messages that our LoPy is publishing to TTN.

Assuming that you are running some flavour of Linux, install the appropriate mosquitto-clients package for your distribution.

This page over at TTN gives an overview of how to get going.

This is the magic incantation to subscribe

mosquitto_sub -h <Region>.thethings.network:1883 -d -t 'my-app-id/devices/my-dev-id/up'

Note that my-app-id is the name of your application and my-dev-id is the name of your registered device. Alternatively you can use the MQQtt wildcard '+' character e.g.

mosquitto_sub -h <Region>.thethings.network:1883 -d -t 'my-app-id/devices/+/up'

With this running, whenever your device publishes a message you'll see it appear in your subscription!

References

For some general background on IoT, ESP8266 & Arduino see here