Skip to content

Instantly share code, notes, and snippets.

@garaboncias
Last active August 15, 2018 07:35
Show Gist options
  • Save garaboncias/09b6bdb4295cc0614ed701af06ef2a6b to your computer and use it in GitHub Desktop.
Save garaboncias/09b6bdb4295cc0614ed701af06ef2a6b to your computer and use it in GitHub Desktop.
Thingy:52 ventilation needed alarm js
/**
# CO2
//https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms
//250-350ppm Normal background concentration in outdoor ambient air
//350-1,000ppm Concentrations typical of occupied indoor spaces with good air exchange
//1,000-2,000ppm Complaints of drowsiness and poor air.
//2,000-5,000 ppm Headaches, sleepiness and stagnant, stale, stuffy air. Poor concentration, loss of attention, increased heart rate and slight nausea may also be present.
# TVOC
https://easlab.com/iaqref.htm
*/
reset();
const YELLOW = 0xFFFF00;
const RED = 0xFF0000;
const GREEN = 0x00FF00;
const BLUE = 0x0000FF;
const MINUTE = 1000 * 60 * 60;
function blink(color, count) {
LED1.write((color & 0xFF0000) >> 16);
LED2.write((color & 0x00FF00) >> 8);
LED3.write(color & 0x0000FF);
setTimeout(function () {
LED1.write(0);
LED2.write(0);
LED3.write(0);
if (count-- > 0) {
setTimeout(() => blink(color, count), 150);
}
}, 100);
}
let gasCheckInterval = 10000;
let batteryCheckInterval = MINUTE;
let gasPrev = Date.now();
let batteryPrev = Date.now();
let feedback;
Thingy.onGas(function (gas) {
let now = Date.now();
if (now - gasPrev > gasCheckInterval) {
gasPrev = now;
if (gas.eCO2 > 1000 && gas.eCO2 < 2000 || gas.TVOC > 300 && gas.TVOC < 500) {
blink(YELLOW);
} else if (gas.eCO2 > 2000 || gas.TVOC > 500) {
blink(RED);
} else {
//blink(GREEN);
}
}
if (now - batteryPrev > batteryCheckInterval) {
batteryPrev = now;
Thingy.getBattery(function (battery) {
let percentage = Math.round(100 / 0.8 * (battery.voltage - 3));
console.log('battery', battery.charging, battery.voltage, percentage + '%');
if (battery.charging) {
batteryCheckInterval = MINUTE;
if (percentage < 10) {
blink(RED, 2);
} else if (percentage >= 10 && percentage < 90) {
blink(YELLOW, 2);
} else {
blink(GREEN, 2);
}
} else if (percentage < 10) {
blink(RED, 2);
batteryCheckInterval = MINUTE;
} else {
batteryCheckInterval = 10 * MINUTE;
}
});
}
console.log(new Date().toISOString().substr(11, 8), 'CO2:', gas.eCO2, 'TVOC:', gas.TVOC);
if (feedback) {
feedback = false;
console.log('feedback', new Date().toISOString().substr(11, 8), 'CO2:', gas.eCO2, 'TVOC:', gas.TVOC);
blink(YELLOW, Math.round(gas.eCO2 / 500));
let battery = Thingy.getBattery();
let percentage = Math.round(100 / 0.8 * (battery.voltage - 3));
console.log('battery', battery.charging, battery.voltage, percentage + '%');
//blink(BLUE, Math.round(percentage / 10));
}
});
setWatch(function () {
console.log("Pressed");
feedback = true;
}, BTN, { edge: "rising", debounce: 50, repeat: true });
//Thingy.onGas()
//{ charging : bool, voltage : number }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment