Skip to content

Instantly share code, notes, and snippets.

@hansamann
Created March 11, 2014 17:33
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 hansamann/9490825 to your computer and use it in GitHub Desktop.
Save hansamann/9490825 to your computer and use it in GitHub Desktop.
Espruino Garduino
var wlan, dht, interval, failTimeout;
var debug = true;
var pastValues = {
temperature: [],
air_humidity: [],
soil_humidity: [],
light: [],
vref: []
};
function onInit() {
dht = require("DHT11").connect(C0);
setTimeout(function () {
wlan = require("CC3000").connect();
if (interval !== undefined)
clearInterval(interval);
interval = setInterval(sendData, 20000);
}, 500);
}
function sendData() {
failTimeout = setTimeout(function () {
console.log("Timed out, disconnecting...");
wlan.disconnect();
}, 30000);
//if cc3000 dies it will the timeout around it, leaving the 60 sec timeout working
setTimeout(function () {
wlan.connect("xxx", "xxx", function (s) {
if (s == "dhcp") {
if (debug)
console.log("Connected.");
dht.read(function (a) {
var current = {
temperature: a.temp - 1,
air_humidity: a.rh,
soil_humidity: readSoilSync(),
light: Math.round(analogRead(C3) * 100),
vref: E.getAnalogVRef()
};
if (debug)
console.log(current);
for (var prop in current) {
var pastData = pastValues[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, was: " + current[prop]);
//set to average if value is 20% higher or lower
current[prop] = average;
}
pastData[pastData.length] = current[prop];
if (pastData.length > 10) { //store max 10 past values
pastData = pastData.slice(1);
}
pastValues[prop] = pastData;
}
var data = [{
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: "vref",
current_value: current.vref
}, {
id: "light",
current_value: current.light
}];
putXively(data);
});
}
});
}, 10);
}
function putXively(data) {
content = JSON.stringify({
version: "1.0.0",
datastreams: data
});
var options = {
host: 'api.xively.com',
port: '80',
path: '/v2/feeds/1848857974',
method: 'PUT',
headers: {
"X-ApiKey": "xxx",
"Content-Length": content.length
}
};
var req = require("http").request(options, function (res) {
res.on('data', function (data) {
if (debug)
console.log("-->" + data);
});
res.on('close', function (data) {
if (debug)
console.log("==> Closed.");
// finished, disconnect anyway
setTimeout(function () {
clearTimeout(failTimeout);
if (debug)
console.log("Complete, disconnecting...");
wlan.disconnect();
}, 1000);
});
});
req.end(content);
}
//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