Displaying buttons on the TFT

Having successfully hooked my small TFT up to the Raspberry Pi, I now wanted to use it! As it was a touch screen version what I wanted was buttons.

This is the end result;

TFT initial state

TFT display updated after a button press

The code is to be found in this gist:

A few comments on the false starts and things I learned (code is well commented I think):

  • I tried to install pygameUI but this failed because of the way I had implemented the TFT itself.
  • pygame is structured in a way I was unfamiliar with (fairly new to python). Key concepts are:
    • The screen (the area you update)
    • Rect – an object that defines a rectangular area of the screen.  I created an array of Rect objects then gave each a centre.  Once defined they will always display at that location unless you change it.
    • You output various layers then update the display.
    • Updating the display is best done on the rects that have changed although in this case, frame rate is not an issue.
    • I used centres as I found that trying to create a border using a top left reference did not result in a constant thickness of the border all round.

I now need to find a way to mount the TFT permanently.  I also really would like an enclosure – I’m going to hunt down a 3D one or pay a visit to my local hacklab.

I also need to expand my code to pull instructions off and send instructions to the main IOT server I have to control the heating.



Facebooktwitterby feather
Posted in Coding and Programming, Python | Tagged , , | Leave a comment

Forcing npm to Update Packages to Latest

I have had a problem getting npm to update packages to the latest version.  The packages involved are packages that have been installed directly and have no dependencies.  It it is down to the use of semantic versioning in npm / node.js but knowing that does not help!

I have a package, canvas-gauges, installed via npm, that I wanted to update.  Running

told me that;

I wanted to update it to the latest version but despite repeated searches, on how to do this, I could not force npm to update the package.

Trying these did not work;

I also tried installing and running ncu but that complains there is no package.json file.

There is nothing wrong in what npm is doing, it is following the ‘Wanted’ version even though I wanted the latest evrsion.

Ultimately the solution was simple (just not easily found);



Facebooktwitterby feather
Posted in Linux, Node-RED | Tagged | Leave a comment

Using a Small TFT with a Raspberry Pi


As I have said before part of the point of this blog is so I have a record of how I did things and this definitely fits into that category.  If others find it useful, that is a bonus.

I have had a small 2.4″ TFT with touch sitting around for ages.  I looked at using it when it arrived, but compling a kernel put me right off.  So this week the notion took me to try again and I discovered it was actually very easy with newer versions of Raspbian.

[Edit 16 Dec 17] I had made an error in the table below putting both clocks on GPIO08. I have had some PCBs made up to simplify the connection – the error blew the TFT I was using sadly.


There are a couple of key sources I used and in places I have blatantly plagiarized them (they are that good) so as to bring it all together, and in case the source disappears (it happens).

  1. Wiring and OS tasks
  2. tslib info
  3. fbTFT info
  4. getting started with pygame
  5. More on pygame
  6. Fixing Coordinates on Jessie


The background detail, found in the first reference, is interesting. It seems the old notro type method is being phased out in preference for overlays.  Worth reading.


I was fortunate in that my board was very similar to the board in reference 1 so I could use those mappings for my wiring.  If you have a different board then head over there and it is explained how to work out the wiring for your specific overlay.

Panel Pin Function name GPIO Pin No.
T_IRQ Panel Touch Interrupt GPIO25 22
T_DO Touch Data Out GPIO09 21
T_DIN Touch Data In GPIO10 19
T_CS Touch Chip Select SPI_CE1_N GPIO07 26
T_CLK Touch Clock GPIO11 23
LED Backlight GPIO18– see note 12
D/C TFT Data / Command GPIO24 18
RESET Reset GPIO23 16
CS TFT Chip Select SPI_CE0_N GPIO08 24
GND Ground GND
VCC 3V3 supply

I did the wiring using a breadboard as a couple of the pins need the same GPIO pin.  Not pretty but functional!

Note the original sources comment on wiring up the LED Backlight.  For now I have just connected to the 3.3V.

