Labels

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, 18 October 2025

Digital Picture Frame II

Intro

Whilst playing with options for my DPF on the iPad I came across an article in LXF describing a simple RPI python slideshow app. Sadly it was the last LXF (Linux format) magazine edition dated July 2025 and was rather nostalgic.  I have enjoyed many of their articles over the last ten years but I guess everone/everything changes over time.

I have a small Samsung TV which functions as an RPI screen and I noticed when experimenting with mirroring my iPad to the TV that it provides an unobtrusive display which can be on in the background and looked at from time to time.

If I attach the screen to my RPI application server (PI41) I have a basic system to use for a PoC to see whether I prefer it to my other DPF versions.

Proof of Concept

The python app requires a desktop GUI to display images and PI41 doesn't have one installed as I usually use it "headless".
My first job was to change over from RPI OS Lite to the desktop version.  I botched it somewhat just adding packages until LXDE worked. I also had to plug in keyboard/mouse to PI41.
I then installed the python pygame module and I was able to display a sample image on the screen and it looked fine.

 I was happy with PoC progress so I setup a Samba share to my database server (PIP) giving me access to my art gallery images.  The image gallery has smaller lower resolution images making them suitable for web loading.  They are fixed height of 922 pixels which fits fits very well, without scaling on the RPI screen which is 1920p wide x 1080p high.

I decided to initially display all images within a folder in my slideshow rather than using the MySQL database containing image, gallery and file information.  Setting up a loop to display the images was straightforward, based on the LXF magazine example.

I was very pleased with the result.  Folders contain a variety of images from different galleries or exhibitions and it is easy to spend a little while looking at images as they attract your attention briefly.  Images look really good on the TV screen; they are a good size and have good quality colours.

Improvements

Choose a gallery

Now we are happy this is a good idea we have various jobs to turn the slideshow into a practical system.  Firstly, I need to be able to control which gallery folder is displayed.  There are about fifteen folders in total each containing about 50-100 images and I want to choose one folder rather than cycle round them all.  Initially I thought that adding websocket processing was the natural way forward but I couldn't easily work out how to run the slideshow and have the websocket waiting for instructions concurrently.

The program which loops round to display files in a folder is called pictureframe.py.  I set it up to read a parameter file picframe.parm which contains the name of the folder to be displayed.  It reads picframe.parm after each image is displayed and, if the folder changes, pictureframe.py loads a new set of images and starts to display them.
A second simple python script, picparm.py, takes a single command line argument, the name of a folder, and saves it to picframe.parm.

It is now an easy task to create a webpage which has a list of folders/galleries as buttons.  When a button is pressed the webpage uses a websocket to invoke picparm.py and change the folder being displayed.  This mechanism has been tried and tested on my music control web pages.


Display Tweak

The Samsung TV screen with a HDMI connection to the RPI has a screen resolution of 1920x1080.  I Rather than display the image as fullscreen using pygame I display a window 1840x1036 which allows me to add the images descriptive file name as a window caption. The image itself is always 922 pixels high which fits nicely into 1036 pixel window height when centred. 

Start and stop the display

The slideshow runs on a RPI desktop but I have no intention of using the attached keyboard to start and stop it.  After a few minutes writing python I was irritated by using the screen / keyboard directly.  The first remote desktop I tried was XRDP, an RDP implementation for RPI.  However it gave me a copy of the console rather than allowing me to "take over" the real one.  Most RPI users choose VNC.  I installed TigerVNC on Windows to communicate with VNC on RPI and this allowed me to stop using the attached keyboard and mouse.

However, I dont really want to do anything at the computer to start the slideshow.  I need RPI to automatically load and run pictureframe.py at startup.  Firstly we can setup RPI to automatically boot into the GUI using raspi-config

Then, in the LXDE GUI preferences, we can add pictureframe.py in autostart to run when the system starts up.

Now, as soon as the system startup our slideshow starts based on the most recent parameter file.


In fact I dont want to have a "stop" option anymore.  I add an extra parameter mode, "pause" or "resume",  to pictureframe.parm.  If pictureframe.py  sees "pause" in the parameter file it loops round until the parameter is changed to "resume" and the screen image doesn't change at all.

We add a "pause/resume" button to the web page so that we can stop and start the display from any browser device.

More enhancements

