Created
December 26, 2019 10:35
-
-
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.
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
/* | |
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