A Raspberry Pi middleware for diyHUE to control LEDs connected to GPIO
- Raspberry Pi (tested with 3b+, 4, ZeroW)
- some LEDs wired up to GPIO (google how.)
- diyHUE installed (either on this Raspberr Pi or somewhere else):
https://diyhue.readthedocs.io/en/latest/getting_started.html#automatic-install
- Raspbian Stretch or higher
- python3 with pip3 and flask
Other libraries or python modules may be required, e.g. for adafruit neopixels. Install them separately.
In a terminal, execute:
git clone https://github.com/tenbergen/hueGPIO.git
cd hueGPIO
sudo -s
sh install.sh
The service will start immediately and after reboot.
Other libraries or python modules may be required, e.g. for adafruit neopixels. Install them separately.
hueGPIO will be default run an emulated light.
To add your own GPIO LEDs or neopixels, first wire them up correctly like you normally would.
Then:
- Open
http://{diyHUE-IP-address}/milight
in your browser - Under
Hub ip
, add the IP address of where your have hueGPIO installed, followed by ":5000", as the port is 5000.
This can be the same Raspberry Pi as the one running diyHUE, in which case, you can simple enter127.0.0.1:5000
orlocalhost:5000
- Under
Device id
, enter a number in the form0x1234
- Under
Mode
, selectRGB
- Under
Group
, select1
- Click
Save
Next, edit `hueGPIO.py` as follows:
- In line 3 of the file, change
from gpio_lights import light_emulator
to something else, e.g.,
from gpio_lights import neopixel
It might be a module available in the folder gpio_lights
, however.
- Locate the 20 lines at the end of the file starting with
#SNIP
and ending with#SNAP
"
#SNIP
@app.route('/gateways/0x0001/rgb/1', methods=['PUT', 'GET'])
def light_0x0001():
name = "0x0001"
if not request.is_json:
return 'Please add this light to diyHUE: <a href="https://diyhue.readthedocs.io/en/latest/lights/milight.html">https://diyhue.readthedocs.io/en/latest/lights/milight.html</a>'
lightstate = LightState(name)
filename = path + str(name) + '.json'
if os.path.isfile(filename):
lightstate.load(filename)
changedstate = parserequest(request.get_json(), lightstate)
changedstate.name = name
changedstate.save(filename)
light_emulator.setHueColor((changedstate.red, changedstate.green, changedstate.blue), changedstate.brightness)
return jsonify(changedstate.serialize())
# SNAP
- Copy and paste everything in between
- Modify every occurrence of the number
0x0001
to the same number that you entered under Step 3 in diyHUE, e.g.,0x1234
. This should only be in the first three lines after#SNIP
:
@app.route('/gateways/0x1234/rgb/1', methods=['PUT', 'GET'])
def light_0x1234():
name = "0x1234"
- In the code you just pasted, change the line
light_emulator.setHueColor(...)
to the same that you have selected in line 3 of the file.
- Repeat these steps for any additional light.
After adding your own light, you may need to restart the service via command line. Run:
sudo systemctl restart hueGPIO.service
In principle, any GPIO device can be controlled using hueGPIO, as long as it contains a method
setHueColor((red, green, blue), brightness)
.
Reading from GPIO devices is currently not supported.
Share the love and improve this thing:
- Flask internal development server is used, which is unsafe. Upgrade to production server.
- Add support for other GPIO devices.
- Add support for reading from GPIO devices.
- LEDs don't turn off when hueGPIO is killed.
neocal - A perpetual calendar for Raspberry Pi using NeoPixel LED lights.
neotemp - An adafruit NeoPixel stick thermostat for Raspberry Pi.