Skip to content

Instantly share code, notes, and snippets.

@ericnakagawa
Created January 28, 2016 17:00
Show Gist options
  • Save ericnakagawa/39aacfacc1cf9473a2ac to your computer and use it in GitHub Desktop.
Save ericnakagawa/39aacfacc1cf9473a2ac to your computer and use it in GitHub Desktop.
My tessel project file for gathering home telemetry and sending to Parse
var tessel = require('tessel');
var wifi = require('wifi-cc3000');
var https = require('https');
var climatelib = require('climate-si7005');
// var ambientlib = require('ambient-attx4');
// Constants
var PARSE_APP = 'APP_ID';
var PARSE_KEY = 'APP_KEY';
var log = [];
var device_id = tessel.deviceId();
var timeouts = 0;
// var ambient = ambientlib.use(tessel.port['A']);
var climate = climatelib.use(tessel.port['B']);
var led1 = tessel.led[0].output(1);
var led2 = tessel.led[1].output(0);
var led3 = tessel.led[2];
var led4 = tessel.led[3];
wifi.on('connect', function(data){
// you're connected
console.log("wifi: connect emitted", data);
setTimeout(loop, 1000);
});
wifi.on('disconnect', function(data){
// wifi dropped, probably want to call connect() again
console.log("wifi: disconnect emitted", data);
})
wifi.on('timeout', function(err){
// tried to connect but couldn't, retry
console.log("wifi: timeout emitted");
timeouts++;
if (timeouts > 2) {
// reset the wifi chip if we've timed out too many times
powerCycle();
} else {
// try to reconnect
connect();
}
});
wifi.on('error', function(err){
// one of the following happened
// 1. tried to disconnect while not connected
// 2. tried to disconnect while in the middle of trying to connect
// 3. tried to initialize a connection without first waiting for a timeout or a disconnect
console.log("wifi: error emitted", err);
powerCycle();
});
function powerCycle(){
console.log('wifi: powercycling')
// when the wifi chip resets, it will automatically try to reconnect
// to the last saved network
wifi.reset(function(){
timeouts = 0; // reset timeouts
console.log("wifi: done power cycling");
// give it some time to auto reconnect
setTimeout(function(){
if (!wifi.isConnected()) {
// try to reconnect
connect();
}
}, 20 *1000); // 20 second wait
})
}
function connect(){
wifi.connect({
security: "wpa2"
, ssid: "Hagakure"
, password: "CHIsuke0811"
, timeout: 15 // in seconds
});
}
// connect wifi now, if not already connected
if (!wifi.isConnected()) {
console.log('wifi: wifi not connected, trying to connect');
connect();
}
// ambient.on('ready', function () {
// console.log('Connected to attx4');
climate.on('ready', function () {
console.log('Connected to si7005');
});
// });
// });
function loop () {
led1.toggle();
led2.toggle();
climate.readTemperature('f', function (err, temp) {
climate.readHumidity(function (err, humid) {
// ambient.getLightLevel( function(err, ldata) {
// ambient.getSoundLevel( function(err, sdata) {
console.log(process.memoryUsage(), timeouts);
if (err) {
console.log('Error in loop:', err);
throw err;
}
var temperature_f = parseFloat(temp.toFixed(4));
var temperature_c = parseFloat((parseFloat(temp.toFixed(4)) - 32) * 5 / 9);
var humidity = parseFloat(humid.toFixed(4));
// console.log("Light level:", ldata.toFixed(8), " ", "Sound Level:", sdata.toFixed(8));
var data = {
'temperature_f': temperature_f,
'temperature_c': temperature_c,
'humidity': humidity,
'device_id': device_id,
};
var data_json = JSON.stringify(data);
var headers = {
'X-Parse-Application-Id': PARSE_APP,
'X-Parse-REST-API-Key': PARSE_KEY,
'Content-Type': 'application/json',
'Content-Length': data_json.length,
// 'Connection': 'keep-alive'
};
var options = {
host: 'api.parse.com',
port: 443,
path: '/1/classes/measurement',
method: 'POST',
headers: headers
};
if(wifi.isConnected()) { // && (log.length >= 2)) {
console.log('making request');
var req = https.request(options, function(res) {
var responseString = '';
res.setEncoding('utf-8');
console.log('request made');
res.on('data', function(data) {
console.log('connected write');
responseString += data;
});
res.on('end', function() {
var resultObject = JSON.parse(responseString);
console.log("successful result", resultObject);
setTimeout(loop, 7000);
});
res.on('connect', function(e) {
console.log('connected!!!!!!!!')
});
res.on('error', function(e) {
console.log('Error:', e, 'Waiting 30 seconds before trying again.');
// turn on all LEDs to signal an issue has occurred
led1.output(1);
led2.output(1);
led3.output(1);
led4.output(1);
setTimeout(loop, 30000);
});
req.on('error', function(e) {
console.log('Error:', e, 'Waiting 30 seconds before trying again.');
// turn on all LEDs to signal an issue has occurred
led1.output(1);
led2.output(1);
led3.output(1);
led4.output(1);
setTimeout(loop, 30000);
});
});
req.write(data_json);
req.end();
}
// });
});
});
}
climate.on('error', function(err) {
console.log('climate: error connecting module', err);
setTimeout(loop, 10000);
});
// ambient.on('error', function (err) {
// console.log(err)
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment