Making A 32x32 Led Ticker Thing with FlashLex

May 04, 2019

pr_tgsjjaiiw

in the wild

I always wanted a way to stream information to a personal dashboard. The idea of being able to send any information I could hack together to single informational display was always something I wanted to make. I also always have loved the old school look of LED displays. This project mashes up great online projects on the internet with flashlex to make a personal informational dashboard using Raspberry PI, a 32x32 LED matrix, and FlashLex.

This project provides the following services for your dashboard.

  • Clock
  • Current Weather, Forcasted for your zipcode.
  • Whatever you can dream up!

Solution Architecture

solution architecture

Important Links and Shoutouts

  • Adafruit RGB Matrix + Real Time Clock HAT for Raspberry Pi - This is an essential guide for this project. It shows you how to build your LED based display platform.
  • rpi-rgb-led-matrix - This is the core matrix driver software which gets installed when you setup your LED Matrix with the Adafruit guide.Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi
  • ledticker-pi - Led ticker is a python based AWS cloud serverless application that sends data to your thing. It could easily be used as an example on how to build any messaging backend that could send data to your matrix.
  • Getting Started With RGB Matrix Panel - Tutorial on can how to create animations, games or all sorts of other fun displays with them.
  • ledticker-backend - To send messages to your thing a backend will run an application that will get data off the internet and send it to your thing to be displayed.

Required Hardware

The LED Thing

Your Backend

The Cookbook

This cookbook assumes that you have started up a Raspberry Pi and know how to build and isntall bonnets and hats. There are things that you will need to know how to do and some things that this guide will show you how to do.

What we dont cover in this guide: These steps are either referenced above or you can find a howto or guide easily on the internet.

  1. Building the Raspian Flash an Startup your Raspberry Pi 3B+ - Installing Images on a Raspberry PI - Using Etcher
  2. Getting your Raspberry Pi updated and running Python 3 - Installing Python 3 on a Raspberry Pi
  3. Assembling the Adafruit RGB Matrix HAT + RTC for Raspberry Pi - Adafruit RGB Matrix + Real Time Clock HAT for Raspberry Pi
  4. Assembling and Adding the LED Matrix - Getting Started With RGB Matrix Panel - rpi-rgb-led-matrix

What this guide shows you how to do:
This is what the guide is intended to show you how to do.

  1. Provision your thing on FlashLex
  2. Download, Install and Test the FlashLex SDK Distribution on your LED Thing Raspberry Pi
  3. Installing and Testing the the LedTicker runtime on your LED Thing Raspberry Pi
  4. Setting up your messaging applciation on your Backend Raspberry Pi

Provisioning Your Thing on FlashLex

Start by using FlashLex to provision a new thing. The docs have a complete guide on how to provision new things for FlashLex. This will build the secure cloud infrastructure for your thing.

Working With the FlashLex SDK Distribution

You will need to download, install and test the SDK distribution for your thing to recieve the messages that will be displayed on the LED. The distribution is open source software that you that allows you to communciate with the FlashLex API using an Software Development Kit when used in conjunction with your FlashLex certificates.

You can see all the source code used for this distribution on github.com, and we welcome involvement from the IOT community.

Be sure to read the entire guide on using the FlashLex distribution to understand how to use it. Follow the guide in our help docs for complete instructions

Installing and Testing the the LedTicker Runtime

Allows a raspberry pi to drive an LED display as an IOT divide in AWS. The idea is to make a IOT thing that is basically just a slave to the messages sent to a queue. This allows the device to be dumb and multiple suppliers to be as sophisticated as needed.

The ledticker-pi project allows your thing to use FlashLex to load messages from the local database as a cache and display them as they arrive. You can see the ledticker-pi project page on github.com

These are the guides that accelerated building this project extensively.

First install flashlex. You will need to install FlashLex in the virtualenv of the Led Ticker app.

