Embedded MicroPython.....................
Tim Armstrong’s new series starts by constructing a wireless “Thing” on that “Internet”, using “LoRa”, “MicroPython”, “TheThingsNetwork” and quotes.
Tim Armstrong has lost his mind constructing a wireless “Thing” on the internet, using LoRa, MicroPython and TheThingsNetwork. What is he on about?
Every day, new IoT devices are hitting the market, from home assistants to Internet-connected toasters, but there’s always something missing; Either you can’t find the product that meets your needs, or when you find something it’s from a manufacturer that has been plagued by poor security or privacy issues. Sometimes if you want something done right, you just have to do it yourself. Which brings us on to the subject of this tutorial series: creating an Internet connected “Thing” (that won’t DDoS, or get DDoSed by your toaster).
The goal here is to explore the practical side of making a simple IoT device, and not get too bogged down in making something specific, because that tends to make the electronics more complex (not ideal for an introductory tutorial). So in part one of this tutorial we’re going to prototype something simple that monitors a single signal level and sends the result via LoRa to The Things Network and in part two we’ll take that data and plot it on a simple website.
Microcontrollers
There’s a cornucopia of hobbyist microcontrollers out there each with their own unique selling points and target audiences, but beginner kits essentially fall into three categories; Arduino-esque, PIC and MicroPython (the new kid on the block). Arduino is by far most ubiquitous on the market at the moment, with good documentation and opensource examples for nearly every use-case you can think of.
There’s only one problem. C (even the Arduino flavour) is still C. It’s an overly complex programming language and unless you have the time to learn it properly it’s like convincing a toddler not to draw on every surface they can reach. The same holds for PIC. If you want anything other than Assembly it’s C. This is the unique selling point of MicroPython, which is a subset of Python 3. Unlike C, Python is a high-level, memory managed, beginner friendly language that enables you to focus on what you want to do, and takes care of the difficult stuff for you.
Getting online
There are a lot of options for getting online, from Ethernet to Wi-Fi to Bluetooth to LTE/GSM (mobile/cellular Internet), but in this article we’re going to focus on something that’s a bit more exciting: LoRa.
LoRa is a long-range wireless standard where the focus of
its design was exclusively on IoT. It uses the license-free (depending on region) former terrestrial television frequency bands of 868 MHz to 928MHz. The standard is split into two specifications: LoRaMAC and LoRaWAN. LoRaMAC is intended for device-to-device communication. Its 10 to 20km-plus (line-of-sight) range is perfect for deploying a mass of sensors and control units throughout a factory, stadium or farm, where the data doesn’t necessarily need storing for extended periods.
LoRaWAN shares the same range as LoRaMAC, but instead of being from device to device, it’s intended for getting the data up and onto your servers as simply and securely as possible. As such the useful range is centred on a router-like device called a gateway (or condenser, depending on the manufacturer).
What’s more, LoRa is generally free. You can sometimes find commercial networks with better coverage, but if you have your own gateway or you’re lucky enough to be in range of someone else’s gateway then The Things Network gives you a free and easy platform to get your things on the internet. All you need is a LoRa transceiver that’s supported on the microcontroller platform you’re using.
Which brings us to which microcontroller we’re going to use. We strongly recommend MicroPython for aspirant hardware hackers. So for this tutorial we’ve selected the Pycom LoPy – a MicroPython-based microcontroller board with built-in LoRa, Wi-Fi, and BLE (Low Power Bluetooth).
The great thing about these boards is that they’re cheap, have a whole bunch of GPIO pins, and because they have both LoRa and Wi-Fi you can easily set one up as a LoRa gateway if you happen to be in a low (or no) signal area. The one downside is that because pycom is moving so fast as a company, rolling newer better firmware (bootloaders) for its products, the documentation on its site is sometimes lagging behind. So to mitigate this we’ve included the firmware and all the code and libraries used in this tutorial on this month’s DVD.
Powering up
Before we get started with the electronics we need to set up the development environment and update the firmware on the LoPy(s). First we need to enable serial port access to your user, as follows: $ sudo adduser <your username goes here> dialout
Because there’s a plugin (called PyMakr) for AtomText Editor that makes programming pycom boards incredibly simple, we’re going to be using Atom for this tutorial. If you don’t have Atom installed already you can, you can download it from www.atom.io and install it with:
$ sudo dpkg -i atom-amd64.deb
Once Atom is installed we need to install the PyMakr plugin. To do this launch Atom, go to Edit>Preferences>Install and search for PyMakr. Then install the official pycom PyMakr plugin.
Now, assuming you’ve got the items from the bill of materials, we can go ahead and get the board(s) powered up. Place each LoPy into its expansion board with the LED on the LoPy on the same side as the microUSB connector, plug the small end of the cable included in the Antenna Kit into the corresponding connector near the LED and screw the antenna onto the other side of the cable. Next, because we aren’t using a battery, go ahead and remove the jumpers from the BAT and CHG headers from the Expansion board.
Now we’re ready to turn it on. Simply plug in a microUSB cable between your computer and the expansion board.
To test the connectivity open Atom and we should see a new bar at the bottom of the window. It’ll say something like “Disconnected” on the left, and have four buttons on the right. Click More, then Get serial ports. This will list all the serial ports on your computer and automatically copy the most probable one from the list to the clipboard (such as /dev/ttyUSB0). Next we need to go to Settings>Global settings and paste the port in the device address field.
Now you can click the Connect button and you should see the familiar python shell prompt, >>> present in the terminal below it. If you don’t see this then you probably need to log out and log back in to apply the serial port access permissions to your current session. Once you see the python prompt, you can test it by issuing the traditional print(“Hello, World”) .
Now we can get started on the firmware update. Take a