LAB
All the code necessary for this Lab session is available in Poliformat/RSE: Recursos/Laboratorio/código practicas laboratorio, or here: https://bit.ly/codigoRSE2021
You can execute the code either in your computer or online:
In your computer:
$ sudo pip3 install paho-mqtt
Online:
Click here for "the documentation of the MQTT Paho API"
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. The simplest format is, for example: {"temperature":10}
So, for example, to send the value 25 to variable the_variable
of device mydevice_pm
the code should look like:
You'll get:
1.- Repite los pasos anteriores con tus datos de Ubidots (credenciales y nombre del dispositivo y variable usandos) y utilizando el codigo del "producer" (sipub.py
).
El codigo tiene que enviar cada vez un numero aleatorio entre 0 y 100. Te conviene utilizar un diccionario python y luego pasarlo a JSON.
Tienes que entregar el fichero python.
random.randint(0, 100)
json
. Ejemplo: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.
2.- Crea una dashboard a tu gusto con los datos que has enviado en el ejercicion anterior.
Haz una captura de pantalla de la dashboard resultato y adjuntala al documento a entregar.
3.- Utilizando como base el codigo del fichero lab4_ex3.py
, escribe un programa que lee los datos desde TTN, como en la sesion de laboratorio anterior, selecciona una variable (p.ej. la temperatura), y la publica en Ubidots.
Los parametros necesarios para leer datos desde TTN son:
Tienes que entregar el codigo python desarrollado.