The code used in this Lab is in the github repository
To run the example in this Lab you'll need to install some software, basically Python3 and Docker…
Option 1:
You can use a virtual machine that you can download here
You have to use VirtualBox with the option: "Files / Import virtualized service". The VM has user "docker" with password "docker"
Option 2 (for the Docker part):
You can download and install Docker on multiple platforms. Refer to the following link: https://docs.docker.com/get-docker/ and choose the best installation path for you.
Option 3 (for the Docker part):
You can execute it online: https://labs.play-with-docker.com/
More details to come…
In the following:
Green boxes indicate questions/exercises
Blue boxes indicate useful information/indications
In this block we take a view from inside a LoRaWAN Network server, reviewing the process required to send sensor data using LoRaWAN, that is moving data from the sensor to the Network Server (NS). As network server we use The Things Networks. Another important Network Server is Loriot. Some useful information is available here.
Remember the overall LoRaWAN architecture:
To start using TTN you need to create an account here: https://account.thethingsnetwork.org/register. Then, you will manage your applications and devices via The Things Network Console.
and this is the main gateway
For example:
The Things Network supports the two LoRaWAN mechanisms to register devices: Over The Air Activation (OTAA) and Activation By Personalization (ABP).
Basically, select "End devices" and then click on:
Detailed instructions can be found here:
https://www.thethingsindustries.com/docs/devices/adding-devices/
In the following demo we will use a device (a LoPy4 plus a PySense board) registered as indicated in the step before to periodically send the sensed temperature, humidity and luminosity (lux).
The device "joins" the Application by:
The main code is something like this:
Now, if we go in the "Live data" section of the TTN Application, we will see something like this:
That is the data coming from the three devices that are using this application. If you click on any of the lines of the data, you'll get:
where you can find a lot of information regarding the sending of you LoRa message.
If you check the Payload field, you will see a sequence of bytes… and that is actually what we sent …
To see what we actually sent, check again the file above:
As you can see we are sending the values of temperature, humidity and luminosity (lux) "compressed" as a sequence of 4*3= 12 bytes (:arrow_right: ... = struct.pack(">fff",...
).
Now, to allow TTN to interpret these sequence of bytes we have to go the the section Payload Format and insert some specific code to interpret the input bytes.
TIME FOR A DEMO!
For the experiment you will use an MQTT client that can be used in any OS, it's called MQTT-explorer:
You have to install it from here: http://mqtt-explorer.com
There are various public MQTT brokers, for example:
In this session we will use the one offered by HiveMQ (broker.hivemq.com). Fill in the data as indicated below:
Then click on ADVANCED
, and add a topic:
if you want you can assing your own client ID. Now click on BACK
, and then on CONNECT
. You will start seeing something like this:
You can execute the code either in your computer or online:
paho-mqtt
library:The documentation of the MQTT Paho API is here: https://www.eclipse.org/paho/clients/python/docs/
File sisub.py
cointains the code of a simple python subscriber. This code connects to a public broker and subscribes to topic $SYS/#
.
$> python3 sisub.py
The same can be tested with the MQTT-Explorer. Try it.
File sipub.py
contains the code of a simple python producer. This code connects to a public broker and periodically publishes random values to topic "PMtest/rndvalue"
$> python3 sipub.py
Check if it is working using the MQTT-Explorer… how?
Check now with the previous code sisub.py
. How do you need to modify it?
The Things Network uses MQTT to publish device activations and messages and allows you to publish a message for a specific device in response.
You will now read the values of two LoRaWAN sensors that periodically send their data to the TTN Network Server from the GRC lab.
The basic data you need is the following:
Modify the code of sisub.py
to get the raw information from TTN.
You should get something like this:
OPTIONAL: Now you can slightly modify the code to get the actual data sent from the sensors; something like this:
In this block you will experiment about how MQTT can be used to send data to a cloud based platform. This procedure allows you to store your data in a cloud based repository and to analyze your data with software tools made available by the used platform. For these experiments we will use the Ubidots plaftorm.
You will have to first create your free account in the Ubidots platform here: https://ubidots.com/stem/
Then you have to add a Device (select first the "Devices" section in the top on the web page):
choose:
and add a name, like (use the name that you want!!):
You'll get:
Now click on the device name and you'll get to the variables creation section:
click on "Add Variable" and create a "Raw" type variable. Use the name that you want:
Now you have your web system prepared to receive the data
Now you will send data to our device using MQTT with Python.
Take a look first to the Ubidots MQTT API Reference: https://ubidots.com/docs/hw/?language=Python#mqtt
The name of the broker for educational users is "things.ubidots.com". To interact with it, you will need a TOKEN. To get yours click on “API Credentials” under your profile tab:
In my case I have:
To connect to the MQTT broker you'll have to use your Default Token
as the MQTT username, and None
as password.
The topic you have to use is /v1.6/devices/{LABEL_DEVICE}
where you have to replace your value for the API label {LABEL_DEVICE}
(e.g., mydevice_pm).
The data must be represented using JSON. So, for example, to send the value 25 to variable the_variable
of device mydevice_pm
the code should look like:
You'll get:
Repeat the above steps with your Ubidots data (credentials and name of the device and variable used) and using the "producer" code (sipub.py
).
The code has to send each time a random number between 0 and 100. You may want to use a python dictionary and then pass it to JSON.
random.randint(0, 100)
.json
class. Example:Ubidots allows to visualize the data in various graphical ways.
Go to the Dashboard section and add new widget associated with the variable of your device.
See the alternatives you have and how they can be configured.
Create a dashboard to your liking with the data you have sent in the previous exercise.
Using the code in ttn_ubidots.py
as a starting point, write a program that reads data from TTN, as in the previous block, selects a variable (e.g. temperature), and publishes it to Ubidots.
The TIG Stack is an acronym for a platform of open source tools built to make collection, storage, graphing, and alerting on time series data easy.
A time series is simply any set of values with a timestamp where time is a meaningful component of the data. The classic real world example of a time series is stock currency exchange price data.
Some widely used tools are:
In this Lab we will use the containers platform Docker.
You can use Docker:
We will use the following images:
InfluxDB is a time-series database compatible with SQL, so we can setup a database and a user easily. In a terminal execute the following:
This will keep InfluxDB executing in the background (i.e., detached: -d
). Now we connect to the CLI:
The first step consists in creating a database called "telegraf":
Next, we create a user (called “telegraf”) and grant it full access to the database:
Finally, we have to define a Retention Policy (RP). A Retention Policy is the part of InfluxDB’s data structure that describes for how long InfluxDB keeps data.
InfluxDB compares your local server’s timestamp to the timestamps on your data and deletes data that are older than the RP’s DURATION
. So:
Exit from the InfluxDB CLI:
We have to configure the Telegraf instance to read data from the TTN (The Things Network) MQTT broker.
We have to first create the configuration file telegraf.conf
in our working directory with the content below:
the values in [[inputs.mqtt_consumer]]
are our example values.
Then execute:
Check if the data is sent from Telegraf to InfluxDB, by re-entering in the InfluxDB container:
and then issuing an InfluxQL query using database 'telegraf':
> use telegraf
> select * from "TTN"
you should start seeing something like:
Exit from the InfluxDB CLI:
Before executing Grafana to visualize the data, we need to discover the IP address assigned to the InfluxDB container by Docker. Execute:
and look for a line that look something like this:
This means private IP address 172.17.0.2 was assigned to the container "influxdb". We'll use this value in a moment.
Execute Grafana:
Log into Grafana using a web browser:
the first time you will be asked to change the password (this step can be skipped).
You have to add a data source:
and then:
then select:
Fill in the fields:
(the IP address depends on the value obtained before)
and click on Save & Test
. If everything is fine you should see:
Now you have to create a dashboard and add graphs to it to visualize the data. Click on
then "+ Add new panel",
You have now to specify the data you want to plot, starting frorm "select_measurement":
you can actually choose among a lot of data "field", and on the right you have various option for the panel setting and visualization.
You can add as many variables as you want to the same Dashboard.
Give it a try!
In this Lab you'll have to do something similar to the previous one, but this time the data from the TTN have to be accessed using a Telegram bot. The final result should be something like this:
What you have to do first is create your own Bot. To generate an Access Token, you have to talk to the BotFather following a few simple steps as described here: https://core.telegram.org/bots#6-botfather
Basically, you need a Telegram account and the Telegram app installed in your smartphone or computer.
The "dialogue" will be something like this:
To program the Bot you can use, as a skeleton, the file ptelegram.py
. Fill in the places in ptelegram.py
where the text ___FILL_IN_HERE___
is found with the proper values.
You can execute your Bot either in your computer or in repl.it and test it using a smartphone with Telegram. The package https://github.com/python-telegram-bot/python-telegram-bot is necessary to running the Bot.
If you are using your computer, than you'll have to install it using:
If you are using repl.it
, you have to install it from the Packages menu:
and typing in the search field python-telegram-bot
and then on the + button
You should eventually get to something like this:
Check that the broker is working, something like this:
Remember that the parameters needed to read data from TTN are:
Modify the Bot so that it can return the temperature, humidity and brightness value based on the request.
You should get something like this: