Created
June 14, 2014 05:11
-
-
Save ducks/c86a6163f09592ae84d9 to your computer and use it in GitHub Desktop.
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
// Script to pipe temperature and humidity in my room | |
// to my irc server and nodebot, Kramer. | |
// Edit HOSTNAME, PORT, and PATHNAME to use your information. | |
// This script uses the climate module because that's the one | |
// I bought but it should work with any of the other modules. | |
var http = require('http'); | |
var tessel = require('tessel'); | |
var climatelib = require('climate-si7005'); | |
var climate = climatelib.use(tessel.port['A']); | |
var postData = {}; | |
var options = { | |
hostname: 'HOSTNAME', | |
headers: { | |
'content-type': 'application/json' | |
}, | |
port: PORT, | |
path: 'PATH', | |
method: 'POST' | |
}; | |
climate.on('ready', function() { | |
setImmediate(function loop () { | |
climate.readTemperature('c', function (err, temp) { | |
climate.readHumidity(function (err, humid) { | |
postData.temp = temp.toFixed(4); | |
postData.humid = humid.toFixed(4); | |
sendPostRequest(postData); | |
// Set timeout at 1 minute for testing. | |
// Started to see leak warnings around 30 mins. | |
// Plan to run it every hour. | |
setTimeout(loop, 60000); | |
}); | |
}); | |
}); | |
}); | |
function sendPostRequest(postData) { | |
var req = http.request(options, function(res) { | |
console.log('STATUS: ' + res.statusCode); | |
console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
req.write(JSON.stringify(postData)); | |
req.end(); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment