Skip to content

thethings.io test application

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

Cloud integration with thethings.io

In this small guide, we will explain how to manage smart things (in this case a belkin wemo switch) using thethings.io cloud service.

We will create a simple application which can turn on/off the wemo switch and send its status to the cloud. We will also be able to read the Wemo switch's status using thethings.io's cloud services.

## 2. SET UP

We will need to set up thethings.io cloud and the development environment.

### 1. thethings.io cloud

First of all, we need to create an account on thethings.io. You can do that here : Create an account

Now that you have created your account log in and follow this useful Guide

We will need to get an activation code in order to obtain a thing token code. This is necessary to communicate our application with the cloud. You can't miss it if you follow the guide.

As soon as you get your thing token code, you'll be ready for the next step.


### 2. development environment.

We have to configure the development environment.

As basic setup for the development of the application, you should have installed npm and node.js. If not, you should do so now.

You need to install the netbeast package manager through npm. Look this guide to get it.

We will create an app called wemoswitch

netbeast new wemoswitch

Now, we need to add some node modules.

  1. cd wemoswitch
  2. npm install thethingsio-api --save
  3. npm install wemo-client --save
  • --save option is recommended to update package.json file.

Now, we are ready to start coding.


## 3. CODING

Once you have created an app, a server.js file while have been created. This is our main backend file where we will put all the code.

We will need to create a second server file. Name it with a different name, for instance, server2.js.

We also need to create a config.json file. Here we will type our thing token previously obtained from thethings.io. We need to write it this way:


### config.json
{
    "thingToken" : "yourthingtokencodehere"
}

### server.js

In this file, we will create an application which is able to turn on/off the Wemo switch and also we will write that value (on/off) on thethings.io cloud service.

var theThingsAPI = require('thethingsio-api');

//create Client
var client = theThingsAPI.createClient()
var Wemo = require('wemo-client')
var wemo = new Wemo()


//Object for thethingio code.
var option = {
  "values": [
  {
    "key": 'status',
    "value": "off"
  }
  ]
}

// Wemo code to activate or deactivate wemo switch.

wemo.discover(function(deviceInfo) {
  // Get the client for the found device
  var client = wemo.client(deviceInfo);
  if (option.values[0].value === 'on')
  // Turn the switch on
  	client.setBinaryState(1)
  else if (option.values[0].value === 'off')
  	client.setBinaryState(0)
  else {
  	client.setBinaryState(0)
  	console.log('Option On/Off')
  }
  	
});


// The things IO CODE

//This is usually fired when there are problems with the activation code
client.on('error', function (error) {
  console.log('Error:', error)
})

//Write option value each 1000ms.
client.on('ready', function () {
//write the object
setInterval(function () {
  client.thingWrite(option, function (error, data) {
    console.log(error ? error : data)
  })
  console.log("send", option)
}, 1000)
})

This app will write Wemo switch status each second on thethings.io cloud.


### server2.js

In this file, we will create an application which is able to read the object value with key 'status' from thethings.io cloud service.

var theThingsAPI = require('thethingsio-api');

//create Client
var client = theThingsAPI.createClient()

//The thing IO CODE

//This is usually fired when there are problems with the activation code
client.on('error', function (error) {
	console.log('Error:', error)
})


//Read information from an object with key status each 1000ms.
client.on('ready', function () {
	setInterval(function () {
		client.thingRead('status', function (error, data) {
			console.log(error ? error : data)
		})},1000)
});

This app will read Wemo switch status each second from thethings.io cloud.


## 4. TESTING
  1. Open one terminal and type

node server.js

You will see a message with key and status value.

That means that you are writing the value on thethings.io cloud.

Also Wemo switch will turn on/off. It depends on the value that you are sending.

  1. Open a second terminal and type server2.js

You will receive the data from thethings.io cloud

Clone this wiki locally