Maybe you read Part 1, maybe you skipped it
Either way Part 2 is going to cover creating a new effect for Nagios. I wont say it’s the best bit of python programming I’ve done (Yes I copied and changed another effect as a starting point). The main thing I wanted was an alert status (yes Star Trek does come into it).
There’s 4 files to this effect:
- alert.py – The actual python program
- alert-green.json – etting a green colour (yes it’s spelt COLOUR not color!!!!!)
- alert-yellow.json
- alert-red.json
My hyperion is installed to ‘/opt/hyperion’ so first thing is to go to the effects directory
1 | cd /opt/hyperion/effects |
Then create the alert.py file
1 | nano alert.py |
Paste the following into it and save the file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import hyperion import time import colorsys import numpy as np # Get the rotation time colorfrom = hyperion.args.get( 'colorfrom' , ( 0 , 0 , 0 )) colorto = hyperion.args.get( 'colorto' , ( 0 , 255 , 0 )) speed = float (hyperion.args.get( 'speed' , 0.05 )) freq = float (hyperion.args.get( 'frequency' , 2.5 )) step = float (hyperion.args.get( 'step' , 30.0 )) ledCount = hyperion.ledCount # Setup the colors colorfromnp = np.array(colorfrom) colortonp = np.array(colorto) #define the lerp calc def lerp(a, b, t): x = a * ( 1 - t) + b * t x = np.around(x) x = x.tolist() return x # Start the write data loop while not hyperion.abort(): for i in range ( 0 , int (step)): n = i / step thiscolor = lerp(colorfromnp, colortonp, n) colorLedsData = ledCount * bytearray(( int (thiscolor[ 0 ]), int (thiscolor[ 1 ]), int (thiscolor[ 2 ]))) hyperion.setColor(colorLedsData) time.sleep(speed) time.sleep(speed * 10 ) for i in range ( 1 , int (step)): n = i / step thiscolor = lerp(colortonp, colorfromnp, n) colorLedsData = ledCount * bytearray(( int (thiscolor[ 0 ]), int (thiscolor[ 1 ]), int (thiscolor[ 2 ]))) hyperion.setColor(colorLedsData) time.sleep(speed) colorLedsData = ledCount * bytearray(colorfrom) hyperion.setColor(colorLedsData) time.sleep(freq) |
I do use a numpy array, I can’t remember installing it but may have done. You can test if you have it installed
1 2 | python import numpy as np |
Then we create the alert-green.json
1 | nano alert-green.json |
And Paste
1 2 3 4 5 6 7 8 9 10 11 12 | { "name" : "Green Alert" , "script" : "alert.py" , "args" : { "colorfrom" : [ 0 , 0 , 0 ], "colorto" : [ 0 , 255 , 0 ], "speed" : 0.05 , "freq" : 2.5 , "step" : 30.0 } } |
alert-yellow.json
1 2 3 4 5 6 7 8 9 10 11 12 | { "name" : "Yellow Alert" , "script" : "alert.py" , "args" : { "colorfrom" : [ 0 , 0 , 0 ], "colorto" : [ 255 , 255 , 0 ], "speed" : 0.05 , "freq" : 2.5 , "step" : 30.0 } } |
alert-red.json
1 2 3 4 5 6 7 8 9 10 11 12 | { "name" : "Red Alert" , "script" : "alert.py" , "args" : { "colorfrom" : [ 0 , 0 , 0 ], "colorto" : [ 255 , 0 , 0 ], "speed" : 0.05 , "freq" : 2.5 , "step" : 30.0 } } |
Finally restart hyperion to pickup the new effects
1 | service hyperion restart |
That’s it, you should now be able to run these effects. From the terminal:
1 | hyperion-remote -e "Red Alert" |
Should start the led’s glowing Red. If not I’d start with ‘hyperion-remote -l’ to check the effects are listed, then move on to working out what’s missing (see above about numpy array).
A few notes on this effect:
- Originally I programmed it forced to black, but it made sense to change this to a colorfrom value for future (if you want it green and fading to red for example).
- The speed is how quickly to progress through the fade.
- The step is how many colours it goes through to get from the colorfrom to the colorto
- Working out the speed and the step together are important for a nice fade (not jerky).
- The Freq in how long it stays on black before running the fade again.
This completes the hyperion side of the setup. I now have this effect installed and running on 3 PI’s and yes when there’s a problem they ALL go to yellow/red alert
2 thoughts on “Hyperion LED’s & Nagios (Part 2)”