NOTE: The Zerynth Ecosystem has been updated to Zerynth 2.1.0. The update removes the distinction between “Zerynth Studio” and “Zerynth Studio PRO”. You can read more about it here.

This article follows the tutorial about how to create a LoRa/LoraWAN network programmed in Python using Zerynth and connected to The Things Network.

Starting with the same setup of the previous project, we’ll see how to get and visualize sensor data of LoRa nodes using Zerynth, The Things Network and Matplotlib, a very popular Python plotting library.

You will see how to:

  • program a LoRa node in Python using Zerynth to get temperature and humidity data;
  • send data to The Things Network, going through a LoRa Gateway;
  • get data from The Things Network console via MQTT;
  • plot sensor data using Matplotlib.

Required Material

Essentially, we need the same configuration used for the previous project, plus a temperature and humidity sensor:

  • LoRa Gateway. In this tutorial we’ve used a Link Labs BS-8, an industrial-grade IoT/M2M gateway for use with LoRa networks. It’s capable of supporting thousands of endpoints with 8 simultaneous receive channels.
    Link Labs Gateway LoRa
  • LoRa end-node. In this tutorial we’ve used we’ve used Flip&Click as MCU board, a LoRa Click and a Temp&Hum Click, that carries ST’s HTS221 temperature and relative humidity sensor.Flip&Click + LoRa click + Temp&Hum Click
  • Zerynth Studio: our professional IDE that provides a platform for developing your Python or hybrid C/Python code and managing your boards. It includes a compiler, debugger and an editor, alongside tutorials and example projects for an easy learning experience.
  • TTN (The Things Network) Console: you need to create/register an account entering a username and an email address.

Programming the LoRa Node in Python to get sensor data

After the LoRa Gateway configuration and the LoRa node configuration steps are performed (see the previous project), we’ll be able to program the device to get data from the Temp&Hum Click and to send these values to The Things Network console.

In particular, we have to create a new project on Zerynth Studio and paste this code:

Edit the lines with the “appeui” and the “appkey” values that you can find in the Device Overview on the TTN Console.

Uplink the code to your board and you’re done!

Now click on the “Data” tab of the TTN Console and you can see your sensor data sent by the LoRa node!

As you can see, this script is very similar to the “ping” example used on the previous project. The only differences are related to the usage of the HTS221 temperature and humidity sensor carried by the Temp&Hum Click. Thanks to Zerynth, you need just a few lines of Python and you can get real data from a sensor and send it to a LoRa network!

Getting data from The Things Network console via MQTT

Now it’s time to get data from the TTN Console. To do this, we’ve prepared a very simple Python script that utilizes the Eclipse Paho MQTT Python client library, which implements versions 3.1 and 3.1.1 of the MQTT protocol.

Note that you have to run this script on your laptop, so you need Python (3.x) installed on your PC.

Real-time plotting of sensor data using Matplotlib

Once you have the data in your computer, you can do all sorts of things with it. In this case, we want to read these temperature and humidity values and plot them as a function of time.

As we love Python, we’ve prepared a script called “plot_data.py” that uses the Matplotlib library for this.

Note that you have to import this file in the previous script to work properly.

And here is what the plot looks like. It scrolls to the right as data keeps coming in.

Expand your LoRa network with Zerynth Studio PRO

The following list includes some of the LoRa key features:

  • Long range: 15 – 20 km;
  • Long battery life: in excess of ten years;
  • Millions of nodes!

Once you created a Zerynth Account in Zerynth Studio, you receive a free “Welcome” pack of 10 Zerynth VM Licenses to use on any of the supported boards. You can unlock this limit by buying more Zerynth VM licences that also include industrial-grade features like:

  • Selectable RTOS;
  • Over the Air update development;
  • Hardware-driven Secured Firmware burned on the device at industrial volumes;
  • …and much more!