Created
August 11, 2017 15:04
-
-
Save justinedelson/fc88febb68aa4d929e2bb558da5172b6 to your computer and use it in GitHub Desktop.
motion sensor to activate Pi touchscreen (or really any DPMS screen)
This file contains hidden or 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
{ | |
"host" : "XXX", | |
"port" : 1883, | |
"username" : "XXX", | |
"password" : "XXX", | |
"presenceTopic" : "sensor/XXX/presence", | |
"screenTopic" : "sensor/XXX/screen" | |
} |
This file contains hidden or 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 gpio = require('rpi-gpio'), | |
mqtt = require('mqtt'), | |
config = require("./config.json"); | |
execSync = require('child_process').execSync; | |
var pin = 11; | |
var client = mqtt.connect(config); | |
client.on("connect", function () { | |
gpio.setup(pin, gpio.DIR_IN, gpio.EDGE_BOTH, function() { | |
var currentPinState = false; | |
gpio.read(pin, function(err, value) { | |
setPinState(value); | |
}); | |
function updateTopic(topic, value) { | |
client.publish(topic, value ? "ON" : "OFF"); | |
} | |
function setPinState(value) { | |
console.log('Value is now ' + value + ' @ ' + new Date()); | |
currentPinState = value; | |
updateTopic(config.presenceTopic, currentPinState); | |
} | |
function turnOnMonitor() { | |
execSync("sudo -u pi xset dpms force on -display :0.0"); | |
} | |
gpio.on('change', function(channel, value) { | |
if (channel == pin) { | |
setPinState(value); | |
turnOnMonitor(); | |
} | |
}); | |
function checkDpms() { | |
var monitorState = execSync("sudo -u pi xset q -display :0.0").indexOf("Monitor is On") > -1; | |
updateTopic(config.screenTopic, monitorState); | |
if (!monitorState && currentPinState) { | |
console.log("Monitor is off, but pin is on; turning on."); | |
turnOnMonitor(); | |
} | |
} | |
setInterval(checkDpms, 500); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment