Store your TTN node data in a NoSQL database, then show it as a line graph on a webpage…

This blog post is all about building a Node.js server to retrieve your LoRaWAN device data from TheThingsNetwork (TTN), then store the data in a Mongo NoSQL database. The assumption is that you have built a Node.js app before. There is also a very simple client app that displays your data as a line graph on a webpage.

The Things Network (TTN) is an open-source, secure and scalable solution that has around 9,000 gateways and 90,000 developers around the world. It started in August 2015 and just keeps on growing.

TTN manages every aspect of creating and managing a LoRaWAN network except storing your data (note that TTN does offer a storage solution, but provides storage for 7 days only). Also note that there are several integrations available, including EVRYTHNG, AWS IoT, Cayenne and Tago.

The following example is useful if you want to keep your data in-house. Also, if you are like me, you want to understand and have control of the key components of your solution, even if you decide in the future to go with a 3rd-party.

This code runs as two separate Node.js applications on your laptop/desktop, but of course when moving to production, the collector app would need to run on a server and the viewer app is actually a web server that would also need to run continuously.

In addition to the code described below, you need:
Node.js – javascript server-side run-time environment – I used version 10.16.3 LTS
MongoDB – NoSQL database, ideal for json-formatted data – I used a free cloud instance, sign up here (no credit card required) – and of course an active LoRaWAN device, registered within an application configured in your own TTN console –

You can find all the source code, together with the slidedeck I presented at TheThingsConference UK 2019, here:

I have split the code into collector and viewer folders. You need to fill out the connection details for your mongodb  instance and your TTN application name and access key (token). Use the config.template as a basis, update with your credentials and then place a copy, called config.js,  into each of the collector and viewer folders on your laptop/desktop.

First, let’s take a look at collector; this uses a Node.js SDK provided by TTN that listens for each new published record, then inserts a document into your database. You can see the code for this in collector.js – note that I have created a separate file, mongo.js, for mongodb connectivity, as this is common code for both collector and viewer.

A quick diversion to talk about nomenclature – both SQL and NoSQL databases use the term ‘database’ – but a ‘table’ in SQL is equivalent to a ‘collection’ in NoSQL and a ‘record’ in SQL is a ‘document’ in NoSQL. What I really like about NoSQL databases is that the underlying format is JSON.

You can check the data is being stored by installing mongodb compass, community edition –

OK, so now that we have data in our database, let’s view it in a webpage as a line graph. The viewer folder holds a copy of config.js (note that this only needs to hold the credentials for the database) a copy of mongo.js for database connectivity, the application code in viewer.js and a sub-folder called views that holds index.hbs – a simplified html file.

The Node.js app actually runs the minimum form of express framework to host a simple web page. Our web page is served using ‘hbs’ as it’s default view engine ( and we use handlebar nomenclature {{JSON string}} to pass data arrays. So, take a look at viewer.js and see that we retrieve a number of documents from the database with ‘getArray’ and then push the relevant data into an array in ‘pushData’ to pass to the web page where we render it. We are going to plot the ‘rssi’ and ‘snr’ as seen by the first gateway to respond to your LoRaWAN device.

The file index.hbs contains the minimal html and javascript to display the data as two line graphs, using the Google charts libraries – more information here:

So, in summary:
TTN provides an excellent platform to move your node data to the cloud
TTN enables simple data access with a Node.js SDK
MongoDB is a NoSQL db, good for JSON data
Node.js is a simple way to build a headless server app

Now, it’s down to you to do something useful with your LoRaWAN device!

Tags: , , , , ,

See the comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My new go-to linux dev machine is a Chromebook…

The other day I arrived at work to discover I had left my laptop at home. I hot-desk some days each week in Liverpool in fabulous SensorCity (@SensorCityUK) and was lost without a keyboard and ability to geek. Fortunately, just 5 minutes walk away is the local branch of a well-known UK “previously-loved” chain, so I went for a meander. I returned with an ASUS C101PA ChromeBook for less than half-price. It has a 10.1″ 1200×800 touchscreen with 6-core ARM CPU, 4GB RAM, 16GB eMMC, USB-C charging and a 9-hour battery – what should a geek do???

The answer is to enable Chrome Developer mode and install xenial ubuntu, of course – note that you should backup before enabling as this will factory reset your chromebook and delete all your data. Warning over.

It is possible to run a full linux desktop (something lightweight, like xfce, bearing in mind the free space on the eMMC), but I like working with a command line and you will see that you end up with linux running in one or more Chrome Tabs, while retaining full access to the Chrome/Android side of things – browser, email, 6Music on BBC Radio iPlayer, etc.

So, The best instructions are those available from the master – take a look at

Here are the basics:

1. Enable developer mode –
2. Download the current version of crouton from here – crouton
3. Open a shell (Ctrl+Alt+T, type shell and hit enter)
4. Run sudo sh ~/Downloads/crouton -t core – wait patiently and answer the prompts to add user, etc.
5. Done! Run sudo enter-chroot

The result is an arm64 install of ubuntu xenial that has access to serial over USB, USB and SD card storage

So far, I have:

1. mbed-cli to build and flash code to STMicro Nucleo boards – this installed build-essential, git, python, etc.
2. go version go1.10.3

I am very impressed. So, don’t dismiss chromebooks as over-sized androids, poor iPad substitutes, especially if you can pick up a bargain…

Tags: , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.