Use an Android smartphone as a Raspberry Pi screen
Event Processing
The button methods retrieve system information and write the results to the text field. However, this cannot happen directly because the application "freezes" while the method is running. MainLoop()
cannot respond to interface events (move window, press button, etc.) during this time.
The button method on_uptime()
starting in line 74 thus only starts a thread, and the actual work is then handled asynchronously in get_uptime()
(starting in line 90).
As usual with all graphical libraries, only the main thread (MainLoop()
) can update the interface. Therefore, the thread creates and sends a self-defined event at the end (line 100). This private event, which I dub SysCmdFinishEvent()
, contains only the text to be displayed; the definition is at the beginning of the listing.
The program also binds a method to this event (line 31) in the constructor of AppFrame
. A text field is updated here (starting in line 81), and thus the circle closes.
If you want to fill a field automatically with status information, you can use the same approach. A thread running for a long time simply sends an update event with the new field content at fixed intervals.
Integration
The program still has two problems: If it starts when the X server is not (yet) running, it crashes, and it stops as soon as the X server app stops running or is out of range.
You can solve the first problem by testing for the X server at program launch and then only continuing the program if there is a response (the code is missing from Listing 1 because of a lack of space). As soon as the app starts, the interface pops up. This only works if you don't use multiple Raspberry Pis on one smartphone.
The system daemon solves the second problem for you. You can start the program as a service with the wxvlcd.service
file (Listing 2), which is also where you set the DISPLAY
variable. The only special thing about the unit definition is line 10 with the Restart=always
statement. It ensures that systemd automatically restarts the program after it has ended. Details and alternatives to always
(e.g., on-failure
) can be found with the man systemd.service
command.
Listing 2
wxvlcd.service
Conclusions
Thanks to the X server and wxPython, you can implement simple control and info displays for your Rasp Pi projects with minimal effort – and all communication is wireless.
If you do not have an Android device or would rather use one of the small 3.2-inch displays with the Raspberry Pi, you can still use wxPython. The program evaluates the DISPLAY
variable, so the program can be output locally or over the network, depending on the configuration.
The mature development stage of wxPython offers another benefit. The toolkit is extremely stable, and you will find a sample solution for almost every need online. Even if you have never programmed a graphical user interface before, you will quickly turn out an attractive GUI just by following the example in this article.
Infos
- Project Blue Dot: https://www.stuffaboutcode.com/2017/04/bluedot-bluetooth-remote-for-raspberry.html
- Pi Control: https://pi-control.de
- Android X server (app): https://play.google.com/store/apps/details?id=x.org.server&hl=en
- Android X server (project): https://github.com/pelya/xserver-xsdl
- Kivy: https://www.kivy.org
- Project page for the article: https://github.com/bablokb/pi-vlcd
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)