Architecture of the Wall Light Device

The wall lamp is based on a Raspberry PI PICO as the central processor.  The following diagram shows the architecture I used for the device. 

The PICO  managed a string of 144 WS2812 LEDs to animate the blade, in just the same way that I demonstrated in my Udemy course on the C development for the PICO. The WS2812 requires 5V power and 5V signal logic, therefore a logic level converter is used for the signal logic. 

The LEDs can pull 4 amps in my test. I did give myself a heart stopping movement during breadboard prototyping with magic smoke floating across my desk. Fortunately this turned out not to be the PICO but the power wire going to the LEDs which was getting very warm carrying 4 Amps of current. I have therefore used very large trails on the PCB and appropriate wires for the connection. To safeguard the unit I included a 5 Amp fuse and thermal fuse (approximately 100C).

The PICO does not have any Wifi connectivity out of the box. I eventually settled on using ESP-01S as a wifi module. These can be purchased very cheaply but require some work to upgrade them to a more recent firmware version. The Github repo has more details on what was needed.

Local control is a switch that pulls a pin to ground, debouncing is done in software, and a PIR sensor. I chose the AM312 which is nice and small and 3.5 volts but has no sensitivity controls included. I found the AM312 to be incredibly sensitive to any electrical interference and required capacity to filter its supply.

I also used a common cathode RGB LED as a status LED. I could have used another WS2812 but didn’t have any single through hole units on hand. I also had code for managing the RGB LED written to reuse along with plenty of spare pins off the PICO.

Solution Architecture

The wall lights connect over Wifi to an MQTT Hub, EMQX. This provides the backbone of communication using publish and subscribe message architecture. For security purposes MQTT is done 

over SSL, though certificates are not checked at the moment.

The wall lights are fairly dumb devices and store no configuration in flash. Its configuration must be reloaded on start up. To handle this a digital twin service, Twin Manager, reloads the configuration (twin state) of a wall light. It also updates its cache each time the wall light notifies it of a change of state. 

The Twin Manager is a little more than an online cache. It also understands that the wall lights are a group of devices. It allows control software to operate on the wall lights as a group. So changes to configuration can be done at group level. Even if a wall light is offline the change is stored and applied as soon as the wall light connects to the network.

Using the Twin Manager the management control panel, SWL Manager, for the lights is actually very simple. It can pull back and update the state of all the lights in one go.

Software and Libraries

The wall lights firmware is written in C++. It requires some concurrency to handle the comms properly along with using both cores of the PICO. I therefore chose to use FreeRTOS to provide concurrent tasks. There are a number of other libraries that made this development significantly easier. So my thanks and praise for their work goes to:

  • Lwesp -  Interface library to ESP-01S for Wifi and MQTT protocol
  • FreeRTOS-Kernel - real time task processing
  • Json-maker - writing JSON, all payloads of my MQTT messages are in JSON
  • Tiny-json - Parsing JSON
  • PicoLED - Manage the WS2812 LEDs

A couple of C++ libraries got build as reusable components for other projects coming down the line:

The Twin Manager and SWL Manager software was written in Python. Notable libraries used include:

Read more »