Banana Pi Cookbook - Sample Chapter
Banana Pi Cookbook - Sample Chapter
Banana Pi Cookbook - Sample Chapter
ee
Banana Pi Cookbook
Banana Pi Cookbook is a practical guide that starts with setting up Banana Pi and moves on to exploring
the more advanced capabilities of the device. You will learn to build a decent Linux-based operating
system that can resolve common server tasks (Samba, Web, DLNA) and multimedia challenges. Also, you
will see how to use the GPIO opportunities by building simple but straightforward circuits.
By the end of this book, you will be an expert in using Banana Pi to deploy a variety of hardware and
software applications.
Banana Pi Cookbook
Banana Pi is a dual core single board microcomputer like Raspberry Pi but is much faster, more
cost-effective, and performs more efficiently than Raspberry Pi.
Sa
pl
e
Banana Pi device
Install Android and a Linux-based distribution
onto Banana Pi
Attach external drives to your Banana Pi by
and problems
P U B L I S H I N G
Ryad El-Dajani
Banana Pi Cookbook
Over 25 recipes to build projects and applications for multiple
platforms with Banana Pi
P U B L I S H I N G
Ryad El-Dajani
Preface
Packed with recipes for the Banana Pi, solving the most common real-world problems, viewers
get a practical assistance to avoid difficulties.
Full of supportive guides, this book is designed to help you build and expand your device into
a versatile box. Any computer enthusiast can quickly learn how to become a Banana Pi expert,
while not requiring skills in programming or Linux. Banana Pi Cookbook will allow you to use
the technology start from the very beginning, through the daily usage and maintenance, up to
setting up a WordPress from scratch, sharing files using Samba or ownCloud, blinking an LED,
or playing 1080p videos.
Preface
Chapter 5, Using the GPIO Pins, introduces the GPIO pins. We will light up an LED using the
shell. Furthermore, we will build a simple circuit, including a pull-up resistor and control an LED
with a button. To achieve this, we will introduce the C programming language with the WiringPi
library and the Python scripting language with the RPi.GPIO library.
Chapter 6, Multimedia, covers configuring the two audio outputs (line out and HDMI). We will
discuss and solve the hardware acceleration problem by compiling important components
and using these mentioned components to build video players and Kodi. The configuration
of a remote control using LIRC is also covered.
Introduction
The Banana Pi is a single-board computer, which enables you to build your own individual
and versatile system. In fact, it is a complete computer, including all the required elements
such as a processor, memory, network, and other interfaces, which we are going to explore.
It provides enough power to run even relatively complex applications suitably.
In this chapter, we are going to get to know the Banana Pi device. The available distributions
are mentioned, as well as how to download and install these distributions. We will also
examine Android in contrast to our upcoming Linux adventure.
Thus, you are going to transform your little piece of hardware into a functional, running
computer with a working operating system. You will master the whole process of doing the
required task from connecting the cables, choosing an operating system, writing the image
to an SD card, and successfully booting up and shutting down your device for the first time.
Banana Pi Overview
In the following picture, you see a Banana Pi on the left-hand side and a Banana Pro on the
right-hand side:
As you can see, there are some small differences that we need to notice. The Banana Pi
provides a dedicated composite video output besides the HDMI output. However, with the
Banana Pro, you can connect your display via composite video output using a four-pole
composite audio/video cable on the jack.
In contrast to the Banana Pi, which has 26 pin headers, the Banana Pro provides 40 pins.
Also the pins for the UART port interface are located below the GPIO headers on the Pi,
while they are located besides the network interface on the Pro.
The other two important differences are not clearly visible on the previous picture. The
operating system for your device comes in the form of image files that need to be written
(burned) to an SD card. The Banana Pi uses normal SD cards while the Banana Pro will only
accept Micro SD cards. Moreover, the Banana Pro provides a Wi-Fi interface already on board.
Therefore, you are also able to connect the Banana Pro to your wireless network, while the Pi
would require an external wireless USB device.
Besides the mentioned differences, the devices are very similar. You will find the following
hardware components and interfaces on your device.
2
Chapter 1
On the back side, you will find:
Various display outputs [HDMI, LVDS, and composite (integrated into jack on the Pro)]
A microphone
Various hardware buttons on board (power key, reset key, and UBoot key)
Various LEDs (red for power status, blue for Ethernet status, and green for user defined)
As you can see, you have a lot of opportunities for letting your device interact with various
external components. In the upcoming chapters, we are going to explore most of the
possibilities in detail.
Android
Android is a well-known operating system for mobile phones, but it is also runnable on various
other devices such as smart watches, cars, and, of course, single-board computers such as
the Banana Pi.
The main advantage of running Android on a single-board computer is its convenience.
Anybody who uses an Android-based smartphone will recognize the graphical user interface
(GUI) and may have less initial hurdles. Also, setting up a media center might be easier to do
on Android than on a Linux-based system.
Linux
Most of the Linux users never realize that they are actually using Linux when operating their
phones, appliances, routers, and many more products, as most of its magic happens in the
background. We are going to dig into this adventure to discover its possibilities when running
on our Banana Pi device.
The following Linux-based operating systemsso-called distributionsare used by the majority
of the Banana Pi user base and are supported officially by the manufacturer:
Raspbian: This is a distribution based on Debian, which was initially produced for the
Raspberry Pi (hence the name). As a lot of Raspberry Pi owners are running Raspbian
on their devices while also experimenting with the Banana Pi, LeMaker ported the
original Raspbian distribution to the Banana Pi. Raspbian also comes with an LXDE
desktop by default.
Bananian: This too is a Debian-based Linux distribution optimized exclusively for the
Banana Pi and its siblings.
All of the aforementioned distributions are based on the well-known distribution, Debian.
Besides the huge user base, all Debian-based distributions use the same package manager
Apt (Advanced Packaging Tool) to search for and install new software, and all are similar to
use. In the upcoming recipes, we are going to use Raspbian. However, most recipes will be
valid for the other Debian-based distributions.
Chapter 1
There are still more distributions that are officially supported by LeMaker, such as Berryboot,
LeMedia, OpenSUSE, Fedora, Gentoo, Scratch, ArchLinux, Open MediaVault, and OpenWrt.
All of them have their pros and cons or their specific use cases. If you are an experienced
Linux user, you may choose your preferred distribution from the mentioned list, as most of
the recipes in this book are similar to, or even equally usable on, most of the Linux-based
operating systems.
Moreover, the Banana Pi community publishes various customized Linux distributions for
the Banana Pi regularly. The possible advantages of a customized distribution may include
enabled and optimized hardware acceleration capabilities, supportive helper scripts,
fully equipped desktop environments, and much more. However, when deciding to use a
customized distribution, there is no official support by LeMaker and you have to contact
the publisher in case you encounter bugs, or need help.
Getting ready
The following components are required for this recipe:
Banana Pi
A computer with an SD card reader/writer (to write the image to the SD card)
Furthermore, you are going to need an Internet connection to download a Linux distribution
or Android.
A few optional but highly recommended components are:
You can acquire these items from various retailers. All items shown in the previous two
pictures were bought from an online retailer that is known for originally selling books.
However, the Banana Pi and the other products can be acquired from a large number
of retailers. It is recommended to get a USB power supply with 2000mA (2A) output.
6
Chapter 1
How to do it
To download an operating system for Banana Pi, follow these steps:
1. Download an image of your desired operating system. We are going to
download Android and Raspbian from the official LeMaker image files website:
http://www.lemaker.org/resources/9-38/image_files.html.
The following screenshot shows the LeMaker website where you can download the
official images:
2. If you are clicking on one of the mirrors (such as Google Drive, Dropbox, and
so on), you will be redirected to the equivalent file-hosting service. From there,
you are actually able to download the archive file.
3. Once your archive containing the image is downloaded, you are ready to unpack the
downloaded archive, which we will do in the upcoming recipes.
See also
LeMaker image files. This is the official location for operating systems by the
manufacturer of the Banana Pihttp://www.lemaker.org/portal.
php?mod=list&catid=4
Getting ready
To prepare your image and the SD card on Windows, you will usually need the following
software ingredients:
7-Zip
SD Formatter
How to do it
In the upcoming steps, we will unpack the archive containing the operating system image
for the Banana Pi and write the image to the SD card:
1. Open the downloaded archive with 7-Zip. The following screenshot shows the
7-Zip application opening a compressed .tgz archive:
Chapter 1
2. Unpack the archive to a directory until you get a file with the file extension .img. If
it is .tgz or .tar.gz file, you will need to unpack the archive twice. The following
screenshot shows the final image file with the file extension .img and the unpacked
and compressed .tgz archive:
3. Create a backup of the contents of the SD card as everything on the SD card is going
to be erased unrecoverablely.
4. Open SD Formatter and check the disk letter (E:\ in the following screenshot).
6. When everything is configured correctly, check again to see whether you are using the
correct disk and click Format to start the formatting process.
10
Chapter 1
Now you can insert the SD card into your Banana Pi and power it up. After the booting
process, you will see the Android operating system.
Once the burning process is done, you are ready to insert the freshly prepared SD card
containing your Linux operating system into the Banana Pi and boot it up for the first time.
How it works
Some images are archived using Linux archive formats, which Windows may be unable
to unpack natively. Therefore, it might be necessary to install a tool which is capable of
unpacking the archive. In this recipe, we use 7-Zip to unpack the archives, which is open
source software and is easy to use. Some images are also available as ZIP files, which
can also be unpacked using 7-Zip or Windows Explorer. To extract the image from a ZIP
file using the Windows Explorer, right-click on the ZIP file and select Extract all.
Linux archives usually have to be unpacked twice. Firstly, to uncompress the archive
(.tar.gz or .tgz) and secondly, to unpack the actual tar archive (which is used to collect
files into one uncompressed file). On Linux, you will be able to unpack the packed archives,
and uncompress the compressed archives with one command.
11
See also
Win32 Disk Imager is an open source tool we use to write the operating
system images to SD cards. You can get it for the Windows platform at
http://sourceforge.net/projects/win32diskimager/
Getting ready
To prepare your image and the SD card on Linux, you will only need the following
software ingredients:
The dd program
12
Chapter 1
How to do it
The following steps are required to unpack the image archive and write the image to the
SD card:
Use the following dd command very carefully. The dd command will
overwrite anything on the output (the of parameter). In fact, you can
damage your computer, if you choose the wrong output. So, make
sure that the value of the parameter of is definitely the SD card.
1. Unpack the downloaded .tar.gz or .tgz archive using the following command:
$ tar -xzvf Raspbian_For_BananaPi_v1412.tgz
2. If you have downloaded a .zip file, you use the following command:
$ unzip Raspbian_For_BananaPi_v1412.zip
3. Determine how your SD card is recognized by the system. You can check the correct
path of your SD card by using the following command:
$ sudo fdisk -l
To determine the correct device, you can compare the results before
and after you plug in the SD card.
The commands used in the next steps assume that your SD card is
recognized as /dev/mmcblk0.
On some systems, the SD card is automatically mounted after the writing process.
Unmount the partitions of the SD card again:
$ sudo umount /dev/mmcblk0*
When the writing process is finished, you can eject your SD card and put it into the SD slot of
your Banana Pi.
13
How it works
On Linux, you also need to unpack an image file and write the image to the SD card. Luckily,
these tasks are much quicker and more easily done on the command line and you usually do
not need to install additional software.
In fact, to unpack the image, you need the tar command and to write an image to the SD
card you need the dd (disk dump) command. The tool tar is a program to pack or unpack
archive files. The tool dd is a utility to convert and copy files from a source (the input filethe
if parameter) to a destination (the output filethe of parameter). In contrast to a normal file
copy, the actual order of the bytes is preserved.
The dd command is executed with root privileges (by using the prefix command sudo) to use
the image file as input, the SD card as output, and to read/write with a block size (the bs
parameter) of one megabyte. That block size value is a safe choice when writing images to or
reading from SD cards. You can also try a block size value of 4M, which results in a faster but
possibly unsuccessful writing process.
You do not need to format the SD card before issuing the dd command as dd also writes the
whole partition information directly to the SD card.
The output parameter has to be the whole SD card (/dev/mmcblk0 in
our previous example). Make sure not to accidentally write to a partition
of the SD card. This means do not use /dev/mmcblk0p1 or the like).
Moreover, depending on your computer, the SD card may be recognized
as /dev/sdX and not /dev/mmcblk0. Use the fdisk -l command
to determine the correct device file as mentioned in this recipe.
The dd command will take some time. If you want to check the
progress, you can issue the following command in another shell:
$ sudo pkill -USR1 -n -x dd
See also
Type in the man dd command into a shell to show the manual page of dd:
$ man dd
Type in the man tar command into a shell to show the manual page of tar:
$ man tar
Type in the man fdisk command into a shell to show the manual page of fdisk:
$ man fdisk
14
Chapter 1
Getting ready
To boot your device, you need the following ingredients:
A wired up Banana Pi
A prepared SD card with an operating system
How to do it
We are going to boot up and shut down the Banana Pi.
Booting up
Do the following steps to boot up your Banana Pi:
1.
2.
3.
4.
5.
The following screenshot shows a new Android installation on the Banana Pi after a
successful boot:
15
16
Chapter 1
How it works
When you have prepared the SD card successfully, you can finally boot up your Banana Pi with
your desired operating system.
The boot sequence should initiate immediately. You will see blinking LEDs. If only the red
LED is lit, you probably made a mistake when writing the image to the SD card or the SD card
is defective.
If you powered off the device previously, you may need to press and hold the Power key
to restart the booting of your device. You will see the boot messages, if you connected your
Banana Pi to a display. When the boot has finished, you are welcomed by your operating
system for the first time.
If you are using the wired Ethernet network interface on Android,
make sure to enable Use Ethernet in the Android settings under
Wireless & Networks | More.
On Linux, you can shut down the device via the shell. If you are on the desktop, you can
access the shell via an application called Terminal (or LXTerminal in case of Raspbian).
The shutdown command expects a mode parameter (-h, that is, halt in this case) and
a time (now). If you want to reboot your device, you can use the mode -r (reboot). As the
shutdown command requires root privileges, we are executing the shutdown command
with the prefix command sudo. This will issue the next commandshutdownto be
executed with root privileges. You will have to enter the password of the user. On most of
the Banana Pi distributions, the default passwords for the default user is bananapi.
Alternatively, you can also power off or reboot your Banana Pi via the LXDE menu or the
appropriate commands. The following are the commands for power off and reboot respectively:
$ sudo poweroff
$ sudo reboot
See also
17
www.PacktPub.com
Stay Connected: