Skip to content

Instantly share code, notes, and snippets.

@joestrong
Created December 4, 2018 20:46
Show Gist options
  • Save joestrong/176b669990eda651235e2bb3e0fff5bd to your computer and use it in GitHub Desktop.
Save joestrong/176b669990eda651235e2bb3e0fff5bd to your computer and use it in GitHub Desktop.
Hue Lights from GPIO switch and LED
const gpio = require('rpi-gpio')
const gpiop = gpio.promise
const { HueApi, lightState } = require('node-hue-api')
const hue = new HueApi('##IP##', '##USERNAME##')
const lights = {
bedroom: 1,
bathroom: 2,
lounge: 3,
loungeLamp: 4
}
const lightSettings = {
off: lightState.create().off(),
on: lightState.create().on().white(500, 100)
}
let buttonPushed = false
let buttonToggle = false
gpio.on('change', function(channel, value) {
if (channel !== 37) {
return
}
if (buttonPushed !== value) {
buttonToggle = !buttonToggle
updateLight()
}
});
gpio.setup(37, gpio.DIR_IN, gpio.EDGE_BOTH);
gpiop.setup(7, gpio.DIR_OUT)
.then(() => {
return updateLight()
})
.catch((err) => {
console.log('Error: ', err.toString())
})
async function updateLight() {
try {
const lightSetting = buttonToggle ? lightSettings.on : lightSettings.off
await hue.setLightState(lights.lounge, lightSetting)
} catch (e) {
console.log(e)
}
await gpiop.write(7, buttonToggle)
return
}
{
"name": "hue-lights",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"node-hue-api": "^2.4.4",
"rpi-gpio": "^2.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment