|
Internet-of-Things RGB LED Matrix via Sockets Branch: refs/heads/dev, commit: fb7e0b9d457c5ab73c20c9ef93001c922ac5d2d1
Animation generation scripts
|

This project in the continuation of the IoT RGB LED Matrix. Socket communication is used, instead of Node-RED/MQTT, for a much faster update rate, allowing to display remotely generated animations. The Adafruit LED Matrix library is used to take advantage of its incredibly optimized display update routine.
The ESP32 acts as the socket server, anything that is recieved from a client is directly read to the screen buffer. Therefore, the data packing must be done on the client side. Having the ESP32 as the server and the Python side the client allows to send data only when needed - when screen needs to be updated with new data.
Because of the way the data is handled in the Adafruit LED Matrix library, only the 3 most significant bits (MSB) are used for the display, allowing only 8 shades per color channel, or 8^3=512 colors. Therefore, when creating animations, one must keep in mind that only the 3 MSB are actually relevant.
All code, animations and options are extensively documented using Doxygen.
Having the client written in Python makes it straight forward to create generated animations using standard libraries and image processing tools, such as Numpy and OpenCV, and link them to various APIs (weather data, mail service...) and display them quickly.
To display an animation on the matrix call the following command
client/main.py $ANIMATION [OPTIONS] display $HOST_IP
For example
client/main.py analog_clock.AnalogClock --timezone Asia/Tokyo display 192.168.3.15
| Animation | Preview | Command |
|---|---|---|
| Analog clock animation | | analog_clock.AnalogClock -t Asia/Tokyo |
| Conway's Game of Life in color | | game_of_life.GameOfLifeColor |
| Conway's Game of Life fast | | game_of_life.GameOfLifeFast |
| Digital data display | | digital_data.DigitalData -t Asia/Tokyo -c Tokyo -k $OWM_API_KEY |
| Generated fire animation | | fire.Fire |
| English word clock | | word_clock.English -t Asia/Tokyo |
| Japanese word clock | | word_clock.Japanese -t Asia/Tokyo |
| Growing tree maze generator | | rgb.GrowingTree |
| Hilbert curve animation | | rgb.HilbertCurve |
| Mandelbrot fractal animation | | rgb.Mandelbrot |
| Static QR code | | qr_code.QRCode --text "test" |
| Generated water drop animation | | water.Water |
| Generated falling snow animation | | snow.Snow |
| Matrix rain animation | | matrix.Matrix --text "test" |
An ESP32 breakout board was used in this project with a custom adapter PCB. Although the adapter is not strictly necessary as the ESP32 breakout can be connected to the RGB LED matrix with some cables just as well.
The pin assignment is not strict and every pin can be reconfigured to another one, but it is recommended to follow the pinout below so it would work with the unmodified code. The only requirement is to have 13 GPIO available.
| Pin | GPIO | Reference |
|---|---|---|
| OE | 13 | |
| CLK | 14 | |
| LAT | 15 | |
| CH_A | 26 | |
| CH_B | 4 | |
| CH_C | 27 | |
| CH_D | 2 | |
| R1 | 5 | |
| G1 | 17 | |
| BL1 | 18 | |
| R2 | 19 | |
| G2 | 16 | |
| BL2 | 25 |
The ESP32 is programmed via Arduino IDE and needs the following libraries
The ESP32 needs to have its firmware uploaded before it can recieve the data stream from the client (Python script). The client must be on the same network and be provided with the IP of the ESP32.
The firmware upload can be done using Arduino IDE together with the ESP32 board definitions.
The board must be configured as follows from the Tools menu in Arduino IDE:
The WiFi credentials need to be entered once using the WiFi access point created by the ESP32 (named ESPLEDMATRIX-XXXXXX), from the address http://192.168.4.1. Once connected to the local WiFi the access point no longer works.

Upon a successful connection to the local WiFi network the matrix displays its IP address.
Once the firmware is uploaded the ESP32 can be reprogrammed using over-the-air (OTA) updating by selecting the new port from Tools, the password is the same as the module name (ESPLEDMATRIX-XXXXXX).
The fonts used in this project are property of their respective creators. As they are free to be redistributed they are also bundeled with this project, but they have their own licenses distinct from this project.
The main.py file, in the client directory, imports all the example animations and allows to configure and launch them. This is the beginning of all execution.
The -h flag displays how to use it.
client/main.py -h
Some animations have extra arguments, such as the time zone for the clocks. See the usage for specific animations in the examples section.
To display the animation on an RGB LED Matrix add the display suffix with the IP of the ESP32.
client/main.py $ANIMATION display $HOST_IP
The display can have its maximum current limited, by setting the --current argument in Amperes. This will automatically dim the LEDs, before sending the data to the matrix, if the estimated current exceeds the set value. The LED current consumption was measured for each of the 8 intensity levels and each color separately.

To limit the current add the --current, or -c, argument to the call.
client/main.py $ANIMATION display $HOST_IP -c 1.2
To save an animation to a file add the save suffix with the number of frames to save. One frame will result in a static png, more will be saved as a gif. The image file name will be $ANIMATION.png/gif.
client/main.py $ANIMATION save $FRAMES
The saving script acts as a virtual display, decoding the encoded frame as the actual display would. Therefore, as with the real display, only the 3 MSB of the color are relevant.
An effective way to run the client side is to use Docker. This is a prepared and tested environment where all the dependencies are already installed.
The Docker image was specifically prepared to be (also) compatible with arm32v7 architecture, so it would run on a SOC such as a Raspberry Pi.
docker build -t iot_rgb_led_matrix .
This command will run the AnalogClock animation by default, other parameters are available (check the Dockerfile).
docker run -it --rm -e HOST_IP=192.168.3.15 iot_rgb_led_matrix
Docker compose allows to configure and run the script, in the Docker container, in the background (-d flag for the detach) as well as make sure the Docker instance restarts upon reboot. See the docker-compose.yml file for details.
docker-compose up -d
To stop the docker container call the following
docker-compose down
This project is published under the [MIT license](LICENSE).