appliot2022
For the first experiments we 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 exercise we will use the one offered by HiveMQ (broker.hivemq.com). Fill in the data as indicated below:
Then click on ADVANCED
, and add the topic test/#
:
Now click on BACK
, and then on CONNECT
. You will start seeing something like this:
Where all this data coming from?
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!! (THis is called many-to-many communications!)
Quick tests:
a. What happens if you type spain/valencia/upv
instead?
b. How would you build a messaging application with a group of friends?
You will now read the values of two LoRaWAN sensors that are periodically sending their data to the TTN Network Server from the GRC lab.
You have to use the MQTT Explore with the following parameters:
All the code necessary for this Lab session is available here . You can execute it either in your computer or online.
executing online. Create an account in https://repl.it
executing in your computer. You must have python3 and the paho-mqtt
library installed ($ sudo pip3 install paho-mqtt
)
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/#
. Let's give it a try.
File: sipub.py
cointains the code of a simple python producer. This code connects to a public broker and periodically publishes random values to topic "PMtest/rndvalue"
What should be modified in sisub.py
so that it can receive the data sent by sipub.py
?
Repeat the previous example in which, using the client http://mqtt-explorer.com we read data from TTN. In this case it is enough to print all the JSON that arrives.
The necessary parameters are:
What should be modified in sisub.py
so that it can receive the data from TTN?
An MQTT broker can be executed as a Docker container. This fact provides flexibility when deploying these services, especially in edge devices, like Raspberry Pi or similar.
Searching for MQTT, you'll get:
The first one, eclipse-mosquitto, is the official image for the Mosquitto MQTT broker, probably the most widely used MQTT broker.
The third one, efrecon/mqtt-client is also a helpful image that allows having a handy client to test deployments.
The docker-compose.yml
file below indicates how to start up an MQTT broker locally:
Executing docker compose up
you will get something like:
giving:
this is obtained with a ./mosquitto/config/mosquitto.conf
file as simple as this:
Try using this broker using "MQTT explorer" as a subscriber and sipub.py as a publisher.
In the shared folder you can find examples of a simple publisher and a subscriber written in micropython for the LoPys.
For example, the code of a subscriber would be this: