Nextion TFT with Touch Screen

After getting the the MMDVM up and running for access to D-Star and DMR Brandmeister networks I experimented with the touch screen of the Nextion TFT display upon request of DL5BQ. The idea was to have a standalone unit that can for example be shut down with a single key press. Currently this is realized with a push button and a Python script (see [1]).

But as we added a Nextion display with touch screen to the Raspberry Pi running the MMDVMHost the idea was of course to use the touch screen for input. The option are for example to shut the RPi down or to link to a specific reflector with a press on the button of the display. This would also save input pins and instead use the wiring to the display which is there anyway.

Nextion TFT with Button on Touch Screen
Nextion TFT with Button on Touch Screen

After fiddling around with the Nextion editor I found at least a little explanation of how it works. You can put buttons on the display that trigger display internal events like changing the text of a textbox. But as we wanted to trigger some action on the host system this was not the way to go because that does not produce any output on the serial line.

The point of the matter was to tick the checkbox “Send Component ID” which gives a few bytes of binary data output on the serial port. In hex it looks something like “0x65000401ffffff”. 0x65 seems to be the inital byte and is followed by a byte representing the page ID, a byte for the component ID and 0x01 for “button pressed” or 0x00 for “button released”. The 0xffffff is a end of data indicator that i needed for writing to the display as well.

So all that was left to do is to have a little script or program that reads from the display and triggers the corresponding action. As a quick and dirty solution I wrote a little shell script that links the ircddbgateway to a specific reflector. The source is here:

#!/bin/bash

PORT=/dev/ttyUSB1
# Serial port the Nextion display is connected to

stty ispeed 9600 cs8 -crtscts < ${PORT}

while [ 1 ];
do
    # Key press gives:   65 00 04 01 ffffff
    # Key release gives: 65 00 04 00 ffffff
    #                       |  |   | |
    #                       |  |   | +- EOT 3x 0xFF
    #                       |  |   +--- Key press or release
    #                       |  |        01 = press
    #                       |  |        00 = release
    #                       |  +------- Component ID
    #                       +---------- Page ID

    READ=`dd status=none if=${PORT} ibs=1 count=7 | xxd -ps`
    if [ ${#READ} -eq 14 ]; then
       if [ ${READ:7:1} == "1" ]; then
          echo "Button pressed. Page ID: ${READ:2:2} Component ID: ${READ:4:2}"
          if [ "${READ:4:2}" == "04" ]; then
             remotecontrold "DF2ET  B" link never "XRF518 C"
          fi
       elif [ ${READ:7:1} == "0" ]; then
          echo "Button released"
       fi
    fi
done

It displays page and component ID and should be easily extensible for other purposes. Just add some more ifs. A little demo video is available on YouTube (see [2]).

References

[1] http://dokuwiki.ernix.de/doku.php?id=hamradio:digitalradio:pi_mit_taster_herunterfahren
[2] https://www.youtube.com/watch?v=xChYZks0qy0