November 13, 2016
Data storage and visualization for The Things Network

Got some LoRa hardware and loking at some way to have historical view on the data you are sensing? Not much web development knowledge? Want a quick way to have an end to end solution in a matter of less than an hour?

Here is a simple guide to collect data with a LoRA enabled node, compute them a bit on NodeRed, store them in InfluxDB and visualize them with Chronograph.

image

The step by step instructions are given for a Windows 10 system, but can be adapted for Linux or MacOS systems.

This tutorial assumes you are familiar with the node programing and you have a The Things Network backend account.

The application we will use is the example DHTSensor sketch provided with The Things Network Arduino library.

During this tutorial, we will register the node and the application on the backend, make some code for the ThingsUNO and a DHT22 temperature/hygrometry sensor but could be easily adapted to any other sensor, retrieve the data on TTN backend, install nodeRed localy, process them to a proper format for the IinfluxDB and visualize them on Chronograh after setting them up.

Create the application

Log in to The Things Network Dashboard.

Create your first The Things Network application by clicking create application. Fill in the desired application name (e.g. Environment data) and click Create application.

You will be redirected to the newly created Application page, where you will be able to register the devices belonging to this application.

You need to write down the information to access the data from outside of the dashboard.

In the application page, in the application info section, click on learn how to get data from this app

  • Write down the application EUI
  • Click on the eye next to the application key to reveal it and copy the key

Register the device – ABP

The Things Network supports the two LoRaWAN mechanisms to register devices: activation by personalization (ABP) and over the air activation (OTAA). In this tutorial, we use ABP.

To register the device, go back to the dashboard and click Register Device on the application page. This will take you to the device registration page. Select ABP. We will let both session keys to be randomly generated. To continue, click Register. Now that the device is registered, the device page is shown:

You need to write down the information to configure the application

  • Click the so the mention at the end change from hex to msb
  • Click the eye so the key becomes visible
  • Copy the Dev Address
  • Copy the Network Session Key
  • Copy the App Session Key

Program the node

Open the example sketch File/Examples/TheThings/Network/SensorExamples/DHTSensor in the Arduino IDE.

Configure properly the Dev address, Application session key and Network Session key in the code, before compiling and transfer it to the board.

You can refer to https://www.thethingsnetwork.org/docs/current/uno/ for additionnal informations.

Check data are received on the Dashboard

Node Red server

Download NodeRed located at https://nodered.org/docs/getting-started/installation, for your system. After the node.js is installed, an entry is added to your start menu. Launch the node.js command prompt, which has been added to your start menu.

To install Node-RED, at the command prompt, type npm install -g --unsafe-perm node-red.

To communicate with TTN backend, you need to install npm install node-red-contrib-ttn

To send data to the InfluxDB, you need to install npm install node-red-contrib-influxdb

You can now launch Node RED server by entering node-red at the command prompt. The Node RED environment is accessible thru a web browser by pointing to http://localhost:1880

Node Red code

Create the flow which will take the data input from TTN and convert it to the proper format and send the records to the InfluxDB. Use the top right menu and select Import/From clipboard option. Paste the content bellow:

[{"id":"b20b740a.0bd03","type":"ttn","z":"ee367a8.1368a88","app":"893e80.139f818","name":"TTN data","x":160,"y":420,"wires":[["8c23c74e.f3aee8"],[]]},{"id":"8c23c74e.f3aee8","type":"function","z":"ee367a8.1368a88","name":"Extract Temp and Humidity","func":"var buf = new Buffer(msg.payload.raw, 'base64'); // put in msg.payload the payload raw data stored initially as Base64\nvar node = msg.devEUI;\n\ntemp = buf[2] * 255 + buf[3];\nhum = buf[0] * 255 + buf[1];\n\n// construct a new object to store the data message\nvar data = {\npayload : [\n    [{\n        numValue: temp/10.0,\n        time: new Date(msg.metadata.server_time).getTime()\n    },\n    {\n        tag1:\"temp\",\n        sensor:node\n    }],\n    [{\n        numValue: hum/10.0,\n        time: new Date(msg.metadata.server_time).getTime()\n    },\n    {\n        tag1:\"hum\",\n        sensor:node\n    }]\n]};\nreturn data;","outputs":1,"noerr":0,"x":440,"y":420,"wires":[["bc620905.c19b58"]]},{"id":"bc620905.c19b58","type":"influxdb out","z":"ee367a8.1368a88","influxdb":"d9bccf83.4cc2c","name":"","measurement":"environment","x":840,"y":420,"wires":[]},{"id":"893e80.139f818","type":"ttn app","z":"","appEUI":"","accessKey":"","broker":"staging.thethingsnetwork.org"},{"id":"d9bccf83.4cc2c","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","database":"environment","name":""}]

You can add it to the current flow, or put it in a new one. You need now to change some parameters. Double click on the TTN node, click on the pencil next to the AppEUI. Paste the AppEUI which has been created earlier. Enter you access key, click on update then Done buttons. Now you can activate the flow, click on the Deploy button net to the menu button at the top right of the window.

The database you will store the environmental data is configured in the InfluxDB node. it has been set to environment in the above flow, on a server located on the same machine (127.0.0.1)

InfluxDB server

Download the InfluxDB server for your system from https://www.influxdata.com/downloads/. You will need to register to access to the download, but no charges will apply.

Unzip the content of the downloaded zip file in an appropriate location.

Launch the influxdb.exe, it will start the InfluxDB server on your system.

InfluxDB database

To manage InfluxDB, in your web browser, go to http://localhost:8083. To create the database, click on the button ‘Query Templates’ and select Create Database.

In the Query field, set the db_name to the one you want – i.e. ‘environment’ – and press the Enter key to execute it.

The name has to be the same as in the Node RED code.

Chronograf server

Download the Chronograf server for your system from https://www.influxdata.com/downloads/. Copy the downloaded file in an appropriate location.

Launch the chronograf_windows_amd64.exe, it will start the Chronograf server on your system.

Chronograph dashboard

In the web browser, go to http://localhost:10000. Chronograf will ask you to configure the server to use. Click on Add Server button.

Set the name you want for the server and click on Add button.

You can now click on Done in the top left corner of the screen.

Click on Add Visualization button

Enter the graph name – i.e. Temperature - and press Save

Click on the button between ‘BUILDER’ and the query to configure the server

Select the Databases to use. Check the Make default box, so it will be automatically selected in the following visualization creation.

Configure the query as below:

image

Then click Done at the top right corner. Do the same for the humidity:

image

You now have two visualizations configured.

Click on Daskboard at the top of the screen. Click on the + to create a new dashboard. Give teh name you want - i.e. environment - and click save. Now click on Add visualization button, the Add from existing visualization button. Check the boxes for humidity and temperature visualtation, and click on Add visualization to dash button.

You now have your dashboard with the historical data. You can resize the panels, place them side by side, or stretch, resize.

The sensor selection has been made variable, this will allow you to have several sensors pushing the same kind of data and select the one you want to view, without creating multiple visualizations.

Blog comments powered by Disqus