Skip to content

Forward data from TTN to any Web app

Thomas Amberg edited this page Feb 3, 2017 · 25 revisions

The TTN backend provides a MQTT broker. All examples here can be run from your desktop. Once it works, you could host the same glue code in the cloud, e.g. at Google App Engine.

Install Nodejs

Download and install Nodejs from https://nodejs.org/en/ then type

$ node -v

(The basic idea of the code below works in any other language, of course.)

Install ttn

$ npm install ttn --save

MQTT Logger

With a MQTT client you can subscribe to packets.

This client just logs incoming packets to your console.

https://github.com/TheThingsNetwork/node-app-sdk/blob/master/src/example.js or https://bitbucket.org/tamberg/iotworkshop/src/tip/NodeJS/ttn-mqtt-logger.js

var ttn = require('ttn');

var appId = 'TTN_APP_ID';
var accessKey = 'TTN_ACCESS_KEY';

var client = new ttn.Client('eu', appId, accessKey);

client.on('message', function(deviceId, message) {
  console.log(message.payload_raw);
});

For app ID and keys, see https://console.thethingsnetwork.org/applications

ThingSpeak

Instead of logging to the console, your MQTT client (aka glue code) can forward packets.

In the case of ThingSpeak you send a GET (!) request to post data to a ThingSpeak channel.

https://bitbucket.org/tamberg/iotworkshop/src/tip/NodeJS/ttn-thingspeak-forwarder.js

IFTTT Maker Channel

In the case of IFTTT the code looks very similar.

Here, the value is transferred in the body of a POST request.

https://bitbucket.org/tamberg/iotworkshop/src/tip/NodeJS/ttn-ifttt-forwarder.js

Hosted Nodejs

If you prefer hosting the node code in the cloud, check https://zeit.co/now/

Troubleshooting

Error: Connection refused: Not authorized => Check your keys.