I am not sure how quickly to change display images so that I can look at them in enough detail, without getting bored.  I have started up with options to display for 5,10,20,60 seconds.  I added an extra line to pictureframe.parm containing the desired interval and added a command line parameter "time" to update the value in python.  I then added buttons in the webpage for each interval.

I also want to see in the browser what the current display parameters are.  I added a "status" parameter to pictureparm.py which returns current parameter values.  I then setup a field in the web page to display the output.


Finally, I added a randomiser, so that we dont start with the same picture each time we display a gallery.  Pictures are still in the same order but we can start with any one.

Outro

I am happy with my latest slideshow.  I like the larger images displayed on the Study TV.  Also you dont need to focus on them - they are just there.  You can control the display from any device and it doesn't require any thought to start / stop or change display.  Unlike the previous slideshows I have actually been using this one.

Wednesday, 29 January 2020

MicroPython

Micropython was invented by an Australian programmer Damien George in 2013.  It is a lightweight version of python, compatible with version 3, suitable for microcontrollers.  It strikes me that I should concentrate more on python programming and this is part of the suite.  In particular I can use it for  ESP8266, ESP32, Arduino and micro:bits.

micro:bit


It couldn't be simpler to get started with micropython as explained at microbit.org.  Just use an on-line editor to create your program - initially you can amend the sample provided.  Once happy with the program it is downloaded to your PC as a hex file.  You connect the micro:bit to the PC which gives you a new drive.  Copy the .HEX file across to the micro:bit drive and your program runs.

ESP32


Install a python utility called esptool in my Windows environment using pip
download and install firmware as described at micropython.org
Install the Thonny IDE as described by RandomNerdTutorials.

Now we select micropython on ESP32 in Thonny options, select the ESP32 com port and see the Thonny shell running on ESP32.


First ESP32 Project

I really want projects to be web based to reduce connection hassles.  So first of all I want to establish wifi connectivity.
RandomNerdTutorials.com have a very simple project which starts wifi and establishes a webserver.
Two files called boot.py and main.py are downloaded to the ESP32.  These are run automatically at reset.  In this case boot.py establishes the wifi connection and main.py causes a webserver to listen for and process connections.
Connections from a browser cause a webpage to be displayed allowing control of a GPIO.
The GPIO in question is the inbuilt LED (pin 2) so clicking a button causes a response to be sent to the webserver turning it on or off.
This gives us everything we need to use the ESP32 as a basic standalone webserver reporting on its local environment.

Webserver

J-Christophe Bos has created a wonderful small webserver which is contained in 3 files:


It allows you to serve static pages, server-side scripting (pyyhon/html), websockets and supports GET/PUT, JSON, AJAX all in 65KB of program.  ESP32 has 2MB flash for filespace so this leaves plenty of room for data and static pages.


There is a microwebsrv2 update on github but as of December 2019 there seems to be a little problem making it trickier (for newbies like me) to setup. 

Monday, 11 March 2019

Controlling Pure Jongos using upnpclient

I have previously written about a python script called nanodlna which is useful for "casting" ie sending dlna commands to Pure Jongo speakers and DACs.
A python script called upnpclient by Ellis Percival on Github does the job much more effectively.  It includes an excellent interactive explanation using upnpclient to explore and control a dlna device.  It provides device discovery storing upnp objects in python variables.  All device parameters are easily available so that device capabilities can be investigated (interactively) and controlled through python.
I developed a python program upnp.py which loaded URLs (SetAVTransportURI) and played / stopped the URI (Play and Stop functions). upnp devices need a streaming URL so a number of UK channels were coded into the script so they could be started.  Alternatively a URL can be entered on the command line to stream from an alternative source, for example the MuSe mpd server or an album / playlist M3U URL.
A web page JongoPanel was created to allow a user to see what is playing on each Jongo and start or stop the stream.  Any source can be directed to any or all Jongos.  JongoPanel calls upnp.py using the cgi interface.

Sunday, 17 February 2019

Python morsels

iPython
iPython is a must for any python programming.  It is simple to install (with pip3), can be used just like the standard interpreter with no learning curve and is packed with useful features.  Features which made my life better from day 1 include:
1) being able to a stop a program at a given point and inspect the variables
2) Hitting tab after an obect name to display a list of its properties.

Python CLI
python command line programs are useful when using linux.  In particular Python is great for simple utilities but it is a nuisance to have to run them from specific directories.  Python CLI makes this easier. Thomas Stringer has provided a simple example.  I also found an explanation on Stack Overflow on usage helpful.