Setting up the OS

As an aside, although I did this initially on a clean install of Raspbian Jessie, I then moved to DietPi. The main reason for this is that you can create a configuration file for such things as WiFi SSID & Password and a fixed IP address.  I could then do everything headless from the go.

The OS changes themselves are simplicity itself.

Edit the file /boot/config.txt (on DietPi you need to edit /DietPi/config.txt) and include the following lines;

And reboot the Pi. That’s it.  If it is OK, the screen will light up during boot, then go blank.

Next step is to check and calibrate the touchscreen.  I deviate a bit from my main source as there were 2 extra packages to install (references 2 & 3).

I found I needed to do the calibration before I did the test so (remembering to change the event number and fb if necessary);

and then

If all is well, you can drag the cross hairs or draw in the screen.

You now need to check the touch input. to do this;

and then run evtest  If you are running headless, the touch input is likely to be event0, if you are not, again head over to the first reference and see how to interpret the contents of  /dev/events/

The AdaFruit tutorial on this has some instructions for creating a udev rule to map the touch screen events to a fixed point.  I have not managed to get this to work and I suspect it is because I am using overlays.

pygame and TSLIB problems with Jessie

I won’t bore you with how I got here, suffice to say I realised that the coordinates of the touch input were not correctly reported by the OS to pygame. After a bit of digging I found reference 6.  In summary, to fix the issue create and run the following script;

This fixed the coordinates for me.

Python and TFT

The aim of all this is to have a small touchscreen that I can use to provide simple control of my heating and hot water – basically a boost function.  I’ll blog about that when I get a bit further.  In the meantime, the following code will test out the touch responses.


Edit 5 June 2017

I have now sorted out some more useful code to display buttons which you will find here.




Facebooktwitterby feather
Posted in Hardware, Linux, RaspberryPi | Tagged , , | Leave a comment

Bash History

On several occasions, I have lost some of my bash history, as the ssh session has not been exited cleanly. This annoyed me as I use the history as a means to remember what I have done.  It prompted me to investigate if I could fix it and Unix being Unix, of course you can.

The History part of my .bashrc file now looks like this;

I tried adding a timestamp but that just looked messy so I took it out again.


Facebooktwitterby feather
Posted in Linux | Tagged , | Leave a comment

Installing pywws and running it as a Daemon

I have been using pywws for a number of years to download the data from my weather station and it simply just works.  There is good support from the author though there are some gaps in the documentation.  Moving pywws to the new Pi gave me the opportunity to update the package which had not been done for a while (ain’t broke don’t fix).

The weather station is a WH1080 compatible system from Maplin.  It is often on offer for about £60 and you can buy spares for all the external parts including the transmitter at a very reasonable price which is a major bonus.

Installing pywws via pip is relatively painless but what I would urge, is that you check out the dependencies first and get them installed before the main package.  It is easy to miss or skip over this step.  There are 2 options for USB libraries and I plumped for the libsub pip install libusb1 .

One of the beauties of this weather station and pywws combination is that the weather station itself stores data so you can disconnect the USB from the base station to play and not lose any data as it just catches up once reconnected.

Once installed, I plugged in the weather station to the new pi to check that it was communicating – I had to do the changes suggested as it would only work as sudo otherwise. Doing this test also created some of the system files.

I then pulled across all the data files using rsync plus a couple of system files I had modified.  These modifications allowed pywws to communicate to EmonCMS and to put data out on mqtt. The way this is setup is a little convoluted (to my mind), not that well documented, and it had taken me a while previously to work it out. To do it you need to create a new ‘service’.

New pywws service

To create a new pywws service you need to create 2 files in the service folder(/usr/local/lib/python2.7/dist-packages/pywws/services), a service file and a template.  The template is used to create the format of the message you wish to send and the service file defines some of the parameters that the template may use.  By defining the parameters correctly, you can also change these in the main configuration file weather.ini in the data folder.

I have created a GitHub repository for the new service files for EmonCMS.  I am using the http API for pushing the data to EmonCMS. One of the problems I had in setting this up is that EmonCMS does not accept valid JSON so it took a bit of time to get the template right.

Running pywws live-logger as a Daemon

Pywws has a daemon version of the main logging script using python-daemon. I have never felt completely comfortable with this as it seems to me that by following the instructions, you can end up with a daemon starting a daemon. The original install was Wheezy based so I had created a file for init.d to use (available from the GitHub repository) but on the newer Jessie based system, I thought I’d try a systemd setup.

After reading this excellent guide on systemd, I generated the following service file and placed it in the systemd folder (/etc/systemd/system).  After a couple of minor teething problems, I got it running and it has just worked since.  This seems a better solution.

Monitoring pywws

On the old Wheezy system, I used Monit to monitor both the daemon and a specific folder.  For a while, pywws had a habit of just stopping the writing of data.  The daemon was still running just no data appeared. I therefore set Monit up to monitor a specific folder to check it was modifed regularly.  As I have installed Monit I brought this across (also on the GitHub repository).

However, one of the advantages of systemd is that it has built-in monitor and restart functionality so Monit cannot be used to monitor the daemon itself.

Final Steps

Once I was happy that the system was working fine, I plugged the weather station back into the original Pi, restarted it and left it running for a while to catch up.  I then stopped the daemon, used rsync to copy across the files again (I blatted that data on the new Pi first just to be sure) and plugged the station into the new Pi.  A quick reboot and it all seems very happy.

All that remained was to set up some web access but that is another blog.

Facebooktwitterby feather
Posted in Technical - General | Leave a comment

Node-RED and icons

I’ve been looking at Pete Scargill’s thermostat and got stuck on icons. He blogged about icons recently but that didn’t say how to install them.  So on the basis of ‘if I didn’t know there will be others who don’t know’, here’s how to install some icons in Node-RED.

Pete’s suggestion is to create a static folder under the node-red folder and put the icons in there such as home/pi/.node-red/public/myicons .

To make these available you need to edit the file /home/pi/.node-red/settings.js  and add the line

note the closing comma! (there is an example in the default file – just search for it).

I’ll quote a comment Pete made on this (explains it better than I could);

So then the web pages think that the root page for all assets is the above – hence your reference in the web starts at /myicons.

The reason I use myimages and myicons etc is that Node-Red already has some directories it uses for UI and images is one of them – if you make an “images” directory it conflicts – so I just prefix any of my stuff with “my” – so there you go.

That solves where to put them, but what to put there.  Well in his blog he directs you to this open icon set on SourceForge https://sourceforge.net/projects/openiconlibrary/.

I downloaded the file from SourceForge and copied it across to the Pi using WinSCP into a folder in my home directory I called ‘icons’.  It is a tar.bz2 file but that is easily unpacked in one go with:

I prefer to keep things like this icon pack separate and link to them so if you want to change the icon set, you just change the link.  To do that move into the public folder you have created  cd /home/pi/.node-red/public and run the following command to create a symbolic link (adjusting paths as necessary) to your icon set;

If you do ls -la you will see the link.  Restart Node-RED

and wait until it has properly started.  Refresh the dashboard page and you should have icons!


Facebooktwitterby feather
Posted in Node-RED, RaspberryPi | Tagged , | 3 Comments

Node-RED, npm, sudo and permissions

I ran into a problem on my Pi yesterday; I could not update any npm installed packages.  If I did try, it simply failed, quietly.  Some searching led me to believe that it was because I had installed npm using sudo and possibly other packages as well (I’d lost some Bash history so I cannot be sure – a blog for another time).

I dug around a little and found this comment on StackOverflow;

“Recent versions of npm switch to user “nobody” as a security measure if it’s being run with sudo privileges.”

I could see exactly this within /usr/lib/node_modules.

