Last active
August 29, 2015 14:11
-
-
Save yamaryu0508/dbbba9065eac6824005d to your computer and use it in GitHub Desktop.
I2C温度センサーADT7410のIoT(M2M)スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ADDRESS, API_KEY, FEED_ID, async, i2c, INTERVAL, readValue, registToXively, request, sensor; | |
i2c = require('i2c'); | |
request = require('request'); | |
async = require('async'); | |
ADDRESS = 0x48; | |
INTERVAL = 60*1000; // mili-sec | |
sensor = new i2c(ADDRESS, {device: '/dev/i2c-1'}); | |
API_KEY = '{X-ApiKey}'; | |
FEED_ID = '{FEED ID}'; | |
// read from ADT7410 | |
readValue = function(callback) { | |
sensor.readBytes(0x00, 2, function(err, data) { | |
var temp, value; | |
temp = (data[0] << 8 | data[1]) >> 3; | |
if (temp >= 4096) { | |
temp -= 8192; | |
} | |
value = temp * 0.0625; | |
console.log("Temperature: " + value + " [Deg. C.]"); | |
callback(value); | |
}); | |
}; | |
// regist to xively | |
registToXively = function(apiKey, feedId, value) { | |
request({ | |
method: 'PUT', | |
url: "https://api.xively.com/v2/feeds/" + feedId + ".json", | |
headers: { | |
"X-ApiKey": apiKey | |
}, | |
json: { | |
'datastreams': [ | |
{ | |
'id': 'Temperature', | |
'current_value': value | |
} | |
] | |
} | |
}, function(err, response, body) { | |
if (err) { | |
throw err; | |
} | |
if (response.statusCode === 200) { | |
console.log('RESULT: '); | |
} else { | |
console.log("response error: " + response.statusCode + ", " + err); | |
} | |
body = body ? body : ''; | |
console.log(body); | |
return body; | |
}); | |
}; | |
// loop | |
async.forever(function(callback) { | |
async.series([ | |
function(callback) { | |
readValue(function(value) { | |
return registToXively(API_KEY, FEED_ID, value); | |
}); | |
setTimeout(callback, INTERVAL); | |
} | |
], callback); | |
}, function(err) { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment