Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Created August 28, 2020 15: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 coderbyheart/94323c67eceb40774252363641f16689 to your computer and use it in GitHub Desktop.
Save coderbyheart/94323c67eceb40774252363641f16689 to your computer and use it in GitHub Desktop.
espruino temperature controlled relay
var dht = require("DHT22").connect(A1);
const threshold = 25;
const interval = 10;
let currentTemp = 0;
// LEDs off
LED1.write(false);
LED2.write(false);
// LED error blink
let l1 = false;
let errorInterval;
var on = false;
function setState(isOn) {
if (on !== isOn) {
if (isOn) {
console.log("Turning on...");
} else {
console.log("Turning off...");
}
}
on = isOn;
LED1.write(on);
digitalWrite(A0, !on);
LED2.write(!on);
}
function turnOn() {
setState(true);
}
function turnOff() {
setState(false);
}
function checkTemp() {
dht.read(function (a) {
if (a.temp === -1) {
console.log("Read error");
// Just in case
turnOn();
if (errorInterval === undefined) {
errorInterval = setInterval("digitalWrite(LED1,l1=!l1);",200);
}
return;
}
if (errorInterval !== undefined) {
clearInterval(errorInterval);
errorInterval = undefined;
}
console.log("Temp is ", a.temp);
currentTemp = a.temp;
if (a.temp > threshold) {
turnOn();
} else {
turnOff();
}
});
}
setInterval(checkTemp, 1000 * interval);
checkTemp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment