# Day 1 - exercise: containerised MQTT
###### tags: `semiotmic2022`
:::info
You can execute the code either in your computer or online:
* online. Create an account in https://repl.it

* or in your computer. You must have python3 installed and the `paho-mqtt` library:
```
$ sudo pip3 install paho-mqtt
```
The documentation of the MQTT Paho API is here: https://www.eclipse.org/paho/clients/python/docs/
:::
:::info
The code of this section is [here ](https://www.dropbox.com/sh/6zhgyfs9znmgeca/AADbIlVtpS50Kg5A2glqG1lka?dl=0)
If you are running Docker online: https://labs.play-with-docker.com/ you can upload files in the session terminal by dragging over it.
:::
## Programming MQTT (quick recap)
### A simple subscriber
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 see:
:::info
https://replit.com/@pmanzoni/sisub#main.py
:::
### A simple producer
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"`
Let's see:
:::info
https://replit.com/@pmanzoni/sipub#main.py
:::
To check whether this is working we can use the previous code `sisub.py` ... with a slight modification... which one?
## Getting data from TTN
[The Things Network uses MQTT](https://www.thethingsnetwork.org/docs/applications/mqtt/index.html) 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 basic 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.py`in the repository.
Let's see:
:::info
https://replit.com/@pmanzoni/sisubttn1-1#main.py
:::
The structure of the raw message is:
:::success
```
{
"end_device_ids": {
"device_id": "lopy4sense",
"application_ids": {
"application_id": "lopys2ttn"
},
"dev_eui": "70B3D5499269BFA7",
"join_eui": "70B3D57ED002AE7C",
"dev_addr": "260BEB29"
},
"correlation_ids": [
"as:up:01FTH1M913N60773814TZW1S6D",
"gs:conn:01FSM6BSTEH922M91YSYZ052CF",
"gs:up:host:01FSM6BSTNJCAP5P8WH9HHY076",
"gs:uplink:01FTH1M8TKM7GK5RTRZJVW7V5G",
"ns:uplink:01FTH1M8TMY1B5RJEQHPWW1PQC",
"rpc:/ttn.lorawan.v3.GsNs/HandleUplink:01FTH1M8TMT29SPDVWQKSFPN0P",
"rpc:/ttn.lorawan.v3.NsAs/HandleUplink:01FTH1M912NCJT97EZT256ZPTC"
],
"received_at": "2022-01-28T19:12:09.252340733Z",
"uplink_message": {
"session_key_id": "AX5STv5aJMlIpBDPZGGrOA==",
"f_port": 2,
"f_cnt": 19121,
"frm_payload": "QbhHJEIA//i/gAAA",
"decoded_payload": {
"humidity": 32.249969482421875,
"lux": -1,
"temperature": 23.03473663330078
},
"rx_metadata": [
{
"gateway_ids": {
"gateway_id": "rak-gtw-grc",
"eui": "B827EBFFFE336296"
},
...
```
:::
Now we can sligtly modify the code to get a specific piece of information, see file `sisubttn2.py`in the repository.
:::info
https://replit.com/@pmanzoni/sisubttn2-1#main.py
:::
To get sometthing like this:
```
Got these values >> temp=23.078 hum=-1.000 lux=32.235
Got these values >> temp=28.000 hum=-1.000 lux=34.714
```
:::danger
EXERCISE:
what if we want to "containerize" this app so that simply running as: `docker run -t mysubttn` we get the result above?
Which is the content of the Dockerfile?
:::