A 10 leds box driven by REST API

September 30, 2017

This project was an order. A developper needed to show differents status with a collection of leds. Only using backend development.

Hardware

There is a specific hardware for this kind of project : a raspberry pi, especially a pi zero W because it is cheaper.

Leds came from a strip. The reference is ws2812, you can find it with the name "neopixel" Amazon ws2812
Connect it and install the library. There is no value to copy/paste the great documentation from Adafruit, follow this link and enjoy : Noepixel on raspberry

The box

It is a cardboard packaging painted in black, using a spray. There was only one constraint for the size of this box : a mobile batery should fits in.

When the box is closed :

Now, the job is partially done. I will not write a tutorial for using this led strip. See the examples directory in the downloaded library.

A REST API

Design

I wanted the simpliest method for a backend developer. No ssh, no python script or anything else. Time was very limited.

Make a POST request with 2 arguments : the led number and the wanted color. Let see :

                {
                    [ 
                        { 'num':1, 'r':255, 'g':255, 'b':40 }
                    ]
                }
                

Send multiple leds status is available, that's why I use an array.

Implementation

Flask is a Python framework for REST API server. http://flask.pocoo.org/ I need only a single endpoint. A second one can be implemented, a GET request that returns the status. Not needed now :).

Main function initialize the led strip :

                
                
                        if __name__ == '__main__':
                        global stip
                        LED_COUNT = 10
                        LED_PIN = 18
                        LED_FREQ_HZ = 800000
                        LED_DMA = 5
                        LED_BRIGHTNESS = 255
                        LED_INVERT = False
                        strip = Adafruit_NeoPixel( LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
                        strip.begin()
                        app.run(host='0.0.0.0', port=5555, debug=True)
                
            
        

And the single endpoint : a loop and a change color function

                        @app.route('/leds', methods=['POST'])
                        def leds():
                            content = request.get_json()
                            for led in content:
                                changeLed(led["num"], led["r"], led["g"], led["b"])
                            return "ok"
                        
                        def changeLed(num, r, g, b):
                            global strip
                            print "set color"
                            print r
                            print g
                            print b
                            strip.setPixelColor( num , Color(g, r, b) )
                            strip.show()
                

Full code : https://github.com/mathieupassenaud/ledbox

That's it !