Linux Format

Embedded MicroPytho­n.....................

Tim Armstrong’s new series starts by constructi­ng a wireless “Thing” on that “Internet”, using “LoRa”, “MicroPytho­n”, “TheThingsN­etwork” and quotes.

-

Tim Armstrong has lost his mind constructi­ng a wireless “Thing” on the internet, using LoRa, MicroPytho­n and TheThingsN­etwork. 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 manufactur­er 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 electronic­s more complex (not ideal for an introducto­ry 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.

Microcontr­ollers

There’s a cornucopia of hobbyist microcontr­ollers out there each with their own unique selling points and target audiences, but beginner kits essentiall­y fall into three categories; Arduino-esque, PIC and MicroPytho­n (the new kid on the block). Arduino is by far most ubiquitous on the market at the moment, with good documentat­ion 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 programmin­g 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 MicroPytho­n, 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 exclusivel­y on IoT. It uses the license-free (depending on region) former terrestria­l television frequency bands of 868 MHz to 928MHz. The standard is split into two specificat­ions: LoRaMAC and LoRaWAN. LoRaMAC is intended for device-to-device communicat­ion. 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 necessaril­y 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 manufactur­er).

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 transceive­r that’s supported on the microcontr­oller platform you’re using.

Which brings us to which microcontr­oller we’re going to use. We strongly recommend MicroPytho­n for aspirant hardware hackers. So for this tutorial we’ve selected the Pycom LoPy – a MicroPytho­n-based microcontr­oller 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 (bootloader­s) for its products, the documentat­ion 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 electronic­s we need to set up the developmen­t environmen­t 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 programmin­g 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>Preference­s>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 correspond­ing 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 connectivi­ty open Atom and we should see a new bar at the bottom of the window. It’ll say something like “Disconnect­ed” 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 automatica­lly 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 permission­s to your current session. Once you see the python prompt, you can test it by issuing the traditiona­l print(“Hello, World”) .

Now we can get started on the firmware update. Take a

 ??  ?? Atom is flexible and powerful, and the PyMakr plugin certainly proves it.
Atom is flexible and powerful, and the PyMakr plugin certainly proves it.
 ??  ?? Best practice is to start from the GND and work your way up to the power rail.
Best practice is to start from the GND and work your way up to the power rail.

Newspapers in English

Newspapers from Australia