Created
November 11, 2014 21:37
-
-
Save norlin/4ddc77c89180add10de3 to your computer and use it in GitHub Desktop.
simple climate monitor
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 Promise = require('es6-promise').Promise; | |
var tessel = require('tessel'); | |
var wifi = require('wifi-cc3000'); | |
var climatelib = require('climate-si7020'); | |
var climate = climatelib.use(tessel.port['D']); | |
var http = require('http'); | |
var led = tessel.led[0].output(0); | |
var led2 = tessel.led[1].output(0); | |
function checkClimate() { | |
led2.output(1); | |
climate.readHumidity(function(err, humid){ | |
climate.readTemperature(function(err, temp){ | |
send(temp.toFixed(2), humid.toFixed(2)); | |
setTimeout(checkClimate, 1000 * 600); | |
}); | |
}); | |
} | |
var climateReady = new Promise(function(resolve) { | |
climate.on('ready', function () { | |
resolve(); | |
}); | |
}); | |
var wifiReady = new Promise(function(resolve, reject) { | |
function check(){ | |
if (wifi.isConnected()) { | |
if (checkInt) { | |
clearInterval(checkInt); | |
} | |
resolve(); | |
} | |
} | |
var checkInt = setInterval(check, 1000); | |
}); | |
climate.on('error', function(err) { | |
console.log('error connecting module', err); | |
}); | |
Promise.all([climateReady, wifiReady]).then(function(){ | |
led.output(1); | |
checkClimate(); | |
}, function(){ | |
onError(); | |
}); | |
function onError(){ | |
var state = 0; | |
setInterval(function(){ | |
if (state) { | |
state = 0; | |
} else { | |
state = 1; | |
} | |
led.output(state); | |
}, 500); | |
} | |
function send(temp, hum){ | |
var options = { | |
hostname: '192.168.1.200', | |
port: 3035, | |
path: '/log?', | |
method: 'GET' | |
}; | |
options.path += 'temp=' + temp; | |
options.path += '&hum=' + hum; | |
console.log('send req...'); | |
var tesreq = http.request(options, function(res){ | |
led2.output(0); | |
}); | |
tesreq.on('error', function(err){ | |
led2.output(0); | |
}); | |
tesreq.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got this test to work by adding the following to the
http.request
callback function:An incompatibility we previously had was not requiring these events to flush, but you might need to add these listeners now. I'll look into this a bit more.
If there are any error messages you are running into, feel free to paste them in a comment here.