Table of content HideShow

This tutorial is about programming the M5Stack Atom using MicroPython to transfer data to a LoRaWAN gateway. The gateway is then connected to the "The Things Network" (TTN) to process the data.

Before you start reading, check out the video of this setup!

The M5Stack ATOM Matrix is the most compact development board in the M5Stack development kit series with a size of only 24 x 24 x 14 mm and a weight of 14 g.

It has:

6 GPIO (Dupont Pins)

GROVE/4Pin PH2.0 interface

5 x 5 RGB LED matrix panel

Infra-red LED

1 programmable button (under the RGB matrix)

1 reset button

MPU6886 Inertial Sensor

Inside the nice ABS case, an ESP32-PICO-D4 is located, which comes with Wi-Fi and Bluetooth technologies and has 4MB of integrated SPI flash memory. The ESP32 runs at 240 MHz and has 520 KB SRAM. The on-board also provides a USB interface (Type-C), which enables rapid programming, code uploading, and execution. One M2 screw hole is located on the back for mounting the board (see Fig. 1). As usual, the M5Stack can be programmed using the Arduino IDE, UI-FLOW or MicroPython. In this article, I will use MicroPython.

Fig. 1: M5Stack ATOM Matrix

Hardware and Software

The following hardware and software will be used in the following sections:

LoRaWAN

LoRa is a wireless data communication technology that allows you to transmit data over a very long range (more than 10 km in rural areas) with low power consumption. It uses license-free sub gigahertz radio frequency bands such as 169 MHz, 433 MHz, 868 MHz for Europe and 915 MHz for North America. LoRa defines the lower physical layer, which means that the upper network layers are lacking, and it is only possible to send data between nodes. LoRaWAN is one of several protocols that was developed to define the upper layers of the network. Therefore, LoRaWAN defines the communication protocol and system architecture for the network, while the LoRa physical layer enables the long-range communication link.

In this tutorial, the M5Stack is programmed to be a LoRaWAN node, which connects to a LoRaWAN gateway and sends payloads to the TTN.

DIY Project

Connecting the M5Stack Atom Matrix to the TTN

Most of the steps to deploy the code on the ESP32 are described in other articles on this blog. Therefore, I will refer to them to keep this article focused.

Connect the M5Stack Atom Matrix to the SX127x board as follows:

M5Stack Atom LoRa V2.0 3V3 VCC GND GND G22 NSS G19 MOSI G23 MISO G33 CLK G21 RESET G25 DIO0

Flash MicroPython on the ESP32 following this tutorial: Tutorial: Getting Started with MicroPython on ESP32, M5Stack and ESP8266 Clone the repository: git clone -b LoRaWAN https://github.com/lemariva/uPyLoRaWAN.git Follow this tutorial Tutorial: ESP32 running MicroPython sends data over LoRaWAN to create an application on TTN. Rename the file tnn_config.sample.py to ttn_config.py following the instructions described in the tutorial from step 3 to add the node to the TTN. If everything works as expected you will see something like Fig. 2

Fig. 2: Getting data from the Node

Decode data from the received payload

You can decode the received payload using a custom function or Cayenne LPP. This can be done under the section Payload Formats (see Fig. 3). In this case, I programmed a simple custom function to get the variables and fill a JSON structure with them:

function Decoder(bytes, port) { var decoded = {}; if (port === 1) { decoded.epoch = ((bytes[4] << 32) + (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + (bytes[0])); decoded.temperature = ((bytes[9] << 8) + (bytes[8]))/100; } return decoded; }

After writing the Decoder and clicking on the save payload functions button, you can see the decoded payloads in the Application Data section as shown in Fig. 4.

Fig. 3: Custom decoder

Fig. 4: Application data decoded

Conclusion

This tutorial explains how to connect an M5Stack Atom to an SX127x and program it using MicroPython to send data to a LoRaWAN gateway. After receiving the payloads, the LG02 gateway forwards them to "The Things Network" and you can decode the data using a payload decoder, which is also described in this tutorial.

The next steps are to send data from the TTN to the LoRaWAN node (M5Stack), which works, but the data is still encrypted. Stay tuned for more updates!

To check the SPI signals and the DIO interruption, I used the WAVE2 (see Fig. 5). The oscilloscope is a nice gadget to have. To read a full review of this oscilloscope, check out this article: WAVE2: an Ensemble Yourself Oscilloscope (DSO)