Home Labs Stories Save your data using nodejs-mqtt-mongodb

SAVE YOUR DATA USING NODEJS-MQTT-MONGODB

image story

ABOUT THIS STORY


Posted on Sept. 27, 2017



Intermediate
1 Hour

Introduction

Introduction


I was looking for an example of using mongodb to store my node data and came across this:

https://github.com/dennisdegreef/mqtt-mongo-recorder

mongodb is an example of a noSQL database. Using the concept of documents and collections, noSQL databases are ideal for string and management of json-formatted data.

I set up this demo on a clean install of ubuntu xenial (16.04.03 LTS server) running on an HP Proliant MicroServer, bought from a man down the pub ;-) - DigitalOcean provide clear and simple install guides for mongodb and nodejs:

https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

Note that I am running mosquitto client, mongodb and the nodejs app on the same machine.

With some simple mods to server.js, config.js and an update of package.json, it's great to see a demo come together.

in server.js, I changed the the format of mqttURI to include username and password:

var mqttUri  = 'mqtt://' + config.mqtt.user + ':' + config.mqtt.password + '@' + config.mqtt.hostname + ':' + config.mqtt.port;`

In config.js, I modded and added a couple of variables:

config.mqtt.namespace = process.env.MQTT_NAMESPACE || '+/devices/+/up';
config.mqtt.hostname  = process.env.MQTT_HOSTNAME  || 'eu.thethings.network';
config.mqtt.port      = process.env.MQTT_PORT      || 1883;
config.mqtt.user      = process.env.MQTT_USER      || '<appid>';
config.mqtt.password  = process.env.MQTT_PASSWORD  || '<accesskey>';

In package.json, I updated the dependencies:

    "dependencies": {
        "mqtt": "2.13.0",
        "mongodb": "2.2.31"
    },

Running server.js populates a database instance called mqtt with the data from all active nodes for the specified application.

So, it's now down to you to analyse the masses of data collected...

UPDATE:

If you want to connect to the TTN broker using mqtts then make the following changes to server.js - note that mqtt.client supports similar options to tls.client

var fs       = require('fs');

var client = mqtt.connect(config.mqtt.hostname, {
                        ca: [fs.readFileSync(config.mqtt.cafile)],
                  username: config.mqtt.user,
                  password: config.mqtt.password,
                      port: config.mqtt.port
             } );

and config.js

config.mqtt  = {};
config.mqtt.namespace = process.env.MQTT_NAMESPACE || '+/devices/+/up';
config.mqtt.hostname  = process.env.MQTT_HOSTNAME  || 'mqtts://eu.thethings.network';
config.mqtt.port      = process.env.MQTT_PORT      || 8883;
config.mqtt.user      = process.env.MQTT_USER      || '<appid>';
config.mqtt.password  = process.env.MQTT_PASSWORD  || '<access_key>';
config.mqtt.cafile    = process.env.MQTT_CAFILE    || 'mqtt-ca.pem';

I hope this helps some people out there, and I welcome any comments.