Introduction: IoT RPi LED Message Board
In this Instructable, I have made a wifi-connected LED message board using a Raspberry Pi (RPi). Users will connect to the Raspberry Pi’s webserver using their browsers to submit short messages that will show up on the 8x8 LED display. Since interfacing 8x8 LED matrix with the MAX7219 driver in Python is well-documented by others on Internet, this project focuses on building a webserver interface and using ZeroMQ Messaging to manage incoming messages.
Update: Here is a follow-up project IoT Decimal/Hexadecimal 8x8 LED Matrix Drawing Board
(I am a club facilitator for Girls Who Club and I came up with this simple project to teach students on web interface design and messaging.)
Step 1: Hardware Setup
This project requires following hardware:
- Raspberry Pi
- USB power source such as Anker with a short USB to MicroUSB cable
- MAX7219 dot matrix module with a ribbon cable (Aliexpress for less than $2)
- Enclosure (I made one out of a cardboard box and spray painted in black)
Hardware setup is the easy part. Simply connect a 5 wire ribbon cable from the LED matrix to the RPi per MAX7219 library doc.
VCC->GPIO Pin #2 (5v)
GND->GPIO Pin #6 (GND)
DIN->GPIO Pin #19
CS ->GPIO Pin #24
CLK->GPIO Pin #23
I used a double-sided foam tape to affix the LED unit to the RPi case. Then, I made an enclosure out of a cardboard box to house both RPi and battery.
Step 2: Software Setup
RPi should have following software:
- Python 3
- Apache 2 websever
- Max7219 driver for Python
- ZeroMQ Messaging
RPi should have Python 3 already pre-installed. While my code is written for Python 3, Python 2 should work with few minor changes.
Setup Apache and enable Python CGI scripting. Below are couple great resources for setting up Apache on RPi so I won’t repeat here. Just follow tutorials below to setup Apache and CGI. Make sure *.py scripts are executable from a browser.
Install the Max7219 driver by following the latest install guide:
After the install, run the example code, matrix_test.py, per install guide to display "Hello World" on the LED Matrix. This has to work before going to the next step.
Why do we need Messaging? Try running the above example code, matrix_test.py, on two terminal screens simultaneously. The system will allow multiple codes to run simultaneously but you’ll see messages being overlapped which is not desirable. In a single user environment, this may not be an issue since you could make sure that only one program could run at a time. In a multi-user environment such as web, the system has to create a FIFO (First-In-First-Out) queue to make sure only one person could execute the code while others wait. While there may be other solutions to achieve this, I decided to use ZeroMQ to manage the FIFO queue. The ZeroMQ server code contains the actual function call to display a message on the LED matrix one at a time while the webserver act as a ZeroMQ client to inquire and submit messages to the ZeroMQ server. This way, while multiple users could submit messages via a web page simultaneously, the ZeroMQ server will only display one message at a time.
For this project, we’ll just install the Python package pyzmq and not the whole ZeroMQ package.
sudo pip3 install pyzmq
Read the ZeroMQ guide on http://zguide.zeromq.org and try out the the hello world server and client example in Python. Copy the Python example code for both server and client to RPi and make sure they work before going to the next step.
Step 3: Web Page Setup
In the web page, I used the bootstrap css/js framework to make the page look pretty. This is totally optional.
Download the attached led_msg.tar.gz file to the Apache root or sub-directory. To untar the gzip'd tar file, run:
tar -xzvf led_msg.tar.gz
This creates following files:
msg.py (main program) templates/interstitial.html (html template) templates/send_msg.html (html template)
static/img/led_150x150.jpg (jpg used in html template)
Optionally, install the bootstrap css/js framework under the static directory.
Enter the URL for msg.py your browser and make sure the web page comes up. Don't submit a message yet!!!
Before messages could be submitted, the ZeroMQ server must be started to accept messages from the webpage client and display them on the LED matrix. Nothing will display on the screen if the ZeroMQ server is not running.
Download the attached max7219_server.py code to your home directory, not in Apache root dir where it could be executed by web users. Run it as root:
sudo python max7219_server.py
Now the ZeroMQ server is ready to receive messages from the web page. Enter and submit a simple message from the web page. If everything is setup correctly, you’ll see that message on the ZeroMQ server screen as well as on the LED Matrix.
If you want to shutdown the server, just do Control-C to exit the server screen.
That’s it. Hope you’ll enjoy this project as much as I did.
One enhancement you could make is to make the ZeroMQ communication between the server and clients asynchronous so that the webpage doesn’t wait while other messages are being displayed. Also, you could attach additional LED matrix in cascade mode. I’ll leave that up to you.