Logging your data in the cloud
As discussed, we will first log the data in the cloud. Here, we will use the same
hardware and software configurations that we used in the previous chapter.
To log data in the cloud, we will use a service called Dweet.io. Dweet.io is a simple
publishing and subscribing platform created for machines, gadgets, and any form of
computer. There's no sign up or set up required so you can use it instantly.
Here is the link to Dweet.io:
http://dweet.io/
We will use the following code. Let's examine it first so that you can understand
it easily:
1. We create a new XDK project first.
2. Then, we include the following libraries, which are used to communicate
with the pins of the Galileo board:
var mraa = require("mraa");
var util = require('util');
var request = require('request');
3. We need to set the analog pins and set their resolution to 12 bits:
var light_sensor_pin = new mraa.Aio(0);
light_sensor_pin.setBit(12);
var temp_sensor_pin = new mraa.Aio(1);
temp_sensor_pin.setBit(12);
4. We then define the function send_data().
5. The first thing that we will measure is the light level. Here, we assign
the sensor to read it and we compute and convert the data to obtain a
percentage result:
var a = light_sensor_pin.read();
var light_level = a/4096*100;
light_level = light_level.toPrecision(4);
console.log("Light level: " + light_level + " %");
We will do the same for the temperature measurement. We assign another
sensor to measure it.
www.it-ebooks.info
Chapter 6
[ 57 ]
6. Then, we will add equations so that the displayed temperature result will be
in degree Celsius:
var b = temp_sensor_pin.read();
var temperature = (b/4096*5000 - 500) / 10;
temperature = temperature.toPrecision(4);
console.log("Temperature: " + temperature + " C");
7. You should give a unique name to your device so you can easily identify it.
You can use your own name for instance.
var device_name = 'galileo_5etr6b';
8. Next, we will build the url:
var device_name = 'galileo_5etr6b';
var dweet_url = 'https://dweet.io/dweet/for/' + device_name +
'?temperature=' + temperature + '&light=' + light_level;
console.log(dweet_url);
9. We will also set an option to request data from Dweet.io:
var options = {
url: dweet_url,
json: true
};
10. Then, we will send that request to Dweet.io:
request(options, function (error, response, body) {
if (error) {console.log(error);}
console.log(body);
});
11. We will set our request to be sent every 10 seconds for us to be able to get the
real-time data:
send_data();
setInterval(send_data, 10000);
12. Don't forget to modify package.json to include all the Node.js modules that
are used in this project:
{
"name": "Dashboard",
"description": "",
"version": "0.0.0",
"main": "main.js",
"engines": {
www.it-ebooks.info
Internet of Things with Intel Galileo
[ 58 ]
"node": ">=0.10.0"
},
"dependencies": {
"util": "latest",
"request": "latest"
}
}
Note that you can download the complete code from the following
GitHub link:
https://github.com/marcoschwartz/galileoblueprints-
book
13. Now, we will build the code, upload it to our Galileo board, and run it. If all
goes well, you should be able to receive an answer every 10 seconds. Here is
an example of what you will see in your console:
14. After this, check results in Dweet.io by visiting the following link:
https://dweet.io/get/latest/dweet/for/my-thing-name
15. Don't forget to change "my-thing-name" to the name of your own device.
Otherwise, you won't get anything.
www.it-ebooks.info