<aside> đź—Ł You can execute the code either on your computer or online:

Programming MQTT

A simple subscriber

File sisub.py contains the code of a simple python subscriber. This code connects to a public broker and subscribes to topic $SYS/#.

https://replit.com/@pmanzoni/sisub#main.py

A simple producer

File sipub.py contains the code of a simple python producer. This code connects to a public broker and periodically publishes random values to the topic "PMtest/rndvalue"

https://replit.com/@pmanzoni/sipub#main.py

Yo can use “MQTT Explorer” to check if it works:

MQTT Explorer

<aside> đź—Ł How should you modify sisub.py to read data from this publisher?

</aside>

Getting data from TTN

The Things Network uses MQTT to publish device activations and messages, but also allows you to publish a message for a specific device in response.

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.

The essential data we need is the following:

Broker: eu1.cloud.thethings.network
Username: lopys2ttn@ttn
Password: NNSXS.A55Z2P4YCHH2RQ7ONQVXFCX2IPMPJQLXAPKQSWQ.A5AB4GALMW623GZMJEWNIVRQSMRMZF4CHDBTTEQYRAOFKBH35G2A
Topic: v3/+/devices/#

As a first example, we will modify the code of sisub.py to get the raw information from TTN; see file sisubttn1.pyin the repository.

https://replit.com/@pmanzoni/sisubttn1#main.py

Now we can slightly modify the code to get a specific piece of information, see file sisubttn2.pyin the repository.

https://replit.com/@pmanzoni/sisubttn2#main.py