Chapter 4. Software
4.1 Making a Media Center
To use your Raspberry Pi as a media center, you should go for the superior performance of the Raspberry Pi 4 B, as video playing is very processor intensive.
You can set up your Raspberry Pi as a media center during the NOOBS (New Out Of the Box Software) installation process (Recipe 1.6). Instead of selecting Raspbian as the distribution to install, select LibreELEC_RPi4 (see Figure 1-9).
LibreELEC is a distribution that optimizes your Raspberry Pi as a media center. It incudes the Kodi media center software, which is based on the XBMC open source project that was originally developed to convert Xbox game consoles into media centers. The code has since been ported to many platforms, including the Raspberry Pi (Figure 4-1).
Raspberry Pi is perfectly capable of playing full HD video as well as streamed music, MP3 files, and internet radio.
Kodi is a powerful piece of software with many features. Perhaps the simplest way to check whether it is working is to put some music and/or video files onto a USB flash drive or external USB hard disk and connect it to the Raspberry Pi. You should be able to play them from Kodi.
Since the Raspberry Pi is likely to be sitting near your TV, you might find that your TV has a USB port that can provide enough current to run the Raspberry Pi. If this is the case, you won’t need a separate power supply.
A wireless keyboard and mouse are a good idea because, if you buy them as a pair, they will use a single USB port for the dongle, which avoids the need for wires trailing all over the place. You can also buy minikeyboards with built-in trackpads that are useful in this situation.
A wired network connection is generally higher performance and better than a WiFi connection, but it is not always convenient to have the Pi near an Ethernet socket. If this is the case, you can set up XBMC to use WiFi.
Setting up Kodi is very intuitive, and you can find full instructions on using the software at http://kodi.wiki.
You can add an infrared (IR) remote to Raspberry Pi to control Kodi.
4.2 Using Office Software
Raspberry Pi is, after all, a Linux computer, and Raspbian includes a collection of LibreOffice programs direct from the Main menu (Figure 4-2).
There is a choice of two word processors, AbiWord and LibreOffice Writer. LibreOffice Writer is part of the popular LibreOffice suite of software that offers a mostly very compatible set of alternatives to the Microsoft Office suite of programs. LibreOffice Writer will open and save Microsoft Word documents and is available for Windows, Mac, and other Linux computers.
A Raspberry Pi 4, 3, or 2 will run office applications much better than an older Raspberry Pi.
4.3 Making a Webcam Server
You can use either a USB webcam or the Raspberry Pi Camera Module as a webcam that you can access from elsewhere on your network using a browser.
Download the motion software. This will allow you to set up a Raspberry Pi with a USB webcam connected to it so you can connect to a web page and view the webcam.
To install the software, enter the following command in a Terminal window:
$ sudo apt-get update $ sudo apt-get install motion
You’ll need to make a few configuration changes. First, use the following command to edit the file /etc/motion/motion.conf:
$ sudo nano /etc/motion/motion.conf
This is quite a big configuration file. Near the top of the file, you will find this line:
Change the line as follows:
The other change is much further down the file. You need to change this line:
webcam_localhost = on
so that it reads like this:
webcam_localhost = off
There is one other file that you need to change. Enter the command:
$ sudo nano /etc/default/motion
To get the web service running, issue the command:
$ sudo service motion start
If you are using the Raspberry Pi Camera Module, there is an extra step. You need to edit the file /etc/modules using the following command:
$ sudo nano /etc/modules
Add the following line to the end of this file, then save and exit nano as usual:
Reboot your Raspberry Pi and you should now be able to open your web browser and view the webcam. To do this, you will need to know the IP address of your Raspberry Pi (Recipe 2.2). You can also check that the webcam is working from the browser on your Raspberry Pi by going to http://localhost:8081.
From another computer on the same network, open a browser and go to the URL http://192.168.0.210:8081/. You will need to change the URL to match the IP address of your Raspberry Pi, but keep the
:8081 port number on the end of the URL.
If all goes well, you should see something like Figure 4-3.
The motion software is really powerful. There are many other settings you can tweak to change how your webcam works.
By default, the webcam will be viewable only from within your network. If you want your webcam to be visible to the entire internet, you need to set up port forwarding on your home hub. This will require you to log in to the admin console for the hub, look for the port forwarding option, and enable it for port 8081 for the IP address of the Raspberry Pi.
You will then be able to view the webcam by using the external IP address allocated by your ISP. This is usually shown on the front page of your admin console. But be aware: unless you pay your ISP for a static IP address, this IP address is likely to change every time you restart your home hub modem.
Services such as No-IP can also be used to provide dynamic domain name service (DNS), so that you can register a domain name that automatically follows changes to your IP address.
There is thorough documentation on the Motion website.
You can find a list of webcams compatible with the Raspberry Pi at https://oreil.ly/_z4GH.
To fix the IP address of your Raspberry Pi webcam so you can always find it, see Recipe 2.3.
To use a webcam for computer vision projects, see Chapter 8.
See Recipe 3.17 for information on using
4.4 Running a Vintage Game Console Emulator
If you fancy rediscovering your misspent youth and playing Asteroids on an emulator for the Atari 2600 (Figure 4-4), the RetroPie project will appeal to you.
Many wonderful projects have been built that create custom consoles and game tables complete with retro games controllers.
It is worth noting that even though these games are ancient, they are still owned by someone. The ROM image files that you need to play the games on an emulator, although easy to find on the internet, are not necessarily yours to take. So please stick to the law.
The emulator uses a surprisingly large amount of the Raspberry Pi’s meager resources, so you might find that you need to use a Raspberry Pi 4, 3, or 2.
In an internet search, you can find many people who have taken this basic setup and added a retro USB controller, like the widely available and quite low-cost controllers, and built the Pi and a monitor into a big arcade-style housing.
You can also buy a kit called the Picade from Pimoroni to make a lovely arcade machine (Figure 4-5).
Full RetroPie documentation is available on the RetroPie site.
4.5 Running Minecraft: Pi Edition
Mojang, the original developers of Minecraft, have ported the game to the Raspberry Pi and Minecraft Pi, is preinstalled on the latest Raspbian distribution (Figure 4-6).
To get Minecraft to fit on the Raspberry Pi, the developers made some shortcuts in the graphics code. This means that you can only play the game directly on the Raspberry Pi with the keyboard, mouse, and monitor connected directly to it. It will not work over a remote virtual network computing (VNC) connection.
The Pi edition of Minecraft is based on the mobile version of Minecraft and lacks some features, most notably Redstone.
4.6 Raspberry Pi Radio Transmitter
Be sure to check out the accompanying video for this recipe.
You want to convert your Raspberry Pi into a low-powered FM transmitter that will send a radio signal to a normal FM radio receiver (Figure 4-7).
Back in the early days of the Raspberry Pi, some clever folks at Imperial College, London, created some C code that allows you to do just this. The download even plays the Star Wars theme as a sample. This project will still work if you have an original Raspberry Pi 1. The code has not been updated since for newer versions of Raspberry Pi.
Fortunately, however, the project lives on in an altogether more advanced project called rpitx.
All you need is a short length of wire attached to general-purpose input/output (GPIO) pin 4. A 10 cm female-to-male header lead will work just fine for this. In fact, it should work with the radio right next to your Pi without any kind of antenna—such is the strength of the transmission.
The first step is to install the rpitx software using the following commands. Note that this installation will change some things about how your Raspberry Pi is configured, including the frequency at which the GPU (graphics process) works. So if this is your main Raspberry Pi, make sure you first back up anything precious. Here’s the code you need:
$ sudo apt-get update $ git clone https://github.com/F5OEO/rpitx $ cd rpitx $ ./install.sh
You will now need to do something else for a good 15 minutes or so while the software installs. You might see what look like error messages and warnings, but these are normal. At the end of the installation, the installer script will ask:
In order to run properly, rpitx need to modify /boot/config.txt. Are you sure (y/n)
Press Y, and the script will then confirm the changes it has made with the following message:
Set GPU to 250Mhz in order to be stable
If you need to reverse this change, edit /boot/config.txt by removing the last line that says
gpu_freq=250, then reboot.
Next, find yourself an FM radio receiver and tune it to 103.0 MHz. If this frequency is already occupied by some other transmission, pick another frequency and make note of it.
Now run the following command (changing the frequency parameter from
103.0 if you had to change frequency):
sudo ./pifmrds -freq "103.0" -audio src/pifmrds/stereo_44100.wav
If all is well, you should hear the voice of the developer talking about left and right channels.
You need to know that this project may not be legal in your country. The power output is higher than that of FM transmitters used with MP3 players.
Were you to put a Raspberry Pi in your vehicle, this would be a great way to output sound through the vehicle’s audio system.
To learn more about the rpitx project, see https://github.com/F5OEO/rpitx.
4.7 Editing Bitmap Images
Download and run the GNU Image Manipulation Program (GIMP; see Figure 4-8).
To install GIMP, open a Terminal session and type the following command:
$ sudo apt-get install gimp
Once GIMP is installed, you’ll find a new entry in your Main menu under the Graphics heading called GNU Image Manipulation Program.
Despite being hungry for memory and processor power, GIMP is usable even on a Raspberry Pi 2 B, but there will be much less waiting around if you use a Raspberry Pi 4.
Raspbian also includes a simpler image manipulation program called ImageMagick. You will find this on the Start menu under Graphics. You also can use it from the command line to do things like change the resolution of images. Read more about it on the ImageMagick GitHub repo.
Find out more from the GIMP website.
GIMP has a lot of features and is a very sophisticated image-editing program, so it does take a little learning. You’ll find an online manual for the software at the GIMP website, under the Docs tab.
For editing vector images, see Recipe 4.8.
For more information on installing with
apt-get, see Recipe 3.17.
4.8 Editing Vector Images
Install Inkscape using the following commands:
$ sudo apt-get update $ sudo apt-get install inkscape
Once Inkscape is installed, its icon will appear in the Graphics section of your Raspberry Pi Start menu.
Inkscape (Figure 4-9) is the most used open source vector image editor.
Inkscape is a very powerful piece of software with very many features that can take some time to master, so don’t be disheartened if it won’t do what you want at first. You will probably need to run through a few tutorials.
Inkscape is another program best run with the extra power of a Raspberry Pi 4, 3, or 2.
4.9 Internet Radio
sudo apt-get install vlc
Once it’s installed, you can find VLC in the Sound & Video section of your Main menu.
Run the program and then, on the Media menu, select the Open Network Stream option. This opens a dialog box (see Figure 4-10) in which you can enter the URL of the internet radio station that you want to play.
You will need to plug headphones or amplified speakers into the audio socket on the Raspberry Pi.
You also can run VLC from the command line as follows:
$ vlc http://www.a-1radio.com/listen.pls -I dummy
VLC will probably produce a series of error messages but then play the audio just fine.