---
# System prepended metadata

title: 'IoT Lab 4: data visualization'
tags: [RSE, Labs]

---

# IoT Lab 4: data visualization
###### tags: `RSE` `Labs`
> curso 2019/2020
> Marco Zennaro, PhD - ICTP

**All the code necessary for this Lab session is available at [bit.ly/rse2019lab](http://bit.ly/rse2019lab)** in folder `codigo/IoT`.


## Introduction
### What is the TIG Stack?
The TIG Stack is an acronym for a platform of open source tools built to make collection, storage, graphing, and alerting on time series data incredibly easy.

![](https://i.imgur.com/TzgFn2G.png)


### What is a time series?
A time series is simply any set of values with a timestamp where time is a meaningful component of the data. The classic real world example of a time series is stock currency exchange price data.

![](https://i.imgur.com/QUUpRV5.png)


### Tools
* **Telegraf** is a metrics collection agent. Use it to collect and send metrics to InfluxDB. Telegraf’s plugin architecture supports collection of metrics from 100+ popular services right out of the box.
* **InfluxDB** is a high performance Time Series Database. It can store hundreds of thousands of points per second. The InfluxDB SQL-like query language was built specifically for time series.
* **Grafana** is an open-source platform for data visualization, monitoring and analysis. In Grafana, users can to create dashboards with panels, each representing specific metrics over a set time-frame. Grafana supports graph, table, heatmap and freetext panels.

![](https://i.imgur.com/aANRGpe.png)


## Installing TIG
Let’s start by adding the influxdb repositories:
```
$ curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
$ echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
$ sudo apt-get update
````

We can now install Telegraf and Influxdb:
```
$ sudo apt-get install telegraf
$ sudo apt-get install influxdb
```

Install Grafana:
```
$ wget https://dl.grafana.com/oss/release/grafana_6.4.3_amd64.deb
$ sudo dpkg -i grafana_6.4.3_amd64.deb
```

We can now activate all the services:
```
$ sudo systemctl enable influxdb
$ sudo systemctl start influxdb
$ sudo systemctl enable telegraf
$ sudo systemctl start telegraf
$ sudo systemctl enable grafana-server
$ sudo systemctl start grafana-server
```

### Getting started with InfluxDB
InfluxDB is a time-series database compatible with SQL, so we can setup a database and a user easily. You can launch its shell with the `influx` command: 
```
$ influx
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
> 
```

#### Creating a database
Next step is creating a database called "telegraf" 
```
> CREATE DATABASE telegraf
> SHOW DATABASES
name: databases
name
----
_internal
telegraf
> 
```

#### Creating a user
Next step is creating a user (called “telegraf”) and granting it full access to the database.
```
> CREATE USER telegraf WITH PASSWORD 'superpa$$word'
> GRANT ALL ON telegraf TO telegraf
> SHOW USERS;
user     admin
----     -----
telegraf false
> 
```

#### Retention Policy
A Retention Policy (RP) is the part of InfluxDB’s data structure that describes for how long InfluxDB keeps data.
InfluxDB compares your local server’s timestamp to the timestamps on your data and deletes data that are older than the RP’s DURATION. A single database can have several RPs and RPs are unique per database.

```
> CREATE RETENTION POLICY thirty_days ON telegraf DURATION 30d REPLICATION 1 DEFAULT
> SHOW RETENTION POLICIES ON telegraf
name        duration shardGroupDuration replicaN default
----        -------- ------------------ -------- -------
autogen     0s       168h0m0s           1        false
thirty_days 720h0m0s 24h0m0s            1        true
> 
```

Exit from InfluxDB:
```
> exit
```


### Configuring Telegraf
Next, we have to configure the Telegraf instance to read from the TTN (The Things Network) server.
Luckily TTN runs a simple MQTT broker, so all we have to do is, first: 
```
$ cd /etc/telegraf/
$ sudo mv telegraf.conf telegraf.conf.old
``` 
then create a new `telegraf.conf` file, in the same directory, with the content below:

    [agent]
      flush_interval = "15s"
      interval = "15s"

    [[inputs.mqtt_consumer]]
      servers = ["tcp://eu.thethings.network:1883"]
      qos = 0
      connection_timeout = "30s"
      topics = [ "+/devices/+/up" ]
      client_id = "ttn"
      username = "XXX"
      password = "ttn-account-XXX"
      data_format = "json"

    [[outputs.influxdb]]
      database = "telegraf"
      urls = [ "http://localhost:8086" ]
      username = "telegraf"
      password = "superpa$$word"

where you have to change: "username" and "password" with the values:

![](https://i.imgur.com/90vU5CZ.png)


### Restart Telegraf
Then we can restart telegraf and the metrics will begin to be collected and sent to InfluxDB:
```
$ service telegraf restart
```
### Check database
We can check if the data is sent from Telegraf to InfluxDB:
``` 
$ influx
```

Enter an InfluxQL query using database 'telegraf':

    > use telegraf
    > select * from "mqtt_consumer"

you should start seeing something like:

    name: mqtt_consumer
    time                counter host   metadata_airtime metadata_frequency metadata_gateways_0_altitude metadata_gateways_0_channel metadata_gateways_0_latitude metadata_gateways_0_longitude metadata_gateways_0_rf_chain metadata_gateways_0_rssi metadata_gateways_0_snr metadata_gateways_0_timestamp metadata_gateways_1_channel metadata_gateways_1_latitude metadata_gateways_1_longitude metadata_gateways_1_rf_chain metadata_gateways_1_rssi metadata_gateways_1_snr metadata_gateways_1_timestamp payload_fields_humidity payload_fields_lux payload_fields_temperature port topic
    ----                ------- ----   ---------------- ------------------ ---------------------------- --------------------------- ---------------------------- ----------------------------- ---------------------------- ------------------------ ----------------------- ----------------------------- --------------------------- ---------------------------- ----------------------------- ---------------------------- ------------------------ ----------------------- ----------------------------- ----------------------- ------------------ -------------------------- ---- -----
    1572521236113061054 499     ubuntu 1482752000       867.5              10                           5                           39.48262                     -0.34657                      0                            -88                      7.2                     409385364                     5                           39.48646                     -0.3589999                    0                            -113                     2.5                     2717632268                    34.882110595703125      55.22838592529297  34.37117004394531          2    rse_testing/devices/device001/up
    1572521255767137586 500     ubuntu 1482752000       867.5        

### Visualizing data with Grafana

Log into Grafana using a web browser:
* Address: http://127.0.0.1:3000/login
* Username: admin
* Password: admin

you will be asked to change the password the first time.

You have to add a data source:
![](https://i.imgur.com/kaBc5PI.png)

then select
![](https://i.imgur.com/OXGJrZR.png)

then fill in the fields indicated with the red arrow

![](https://i.imgur.com/djIqjIq.png)


If everything is fine you should see:

![](https://i.imgur.com/l5Msx7V.png)

![](https://i.imgur.com/SR5y6mR.png)

Now you have to add a dashboard and add graphs to it to visualize the data:

click on
![](https://i.imgur.com/tNBHesC.png =x200)

then "New Dashboard", and "Add Query":

![](https://i.imgur.com/ya27c3v.png =x200)

and then specify the data you want to plot:
![](https://i.imgur.com/tkuYl1N.png)

you can actually see a lot of data "fields":


![](https://i.imgur.com/cedijx3.png)

Try to get something like:

![](https://i.imgur.com/pVwnpuc.png)


### Final result
You can add as many variables as you want to the same Dashboard. Have fun exploring Grafana!

:::danger
1.- Haz una captura de pantalla de la dashboard que has creado
:::


## InfluxDB and Python
You can interact with your Influx database using Python. You need to install a library called `influxdb`. The complete instructions are here:
https://www.influxdata.com/blog/getting-started-python-influxdb/


### Installing the Library
Like many Python libraries, the easiest way to get up and running is to install the library using pip.

We’re going to run pip using the -m argument to the Python command, in order to be certain which Python is the install target.
``` 
$ python3 -m pip install influxdb
```

You should see some output indicating success.

We’ll work through some of the functionality of the Python library using a REPL, so that we can enter commands and immediately see their output. Let’s start the REPL now, and import the InfluxDBClient from the python-influxdb library to make sure it was installed:
```
$ python3
Python 3.6.4 (default, Mar  9 2018, 23:15:03) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>>
```
No errors—looks like we’re ready to go!

##### Making a Connection
The next step will be to create a new instance of the InfluxDBClient (API docs), with information about the server that we want to access. Enter the following command in your REPL... we’re running locally on the default port:

``` 
>>> client = InfluxDBClient(host='localhost', port=8086, database='telegraf')
>>> 
```

:::info
There are some additional parameters available to the InfluxDBClient constructor, including username and password, which database to connect to, whether or not to use SSL, timeout and UDP parameters.
:::

We will list all databases and set the client to use a specific database:


Let’s try to get some data from the database:

    >>> client.query('SELECT * from "mqtt_consumer"')

The query() function returns a ResultSet object, which contains all the data of the result along with some convenience methods. Our query is requesting all the measurements in our database.


You can use the get_points() method of the ResultSet to get the measurements from the request, filtering by tag or field:

    >>> results = client.query('SELECT * from "mqtt_consumer"')
    >>> points=results.get_points()
    >>> for item in points:
    ...     print(item['time'])
    ... 
    2019-10-31T11:27:16.113061054Z
    2019-10-31T11:27:35.767137586Z
    2019-10-31T11:27:57.035219983Z
    2019-10-31T11:28:18.761041162Z
    2019-10-31T11:28:39.067849788Z


You can get mean values, number of items, etc:

    >>> client.query('select count(payload_fields_Rainfall) from mqtt_consumer’)
    >>> client.query('select mean(payload_fields_Rainfall) from mqtt_consumer’)
    >>> client.query('select * from mqtt_consumer WHERE time > now() - 7d')
    
    
:::danger
2.- Crea un programa python que imprima en pantalla los datos del ultimo dia.
:::
