Skip to content

Instantly share code, notes, and snippets.

@jiahuang
Created October 15, 2014 01:52
Show Gist options
  • Save jiahuang/5bba5918999d5d3f34c1 to your computer and use it in GitHub Desktop.
Save jiahuang/5bba5918999d5d3f34c1 to your computer and use it in GitHub Desktop.
/* global console */
/* global require */
var tessel = require('tessel');
var http = require('http');
var mainLoop = function() {
var options = {
hostname: '192.168.1.106',
port: 3000,
path: '/sensor_readings',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
var data = {temperature_f: 100.0};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function () {
console.log('Response received.');
});
res.on('end', function(){
// after the request is finished, loop again
setTimeout(mainLoop, 5000);
});
});
req.on('error', function(e) {
console.log('problem with request: ', e.message);
// on error loop again
setTimeout(mainLoop, 5000);
});
req.write(JSON.stringify({secret_token: 'cookie', sensor_reading: data}));
console.log('Pushed data.');
req.end();
};
console.log('Tessel started!');
setImmediate(mainLoop);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment