Skip to content

thethings.iO integration

Jon Senra Dearle edited this page Oct 30, 2015 · 4 revisions

An IoT cloud SaaS

thethings.io is a cloud service from a partner company that allows you to easily set up applications that share and publish data to a common panel. This way you can design Netbeast applications that fit better into bigger scenarios like organizations or cities, without worrying about having to build your own cloud or a complex p2p system onto the Netbeast router.

thethings.iO has published several node.js packages making it a seamless development experience if you already are familiar with the Netbeast development environment.

Getting started

Here's an example app. To read temperature from a device remotely and push it to your things panel could be as simple as this :

// npm install thethingsio-api
var theThingsAPI = require('thethingsio-api');
var client = theThingsAPI.createClient();
// Or client = theThingsAPI.createSecureClient() for https
client.on('ready', function () {
	client.thingRead('temperature', {limit:1}, function (error, data) {
		console.log(error ? error : data);
	});
});

MQTT communication

Here is an example on how to reach their servers with an mqtt connection

//Example to activate a client at thethings.iO platform
//Note: the activation code shall not be used instead, you can set the
//thingToken directly

//Usage: write key-value pairs in the console

var mqtt = require('mqtt')

var activationCode = 'one of your activation codes'//you should change this
var thingToken = null //you can add a thingToken too if don't have an activation code

var client = mqtt.connect({
  host:'mqtt.thethings.io',
  port:1883
  })

var activationTopic = 'v2/activations/'+activationCode+'/'+Math.random()
var thingTopic = 'v2/things/'+thingToken

client.on('connect',function(){
  console.log('connected to thethings.iO!')
  if(!thingToken){
    client.subscribe(activationTopic,function(err,granted){
      console.log('subscribe result for', activationTopic, err, granted)
    })
  }else{
    client.subscribe(thingTopic,function(err,granted){
      console.log('subscribe result for', thingTopic, err, granted)
      console.log('write your key value in the console:')
      console.log('Example:')
      console.log('fun',9000)
    })
  }
})

client.on('message', function (topic, message) {
    message = JSON.parse(message)
    console.log('message',topic, message)
    if(topic === activationTopic){
      console.log('activation')
      if(message.status === 'error'){
        console.error('failed to activate your thing')
        console.error(message.message)
        process.exit(1)
      }
      console.log('thing activated, hurray!')
      thingToken = message.thingToken
      thingTopic = 'v2/things/'+thingToken
      client.subscribe(thingTopic)
      console.log('write your key value in the console:')
      console.log('Example:')
      console.log('fun',9000)
    }
})

//this dependency is just for the example
var readline = require('readline')

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: true
})

rl.on('line', function (cmd) {
    var kv = cmd.split(' ')
    if(kv.length < 2) return
    var values = {values:[{key:kv[0].toString(), value:kv[1]}]}
    console.log('sending ',values,'to ',thingTopic)
    //publish your data
    client.publish(thingTopic,  JSON.stringify(values));
})

Wrap-up and useful stuff

There is so much more that you can do through the cloud platform of thethings.io. Here you can find out how we built our first app with thethings.io. Also, for further info you should take a look at https://thethings.io/