Real-time data plotting of IoT sensor using Python

By on June 9, 2017

Web site:

Project Summary:

How to get and visualize sensor data of LoRa nodes using Zerynth, The Things Network and Matplotlib, a very popular Python plotting library.

Full Project:

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: a 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:

# LoraWAN sensor data Logging

import streams
from microchip.rn2483 import rn2483
from stm.hts221 import hts221


    rst = D16 # reset pin 
    # insert otaa credentials!
    appeui = "YOUAPPEUI"
    appkey =  "YOUAPPKEY"
    if not rn2483.init(SERIAL1, appeui, appkey, rst): # LoRa Click on slot A
        print("denied :(")
        raise Exception

    print("sending first message, res:")

    temp_hum = hts221.HTS221( I2C1,D31 ) # Temp Hum Click on slot C
    while True:
        temp, hum = temp_hum.get_temp_humidity()
        print('temp: ', temp, 'hum: ', hum)
        data = bytearray(4) 
        data[0:2] = bytearray([ int(temp) + 127, int((temp - int(temp)) * 100) ])
        data[2:4] = bytearray([ int(hum) + 127, int((hum - int(hum)) * 100) ])
        r = rn2483.tx_uncnf(data) # send data to TTN
except Exception as e:

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.

# Get and plot data from TTN Console using Python

import paho.mqtt.client as mqtt
import json
import base64


import matplotlib.pyplot as plt
#import DataPlot and RealtimePlot from the file
from plot_data import DataPlot, RealtimePlot

fig, axes = plt.subplots()
plt.title('Data from TTN console')

data = DataPlot()
dataPlotting= RealtimePlot(axes)


def bytes_to_decimal(i,d):
    xx = i - 127
    dec = (-d if xx < 0 else d)/100
    return xx + dec

def on_connect(client, userdata, flags, rc):

def on_message(client, userdata, msg):
    j_msg = json.loads(msg.payload.decode('utf-8'))
    dev_eui = j_msg['hardware_serial']

    tmp_hum = base64.b64decode(j_msg['payload_raw'])
    tmp = bytes_to_decimal(*tmp_hum[0:2])
    hum = bytes_to_decimal(*tmp_hum[2:4])

    # print data
    print('tmp:', tmp, ' hum:', hum)
    print('dev eui: ', dev_eui)

    # plot data
    global count
    data.add(count, tmp , hum)

# set paho.mqtt callback
ttn_client = mqtt.Client()
ttn_client.on_connect = on_connect
ttn_client.on_message = on_message
ttn_client.username_pw_set(APPID, PSW)
ttn_client.connect("", 1883, 60) #MQTT port over TLS

except KeyboardInterrupt:

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 “” that uses the Matplotlib library for this.

import time
import math
from collections import deque , defaultdict

import matplotlib.animation as animation
from matplotlib import pyplot as plt

import threading

from random import randint

from statistics import *

class DataPlot:
    def __init__(self, max_entries = 20):
        self.axis_x = deque(maxlen=max_entries)
        self.axis_y = deque(maxlen=max_entries)
        self.axis_y2 = deque(maxlen=max_entries)

        self.max_entries = max_entries


    def add(self, x, y,y2):


class RealtimePlot:
    def __init__(self, axes):
        self.axes = axes

        self.lineplot, = axes.plot([], [], "ro-")
        self.lineplot2, = axes.plot([], [], "go-")

    def plot(self, dataPlot):
        self.lineplot.set_data(dataPlot.axis_x, dataPlot.axis_y)
        self.lineplot2.set_data(dataPlot.axis_x, dataPlot.axis_y2)

        self.axes.set_xlim(min(dataPlot.axis_x), max(dataPlot.axis_x))
        ymin = min([min(dataPlot.axis_y), min(dataPlot.axis_y2)])-10
        ymax = max([max(dataPlot.axis_y), max(dataPlot.axis_y2)])+10

def main():
    fig, axes = plt.subplots()
    plt.title('Plotting Data')

    data = DataPlot();
    dataPlotting= RealtimePlot(axes)

        while True:
            data.add(count, 30 + 1/randint(1,5) , 35 + randint(1,5))

    except KeyboardInterrupt:
        print('nnKeyboard exception received. Exiting.')

if __name__ == "__main__": main()

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!

The free version of Zerynth Studio allows you to use all the features you need to prototype your LoRa solutions but you can program for free up to 5 units of each supported board. You can unlock this limit with the Zerynth Studio PRO version, that also includes 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

Find out more about Zerynth Studio PRO

About Luigi F. Cerfeda

Yet another Tony Stark wannabe

Leave a Reply

Your email address will not be published. Required fields are marked *