On the npm site itself, there is an item on this issue which suggests some modifications which I really did not look the like of, as  npm config get prefix  returned just /usr.

Another item I found suggested changing permissions in a similar manner to the npm docs but I felt it was rather over the top and changed too much.  So I took a middle road and did the following;

This changed all the ‘nobody’ owned files and folders to owned by pi (which I was logged in as).

What I cannot remember (lost history again) is if I did it for the folders in my home folder. If I had found any files owned by ‘nobody’ I would have done so.

This has solved the problem and now I can use npm without sudo and update (both locally and globally) work fine.


Facebooktwitterby feather
Posted in Linux, Node-RED | Tagged , | Leave a comment

A New Discovery

I actually love it when I come across something new, even if I should have found it ages ago. WinSCP. How I have never come across it beats me, but if you regularly edit files on different systems, this is for you. It even imports PuTTY session information.

Facebooktwitterby feather
Posted in RaspberryPi, Technical - General | Leave a comment

Trouble with Hosting at SmartHost

If you happened to notice a number of posts simply disappearing, it was because my hosting provider SmartHosting had some sort of failure last week which meant they lost a number of days work.  I noticed it on Sunday and they had to go back to the last backup which was Thursday.  Rather unfortunate especially as I have only been with them for a few weeks and I just happened to do a load of updates in those couple of days.

Now I’m not unhappy it happened, these things do happen, but going back to a backup 3/4 days ago seems a little off. OK I don’t pay for the super duper backup but still…

What I am less happy about is their communication.  If I hadn’t noticed it, they would never have told me as I have received no formal notification and I would have wondered where it had all gone.  Their status page is impossible to find until support send you the link and it is rather reticent in explaining the problem and it fails to mention they went back to a relatively old backup so many people would have lost data.  Their twitter feed appears to never be read or if they do they don’t actually respond.

They claim a 99.9% up time.  It took 6.5 hours to fix, so if that is 0.1% they should not have another outage for 270 days.  Let’s hope so or else I have paid for a pup.

Facebooktwitterby feather
Posted in General | Tagged , | Leave a comment

Node-RED and XML to JSON

I’m trying to catch up with a whole load of stuff I did before I started this blog; as much as anything, so I can remember what I did for when I need to do it again!

Following on from installing Monit, I wanted to use Node-RED to monitor Monit so that ultimately I can have a dashboard for all sorts of things to be monitored and possibly alerts (rather than Monit doing that directly).

As previously mentioned, Monit communicates via an http interface so extracting the data was remarkably simple. All you need is a websocket flow and a URL like http://x.x.x.x:2812/_status?format=xml . It seemed that the obvious way to use this data was to convert the XML to JSON.

However, when I tried the built-in XML parser it did not work. Some digging brought up the fact that this built-in node has a limited depth. Not very useful. Further research led me to the xml2json node on npm. I’d never really understood the relationship between npm and Node-RED. What I think I understand is that Node-RED uses some command line nodes from npm which in itself is a Javascript package manager.

So in order to use this command line node (rather than a Node-RED flow) I needed to install it then ‘require’ it. The whole Javascript ecosystem is rather alien to me at the moment so getting it installed took a fair bit of experimenting although it is actually very simple to do.

It comprises of 3 steps:

  1. Install the node from npm. As you want to use it from Node-RED, you need to *not* install it globally – counter-intuitive right? So simply go into your node red folder (usually ~/.node-red) and then npm install xml2json (reference).
  2. Next, you need to edit your settings.js file to ‘require’ the node (reference). This file is also usually found in the ~/.node-red directory.
    Look for the functionGlobalContext and add in a line for the xml2json node.

    Save and restart Node-RED sudo service nodered restart .
  3. Add the code into a function such as:

This will now enable you to take an XML flow and break it down into its constituent parts

The complete Node-RED flow:


Facebooktwitterby feather
Posted in Coding and Programming, Node-RED | Tagged , , , | Leave a comment