Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2014 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9572141 to your computer and use it in GitHub Desktop.
Save anonymous/9572141 to your computer and use it in GitHub Desktop.
var wlan, dht, interval, failTimeout;
var debug = true;
var pastValues = {
temperature: [],
air_humidity: [],
soil_humidity: [],
light: []
};
function onInit() {
dht = require("DHT11").connect(C0);
//reset values
for (var prop in pastValues) {
pastValues[prop] = [];
}
setTimeout(function () {
wlan = require("CC3000").connect();
if (interval !== undefined)
clearInterval(interval);
interval = setInterval(gatherAndSend, 20000);
}, 500);
}
function gatherAndSend() {
dht.read(function (a) {
var current = {};
current.temperature = a.temp - 1;
current.air_humidity = a.rh;
current.soil_humidity = readSoilSync();
current.light = Math.round(analogRead(C3) * 100);
if (debug)
console.log(current);
for (var prop in current) {
var pastData = pastValues[prop];
//current reading is added to past, no matter what
pastData[pastData.length] = current[prop];
var total = E.sum(pastData);
var average = total / pastData.length;
var tolerance = average / 100 * 20; //20%
var upper = average + tolerance;
var lower = average - tolerance;
if (current[prop] > upper || current[prop] < lower) {
if (debug)
console.log(prop + " value set to average (" + average + "), was: " + current[prop]);
//set to average if value is 20% higher or lower
current[prop] = average;
}
if (pastData.length > 10) { //store max 10 past values
pastData = pastData.slice(1);
}
pastValues[prop] = pastData;
}
var options = {};
options.data = JSON.stringify({
version: "1.0.0",
datastreams: [{
id: "air_humidity",
current_value: current.air_humidity.toString()
}, {
id: "soil_humidity",
current_value: current.soil_humidity.toString()
}, {
id: "temperature",
current_value: current.temperature.toString()
}, {
id: "light",
current_value: current.light.toString()
}]
});
options.host = 'api.xively.com';
options.port = '80';
options.path = '/v2/feeds/1848857974';
options.method = 'PUT';
options.headers = {
"X-ApiKey": "xxx",
"Content-Length": options.data.length
};
connectAndPut(options);
});
}
function connectAndPut(options) {
failTimeout = setTimeout(function () {
console.log("Timed out, disconnecting...");
wlan.disconnect();
}, 15000);
setTimeout(function () {
wlan.connect("xxx", "xxx", function (s) {
if (s == "dhcp") {
var req = require("http").request(options, function (res) {
res.on('close', function (data) {
clearTimeout(failTimeout);
wlan.disconnect();
if (debug)
console.log('request done');
});
});
req.end(options.data);
}
});
}, 10);
}
//0.45 seems to be the absolute low value
function readSoilSync() {
var direction = (Math.random() > 0.5);
var outPin, inPin;
if (direction) {
outPin = C1;
inPin = C2;
} else {
outPin = C2;
inPin = C1;
}
pinMode(outPin, 'output');
pinMode(inPin, 'input_pulldown');
digitalWrite(outPin, true);
var total = 0;
for (var i = 0; i < 20; i++) {
total += analogRead(inPin);
}
var average = total / 20;
//if (debug)
// console.log("Humidity val: " + average);
digitalWrite(outPin, false);
return Math.round(average * 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment