-
Notifications
You must be signed in to change notification settings - Fork 6
thethings.io test application
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 that 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.
## Set upWe will need to set up thethings.io cloud and the development environment.
### thethings.io cloudFirst of all, we need to create an account on thethings.io. You can do that here : Create an account
Once 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. This process is pretty straightforward, 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.
### Development environment.
Once we have the thing token code, we have to configure the development environment.
As a basic setup for the development of the application, you need npm and node.js.
You also need to install the netbeast package manager through npm. Check this guide to get it.
We will create an app called wemoswitch
netbeast new wemoswitch
Now, we need to add some node node modules.
npm install thethingsio-api --save
npm install wemo-client --save```
* --save option is recommended to update package.json file.
We are now ready to start coding.
***
<a name="coding">
## Coding
</a>
A server.js file will have been created once you have created an app, . It is our main backend file where we will put all the code.
We also need to create a second server file. Name it with a different name, for instance, server2.js.
We also need a config.json file. On it we will type our thing token previously obtained from thethings.io. This is how:
***
<a name="config">
### config.json
</a>
{ "thingToken" : "yourthingtokencodehere" }
***
<a name="server">
### server.js
</a>
In this file, we will write an application that 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.
***
<a name="server2">
### server2.js
</a>
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.
***
<a name="testing">
## Testing
</a>
Open a terminal in your project folder and type
`node server.js`
You will see a message with a key and status value.
That means that you are writing the value on thethings.io cloud.
Also, the WeMo switch will turn on/off depending on the value you are sending.
Now, open a second terminal and type
`node server2.js`
You will receive the data from thethings.io cloud.
# warning
: now our docs are being published in our gitbook and available on our website. Visit http://docs.netbeast.co/
- Visit our site https://netbeast.co
- Mail us: staff [at] netbeast.co
- Report a bug or enter discussion at issues
- Other resources: Dashboard, Netbeast API