Skip to content

Instantly share code, notes, and snippets.

@xettri
Created December 26, 2019 10:35
Show Gist options
  • Save xettri/400a4b118d01a7106ef65d3c239e7480 to your computer and use it in GitHub Desktop.
Save xettri/400a4b118d01a7106ef65d3c239e7480 to your computer and use it in GitHub Desktop.
Pin - 21 for gpio input and pin 20 is for gpio output if moisture detected.
/*
Install rpi-gpio using
npm install rpi-gpio -g
*/
const gpio = require('rpi-gpio');
var pin = 21;
var pin2 = 20;
gpio.setMode(gpio.MODE_BCM);
gpio.setup(pin, gpio.DIR_IN);
gpio.setup(pin2, gpio.DIR_OUT);
function output(status) {
var mode = (status === false) ? 1 : 0;
gpio.write(pin2, mode, function(err) {
if (err) throw err;
var msg;
if(status === false){
msg = "Moisture detected";
} else {
msg = "Moisture not detected";
}
console.log(msg);
});
}
function test() {
gpio.read(pin, (err, data) => {
if (err) throw err;
return output(data);
});
}
setInterval(function () { test() }, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment