Wednesday, 22 June 2022

LilyGo T-Display Keypad


Rationale

Some years ago I setup a home music server based on Linux Music Player Daemon (MPD) with a web front-end to select and control music playing on my HiFi separates system.  Typically I would choose music to play using  my iPad or phone browser.
As an add-on I developed an ESP8266 attached keypad which also allowed me to control the music.  I call it my "Button" although it has a number of buttons/functions.  It is particularly useful for pause / resume, next type functions and to start my favourite playlists and radio channels.  Effectively it is a clever remote control tailored to suit my needs.
The original "Button" version 1 has a USB cable to power the ESP8266 so it isn't very portable; typically this doesn't matter but I did develop version 2 which has a rechargeable battery.  I found this cumbersome and I haven't used it much.  Both Buttons have a tiny LCD screen to display status info but this isn't very visible as I have had difficulty creating suitable enclosures to accomodate ESP8266/keypad/displays.


Recently I have implemented my Home Assistant (HA) Server, including MPD for music server control and Arduino based MQTT communication.  This allows me to control my HiFi from an ESP device and I added it to my "Button" software for testing purposes and it works fine. It works in parallel to my original application and has the potential for more functionality / flexibility.

In advance of doing more work on the button I looked out for a case for an ESP device and keypad which would be more compact than my home-made devices.  I was excited to stumble on the LilyGo T-Display Keypad on Aliexpress.
It is based on the ESP32, has a mechanical keyboard, a small TFT display and a rechargeable battery all in a neat case.  This is an absolutely perfect replacement for my old buttons and I ordered one immediately.

First look

Connecting the USB-C cable causes the ESP32 to start up.  A simple display appears allowing you to display wifi networks, input voltage or put the processor into a deep sleep.  There are two buttons adjacent to the TFT screen for these controls.  There is also a reset button on the top or right of the case.
Like a remote control there is no on / off switch which feels very strange to me.





The LilyGO github repository provides you with instructions to load the TFT library and example sketches into your PC Arduino development environment.
From there it is easy to compile and load the Factory_Test sketch which is the same one as installed on delivery.  This is awesome, nothing makes life easier than a working example containing many of the features of the device.  In particular it provides information on:
    All the pins needed for keypad, TFT, buttons, SD Card
    Arduino libraries for all hardware
    Example code to use the TFT display
    Example code to find the input reference voltage (useful for low battery check)
    Example to display a bitmap on the TFT
    How to put the ESP into deep sleep

In addition there are sketches for
    Example code to use the keypad
    Test SD functions to create, view, delete file and directory list.
    A rather good animated eyes sketch which makes really brings the screen to life.

This makes my life so much easier and saves me many, many hours looking at schematics, finding libraries, writing code to set up everything.

Application One

My first application requirement is clearly to setup similar functionality to the "Button".
Firstly I amended example code to read input from the keypad and display messages on the screen.  I then added wifi and websockets based on "Button 1" code.  Using a case statement I could then easily process key presses and send websocket messages to PI40 (which processes websocket input) for processing.  Within a few hours I have a working example for the "Button 3" based on the web page which I usually use.

I added and tested MQTT, using Home Assistant (HA) as my MQTT Broker.  This allows me to communicate with HA and use a HA script or automation to control any function HA function.  This is an extremely powerful extra.  On my webpage I cannot control amplifier volume since the amp is old and uses an IR remote control.  However my Broadlink IR remote is controlled by HA and I can add volume up/down buttons to LilyGo.

The LilyGo battery runs out after about 24 hours if not recharged, so I set up a button to show battery percentage / time remaining and another button to clear the screen.  This completes a basic setup which is a great improvement over my previous buttons and which I use in preference to my phone.

I cannot stress enough what a good product this is for me, both in terms of the hardware purchased and the software provided to help get started.  I shall look out for other products they can inspire me with.


     

 


Friday, 17 June 2022

Lu J Son

 Whilst on holiday, walking in the Pyrenees, we stopped for dinner at a lovely restaurant called El Montanes in Biescas.   As well as a wonderful dinner in a convivial surroundings the walls were decorated with some striking pictures.  As I was leaving I noted the pictures were signed by the artist LuJSon and I followed up on my return to England.

@Lu_J_Son is Tomas Langarita, who is from Zaragoza and may well teach there at the University. I contacted him via Instagram and found he has lot of beautiful work.  


In particular, in the restaurant I loved "8p.m.".  It is easily recognisable as lady on a sofa drinking a glass  of wine, with book shelves in the background.  There is a hint of Mondrian (who I dont like) in the white bookshelf grid.  The books are bright primary colours, neat but not regimented, random but harmonious, filling the top half of the frame.  The bottom half contains a grey sofa which is occupied by a lady, making herself comfortable with her feet up.  She is holding up a glass of good red wine which she is savouring.  Her posture, expression and mood are captured with a few straight line segments.  I think this is brilliant. I like the way that artists can capture meaning in single curves representing people or objects; this goes a step further capturing something extra from straight lines.  All the colours are outlined with black lines and it would be easy to translate the picture to stained glass fragments, joined / separated by lead joints (e.g. Sophie Taeuber-Arp work)

I have contacted Tomas and I may actually be able to buy this picture.  It would be lovely if this works.

In the meantime I have looked at more of his pictures some of which were at El Montanes....































Tuesday, 14 June 2022

HA : WLED

I purchased a WS2812B LED strip over a year ago.  The LEDs are individually addressable, allowing you to set up patterns on the lights.  I didn't get around to doing anything with them until I saw that they can be controlled using an app called WLED which in turn can be integrated with Home Assistant (HA).  I think the W in WLED stands for wifi; LEDs are connected to an ESP8266 which controls them and the WLED iPAD app allows you to configure patterns.

Arduino Setup

My first task was to setup an ESP8266 and connect the LED strip.  I used an article by mechatronics to guide me, it shows the connections are very simple.




I then loaded the example sketch, which uses the FastLED library, into ESP8266 and it works, without modification, displaying an attractive red / blue pattern on the LEDs .  Always a pleasant surprise when this happens😊

The sketch shows that displaying a pattern is simply a matter of putting correct colour values in the array leds[] and calling FastLED.show to display them.  They pattern can be made to change by adding loop to the sketch specifying how the pattern changes each time.  It gives me complete control of which LEDs are are used, what colours they are and when they change from a C program😊

WLED Install

WLED runs on an ESP8266 to control the strip.  It has a ultra-cool website URL kno.wled.ge  which provides the relevant installation instructions.  WLED is loaded into the ESP8266 using a utility esptool.py.  I downloaded esptool.py and WLED 0.13.1 to WSL then ran esptool.py to flash WLED onto the ESP8266.   ESP8266 starts up with an access point WLED-AP.  I attach to WLED-AP and go to address 4.3.2.1 in my browser to connect to the ESP program.

Now all I have to do is configure my Wifi network and password, then restart and I can access the browser app from any local network browser😊

WLED is surprisingly complicated to use; there are over 100 effects you can use to create LED patterns and a variety of colour palettes you can choose.  You can set the speed, select groups of LEDs and string a sequence of patterns together.  


Luckily there are various youtube tutorials to help you and kno.wled.ge provides a list.  I used a couple of tutorials by Dr zzs to give me the general idea.

WLED API

I dont find it easy to setup devices based on a graphical user interface, it is too easy to forget/overlook details.  WLED provides both HTTP and JSON APIs allowing you to control WLED remotely.  JSON is the newer, more complete interface which is easier to code / read so I concentrated on setting up some JSON configs.  Curl is used to send WLED API commands, as a simple example the following command turns the LED strip on.


In practice it is better to put the json into a file, the example below causes WLED to blink LEDs 30 to 40 in the strip between blue and green.
Now that I have a mechanism to program WLED I can create a variety of configs for the lights.  WLED allows you to setup presets to store configurations; once I have downloaded a configuration I save it in a preset.

HA and GA

There is a HA native integration for WLED making it trivial to include it within the system.  HA allows you to control individual elements, however I think it is easier to setup presets as shown above then invoke them within HA.



Now WLED can be controlled by running a HA script and I can set up a routing in Google Home for voice control of LEDs, job done😊