Created
August 28, 2020 15:59
-
-
Save coderbyheart/94323c67eceb40774252363641f16689 to your computer and use it in GitHub Desktop.
espruino temperature controlled relay
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 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