This is a flashlex project. That is it uses the flashlex cloud IOT framework to send messages to the Raspberry Pi. You can get more info on flashlex at flashlex.com

source venv/bin/activate #activate your virtual env
cd .. # goto the parent of the ledticker project
git clone https://github.com/claytantor/flashlex-iot-python.git
cd flashlex-iot-python # go into the flashlex distribution
sudo python setup.py install # install the sdk

How to Display a Message To the LED using GPIO and Python

You will want to extend your class from SampleBase which contains the RGBMatrix bindings that will let you map a display buffer to the LED Matrix.

The implemented display class would then implement a run that would let you manage sending data to the display buffer:

class LedDisplay(SampleBase):  

    def __init__(self, *args, **kwargs):
        super(LedDisplay, self).__init__(*args, **kwargs)
        self.parser.add_argument('--log', type=str, default="INFO", required=False,
                        help='which log level. DEBUG, INFO, WARNING, CRITICAL')
        self.parser.add_argument('--config', type=str, required=True, default='config.yml',
                        help='the name of the configuration section to use.')

    def get_messages(self, config):
            print("getting messages")
            sdk = FlashlexSDK(config)
            messages = sdk.getSubscribedMessages()
            for message in messages:
                yield message
                sdk.removeMessageFromStore(message)

An example of displaying fixed text using this pattern:

class FixedText(LedText):

    def __init__(self, runner, message):
        super(FixedText, self).__init__(runner, message)
        self.name = "Fixed Text"

    @staticmethod
    def matches(message):
        if 'body' in message and 'behavior' in message and message['behavior'] == 'fixed' and message['type'].startswith('text'):
            return True
        else:
            return False

    def display(self):

        offscreen_canvas = self.runner.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont(self.get_font_path(self.message))

        textColor = self.get_text_color(self.message)

        len = graphics.DrawText(offscreen_canvas, font, 0, 10, textColor, '{0}'.format(self.message['body']))

        pos = math.ceil((offscreen_canvas.width-len)/2.0)
        height = math.ceil(offscreen_canvas.width/2.0)+math.ceil((font.height-2)/2.0)
        # print("height: {0} font.height: {1}".format(height, font.height))

        start_time = time.time()
        elapsed_time = time.time() - start_time
        elapsed_time_boundry = 10.0
        if 'elapsed' in self.message:
            elapsed_time_boundry = self.message['elapsed']

        while elapsed_time < elapsed_time_boundry:
            #print time.time(),start_time
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, height, textColor, '{0}'.format(self.message['body']))
            time.sleep(1)
            elapsed_time = time.time() - start_time
            offscreen_canvas = self.runner.matrix.SwapOnVSync(offscreen_canvas)

        offscreen_canvas.Clear()
        offscreen_canvas = self.runner.matrix.SwapOnVSync(offscreen_canvas)

The next step is to run the program on the command line. To run the LED Hat the system requires that to run as sudo.

sudo /home/pi/projects/ledticker-pi/venv/bin/python -u /home/pi/projects/ledticker-pi/ledticker.py --led-no-hardware-pulse true -m adafruit-hat -r 32 --led-cols 32 --log DEBUG --config /home/pi/projects/ledticker-pi/config.yml 

Your thing should now be ready to recieve messages.

Sending a test message to the display

There are two ways to send a message to your thing.

  1. Use the FlashLex message test form for your thing
  2. Use the FlashLex Cloud API.
curl -X POST \
  https://apis.flashlex.com/dev/v1/things/48338542-9866-4046-9ecc-ad74e1807cc5/publish \
  -H 'Authorization: Bearer eyJraWQiOiJQTytrSGI5RXB2QjVRSFlka2duWWtRUmtjazhQSVhkakNaUGtFMnJGUFdRPSIsImFsZyI6IlJTMjU2In0' \
  -H 'Content-Type: application/json' \
  -d '{"body": "Clouds|55|F", "color": "#3366ff", "type": "weather", "behavior": "current", "elapsed": 20.0}'