# IoT Lab 2 ###### tags: `SRM2020` # Part A: Sending data to a cloud based platform (UBIDOTS) 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](https://ubidots.com/) plaftorm. ## Connecting to the Ubidots platform 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): ![](https://i.imgur.com/mnoRZAJ.png) choose: ![](https://i.imgur.com/NK04pBX.png =50x) and then ![](https://i.imgur.com/w0WCv8C.png =100x) and add a name, like (**the values in the figure are just examples, use the values that you want**): ![](https://i.imgur.com/Y0BoWua.png =200x) Now click on the device name and you'll get to the variables creation section: ![](https://i.imgur.com/TZZk8jC.png =300x) click on "Add Variable" and create a "Raw" type variable. Use the name that you want: ![](https://i.imgur.com/7egsbn9.png =200x) Now you have your web system prepared to receive the data of that variable for that device. ## Sending data to Ubidots Now you will send data to our device **using MQTT with Python**. 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: ![](https://i.imgur.com/StmHEVg.png) In my case I have: ![](https://i.imgur.com/pCmKyeQ.png) 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 `{LABEL_DEVICE}` with the ``API label`` of your device (e.g., `device1`). ![](https://i.imgur.com/jBwJAMB.png =400x) The data must be represented using JSON, like this: * Single value: {"temperature": 27} * Multiple values: {"temperature": 27, "humidity": 55, ... ,"pressure": 78} So, for example, to send the value 25 to variable `variable1` of device ``device1`` the code should look like: ```python= ... msg_to_be_sent = '{"variable1":25}' client.publish("/v1.6/devices/device1", payload=msg_to_be_sent, qos=0, retain=False) ... ``` You'll get: ![](https://i.imgur.com/YsboDek.png) :::danger * 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`). ::: ## Creating dashbord elements. 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. ![](https://i.imgur.com/YDQBZ4z.png) See the alternatives you have and how they can be configured. ## Exercise 1: Getting data from TTN Modifica el código del Ex. 2 de la sesión de laboratorio anterior en la que leías datos desde TTN, para que envie los valores de temperatura, humedad y luminosidad a Ubidots. El fichero `iot2_ex1.py` ofrece un esqueleto de la solucion. :::warning Tienes que entregar el fichero con el codigo. ::: # Part B: Data to Telegram Bot In this part you'll have to do something similar to the previous one, but this time the data from the TTN have to be accessed using a Telegram bot. The final results should be somehting like this: ![](https://i.imgur.com/r8HM7xw.png =300x) What you have to do first is create your own Bot. To generate an Access Token, you have to talk to the BotFather following a few simple steps described here: https://core.telegram.org/bots#6-botfather (you should have the Telegram app installed in your smartphone) The "dialogue" will be something like this: ![](https://i.imgur.com/uqknk7g.png) ## Exercise 2: As a starting point you have to use as a reference the file `iot2_ex2.py`. Fill in the places where the text `___FILL_IN_HERE___` is found with the proper values. Select just one value from the sensor for the moment (e.g., temperature) Execute you Bot either in your computer or in repl.it and test it using a smartphone with Telegram. :::info To program the Bot in python we will use this package: ``` https://github.com/python-telegram-bot/python-telegram-bot ``` If you are using your computer, than you'll have to install it using: ``` $ pip install python-telegram-bot ``` or, if you aree using `repl.it`, you have to istall it from the Packages menu: ![](https://i.imgur.com/ecbQt6I.png =x200) and typing in the search field `python-telegram-bot` and then on the **+** button ::: Modify the Bot so that it can return the value of temperature, humidity and luminosity according to the request. :::warning Tienes que entregar el fichero con el codigo. :::