EBC Exercise 31 Pushing Data to the Cloud

From eLinux.org
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder


Now that you are able to gather data on the Bone it would be nice to store it some place in the "Cloud". In this exercise we'll show how to push data to Sparkfun's Phant, and then later pull it from Phant and plot it with Plotly.

Gathering the Data

Here's a simple program for ready temperatures from some TMP101 sensors.

#!/usr/bin/env node
// Reads the tmp101 temperature sensor.

var i2c = require('i2c-bus');
var bus = 2;
var tmp101 = [0x48, 0x49, 0x4a];
var time = 1000;    // Time between readings

var sensor = i2c.openSync(bus); 

var temp = [];

for(var i=0; i<tmp101.length; i++) {
    temp[i] = sensor.readByteSync(tmp101[i], 0x0);
    console.log("temp: %dC, %dF (0x%s)", temp[i], temp[i]*9/5+32, tmp101[i].toString(16));
}

Create a Data Stream on Phant

Next go to https://data.sparkfun.com/ and click on CREATE on the top right. On the next page fill in the fields. I used:

Title: Three TMP101's
Description: Playing with phant and logging TMP101's.
Fields: temp0 temp1 temp2
Tags: temp
Location: Terre Haute, IN, United States

The only part the really matters is Fields, since that impact our code.

Click Save.

The next page has information about Public and Private Keys, but the most important part is to click Download your keys as a JSON file. Move the downloaded file to the folder where your code is and rename it keys_tmp101.json.

Pushing Your Data to Phant

Go to https://github.com/MarkAYoder/BeagleBoard-exercises/tree/master/iot/phant and download tmp101.js. You can do it with:

wget https://github.com/MarkAYoder/BeagleBoard-exercises/raw/master/iot/phant/tmp101.js

It should look like:

#!/usr/bin/env node
// Reads the tmp101 temperature sensor.

var i2c     = require('i2c-bus');
var fs      = require('fs');
var request = require('request');
var util    = require('util');

var filename = "/root/exercises/iot/phant/keys_tmp101.json";

var bus = 2;
var tmp101 = [0x48, 0x49, 0x4a];
var time = 1000;    // Time between readings

var sensor = i2c.openSync(bus);

var keys = JSON.parse(fs.readFileSync(filename));
// console.log("Using: " + filename);
console.log("Title: " + keys.title);
console.log(util.inspect(keys));

var urlBase = keys.inputUrl + "/?private_key=" + keys.privateKey 
                + "&temp0=%s&temp1=%s&temp2=%s";

var temp = [];

// Read the temp sensors
for(var i=0; i<tmp101.length; i++) {
    // temp[i] = sensor.readByteSync(tmp101[i], 0x0);
    temp[i] = Math.random();
    console.log("temp: %dC, %dF (0x%s)", temp[i], temp[i]*9/5+32, tmp101[i].toString(16));
}

// Substitute in the temperatures
var url = util.format(urlBase, temp[0], temp[1], temp[2]);
console.log("url: ", url);

// Send to phant
request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body); 
    } else {
        console.log("error=" + error + " response=" + JSON.stringify(response));
    }
});




thumb‎ Embedded Linux Class by Mark A. Yoder