-
Notifications
You must be signed in to change notification settings - Fork 6
thethings.io test application
In this file, we will explain how to manage smart things (in this case wemo switch) using thethings.io cloud service.
We will create a simple application which can turn on/off wemo switch and send wemo switch status to the cloud. Also we will be able to read wemo switch status from the cloud service of thethings.io
We will need to set up thethings.io cloud and the development environment.
First of all, you need to create an account. You can do that here : Create an account
Once that you have created your account. Log in and follow this Guide
You will need to get an activation code in order to get a thing token code. That will be necessary to communicate you application with the cloud. You can't miss it if you follow the guide.
As soon as you get your thing token code, you will be ready for going to the next step.
You have to configure you development environment.
You should have installed npm and node. If other case, do it!.
You need to install the xway package manager through npm. Look this guide to get it.
Create an app called wemoswitch
xway new wemoswitch
Now, we need to add some node modules.
cd wemoswitch
npm install thethingsio-api --save
npm install wemo-client --save
- --save option is recommending for update package.json file.
Now, we are ready for start coding.
When you have created an app, also a server.js file has been created. This is our main file where we will put all the code.
You will need to create a second server.js file. Named it with a different name, for instance, server2.js.
We also need to create a config.json file. Here we will type our thing toke that we have got from thething.io. You need to write on this way:
{
"thingToken" : "yourthingtokencodehere"
}
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 desactivate 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 thing 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.
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.
- 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 turns on/off. It depends on what value you are sending.
- Open a second terminal and type server2.js
You will receive the data from thethings.io cloud
##CONTACT
Netbeast: http://netbeast.co /[email protected]
Thethings.io: https://thethings.io
*Be brave and make your own apps. We are looking forward to know them
# 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