SEMINARIO
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:
Let's start with an easy one. Click on the DISCONNECT
button. Now add a subscription to the topic Spain/Valencia/UPV
, then CONNECT once again.
Now, (1) write the same identical text for the topic (i.e., Spain/Valencia/UPV
) in the topic
field of the Publish section, select the raw
option and write a text in the box below. The text can be whatever you want (e.g., Ciao!!
). When you are done click on the Publish
button.
You'll get something like the image below… plus all the messages written by all the other clients. With just one publish action you actually reached various devices!!
Quick tests:
spain/valencia/upv
instead?Let's use now MQTT Explore with the following parameters:
You will start seeing something like this:
We are currently reading two LoRaWAN sensors that are periodically sending their data to TTN from a remote lab… in Valencia.
There are many IoT devices available in the market. I have various LoPy4 with a PySense board or PyTrack board, for possible projects.
LoPys are based on MicroPython. MicroPython is a full Python compiler and runtime that runs on the bare-metal. You get an interactive prompt, called the REPL ("Read Evaluate Print Loop"), to execute commands immediately, along with the ability to run and import scripts from the built-in filesystem. The REPL has history, tab completion, auto-indent and paste mode.
More infos can be found here: https://docs.pycom.io/gettingstarted/
A very basic tool to connect to the LoPy's REPL is a Python tool called mpy-repl-tool.
other, more friendly tools are available like Atom or Microsoft’s Visual Studio Code. Instructions are available here: https://docs.pycom.io/gettingstarted/software/
An example with Atom:
The PySense offers various embedded sensors, namely:
Take a look at the code below:
You'll get something like:
A standard MicroPython project will have a lib folder for additional libraries, and two Python files: main.py and boot.py.
- lib
folder: For example, the extra sensor libraries for the Pytrack, Pysense and Pyscan are put in this folder.
- main.py
This script runs directly after boot.py and should contain the main code you wish to run on your device.
- boot.py
This is the first script that runs on your module when it turns on. This is often used to connect a module to a network without cluttering up the main.py file.
In this block we will review 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 big Network Server is Loriot.
A lot of useful information is available here.
The following provides some examples on how to use the TTN NS.
To start using TTn you need to create an account here: https://account.thethingsnetwork.org/register
Then, you will mmanage 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 this step we will use the device (the LoPy plus the PySense) registered in the step before to periodically send the sensed temperature, humidity and luminosity (lux).
Now, if we go in the "Live dta" 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 (
... = 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.