Perl Raspberry Pi ebook sample chapter 1

The Perl Raspberry Pi ebook campaign is going great so far. As promised, here’s another sample chapter, where we cover the lineup of Raspberry Pi models, as well as some tools you’ll want/need.


Let’s Get Down to Basics

The Raspberry Pi has expanded into a range of devices, which may seem
overwhelming at first. There’s also the matter of which tools you’ll need once
you do end up requiring one.

Raspberry Pi Models

Model A and B(+)

The initial offerings were the Pi Model A and B in 2012. These were both
based on the Broadcom BCM2835 System on a Chip. The Model A had 256MB of
RAM for $25, while the Model B had 512MB for $35.

These models also standardized the layout for most future Pi’s, including the
location of the pin header and HDMI port. These models also include a composite
video connector.

In 2014, the A+ and B+ models were released, with prices reduced to $20 and
$25, respectively. They expanded the pin header, which has been used on other
Pi releases ever since. The separate composite video connector was also removed,
with its functionality still accessible through a 3.5mm connector. The A+ also
shrunk its form factor.

Compute Module

Released in 2014, the Compute Module was designed for industrial use. Instead of
external ports, it uses the same connector as DDR2 SODIMM RAM (but don’t try
plugging it into your laptop). These require a special development kit for
programming.

Zero and Zero W

The Raspberry Pi Foundation made headlines in 2015 when they included an
entire computer bundled with a magazine. The Pi Zero had an MSRP of $5, or
free if you bought the December issue of the MagPi magazine. It introduced a
smaller form factor that’s not much bigger than a stick of gum, and used the
same BCM2835 used in the original Model A and B. In 2017, the Pi Zero W was
released for $10 that included onboard WiFi and Bluetooth, just like the B3/B3+
(see below).

B2 and B3

In early 2015, the Pi B2 was introduced with a new BCM2836 SoC. This upgraded
the CPU from single to quad core, and from 700MHz to 900MHz. It also bumped the
memory to 1GB of RAM. This would be followed in 2016 by the B3, which has a
BMC2837, bringing the clockrate to 1.2GHz. It also gave you built-in
WiFi and Bluetooth, which means you no longer needed a separate dongle to
connect your Pi to the Internet.

Both the B2 and B3 had an MSRP of $35.

B3+

While we wait for the next big iteration, the B3+ was released. It pushed
the clockrate further to 1.4GHz, and now includes dual-band 802.11ac
WiFi. It’s also the first Pi to have gigabit Ethernet. Following tradition,
it’s priced at $35.

The Future?

The Raspberry Pi Foundation isn’t sitting still with the B3+ at the top of the
model lineup, and are hard at work on the next major revision. Hoped-for
improvements are support for USB3, a faster CPU, and breaking the 1GB RAM
limit. We’ll have to wait and see.

Which Model Should You Buy?

The hardware projects in this book can be easily done on any of the more
recent models–Zero (W), Pi2, or Pi3. The Compute Module requires an
extra development kit, which you will probably want to avoid until you have
a specific need and experience working with the more mainstream boards.

If you need to fit the project in a small space, run off of low power, or want
the cheapest option available, the Zero or Zero W are your best options. If
you want speed and don’t need to worry about power usage, the Pi3 is the best
option. If you find a Pi2 for cheap, that’s a perfectly good option, as well.

The Tools You Need

Power Supply

When the Raspberry Pi was first released, many people saw that it used a
micro USB power port, and tried to use their leftover cellphone chargers to
power it.

This did not work.

Cellphone chargers are meant for charging cellphone batteries. Under load,
their voltage may sag below the level needed to keep the Pi running. A
charger rated for 500mA may not be enough when you add a WiFi dongle, a
monitor, a keyboard, and a mouse. It might boot up, but you’ll likely run
into strange problems with no obvious fix due to undervoltage/undercurrent.

The CanaKit micro USB power adapter is the standard way to power the Pi.
It’s rated for 2500mA, which is plenty to run even the Pi3.

WiFi Dongle

Most of the Pi lineup doesn’t have built-in WiFi. If you buy the Pi3 or the
Zero W, you won’t need one. If you can use wired ethernet and have a Pi
with a wired port (not the A(+), Zero (W), or Compute Module), then you can
also skip a WiFi dongle.

Otherwise, there are a number of cheap USB dongles that will work with the Pi.
Edimax sells a small nub adapter. If you need more signal, the EDUP adapter
has an external antenna, and works well with the Pi.

SD Card

The main storage system on the Pi is an SD card (the Pi3 requires
a micro SD card). If you take a quick look at SD card speed classes,
you may be tempted to buy the first Class 10 or U3 card you can find.

Before you do that, note that the speed classes on SD cards refers to
the sequential writing speed. This is good if you were putting your
SD card in a camera, since streaming 60 frames of 4k video every second
takes a lot of sequential writing. Running an operating system off that
SD card does a lot of random access, not sequential. In practice,
you’ll often find that a quality Class 4 card works better than a cheap
Class 10.

Recently, the A1 and A2 performance classes were released. These specify
a minimum number of random IO operations per second, which is exactly
what we want for the Pi. These are worth looking into.

You should look at benchmarks to compare SD cards on the Pi. The Pi’s own
SD card access rate is admittedly limited, so you may see the Pi hit a
bottleneck before the SD card itself is maxed out.

As for size, at least 8GB will do. You can get away with 4GB, but you’ll
find yourself limited at that size.

USB Micro to USB A adapter, and hub (for the Zero and Zero W)

The Zero and Zero W have only one USB port for connecting external
devices, and it’s a micro port. To connect anything, you’ll need a
male micro to female USB A adapter. If you need to connect more than
one device, then you’ll also need a hub, preferably a powered one so
that you’re not drawing too much from the Pi itself.

Soldering Iron

When I was young, I bought a $15 Radio Shack stick soldering iron that
plugged directly into the wall. I was always frustrated at getting bad
solder joints that would never form into the nice little volcano-shaped
mounds you usually see on PCBs. I dreaded soldering pins that were close
together, because it was always a struggle to keep the solder from spilling
over and bridging the connections together. I thought I was bad at
soldering.

I eventually bought a nice soldering iron with a good temperature control
unit. Turned out, I was pretty good at soldering, I just had the wrong
tool from the start.

Where that old stick iron simply heated the element up with power right
from the socket, a temperature controlled iron measures the heat of the tip
and supplies power to keep it near a setting. They heat up much faster,
as well.

The one that I bought is the Kendal 937D, and I still use it as my main
iron. The Hakko FX-888D is one of the more popular models out there,
though I’m not a fan. It’s more expensive than the Kendal, and setting
the temperature is more cumbersome.

Solder

Be sure to get solder meant for electronics. Plumbing solder can damage
electronics components. Other than that, something around 0.8mm
thickness will do fine.

Solder Sucker and Wick

Even with a good iron, I sometimes make mistakes. The Solder Sucker uses a
plunger or a squeeze bottle to suck the molten solder away. The wick uses
capillary action to do the same. I tend to prefer the sucker method, but
this is personal opinion. Either option is cheap, so it doesn’t hurt to have
both around.

Solderless Breadboard

Not everything has to be soldered together. A solderless breadboard has
pins connected in rows, which is nice for prototyping before laying
everything down on a circuit board and making it permanent. Buy a
few different sizes.

Jumper Wires

These connect the Pi and other devices together using either the pin
headers or a solderless breadboard. You’ll need male-to-male,
male-to-female, and female-to-female connectors. These are often
available in a variety pack, in a flat strip where the individual
wires can be pulled apart, and often you find these wire variety
packs bundled with a breadboard (mentioned above).

Resistors, LEDs, Switches, and Capacitors

These four components are the bread and butter of electronics. You’ll want
a variety of all these. 100 Ohm resistors are especially needed, since
this is a good value for protecting LEDs from pulling too much current.
Other than that, assortment packs are readily available for all of these.

Breakout Boards

Modern electronics manufacturing has moved towards surface mount
components, which can’t be plugged into a breadboard directly. They need
a specific footprint on a PCB.

Fortunately, two of the major hobbyist electronics suppliers, Sparkfun
and Adafruit, sell breakout boards for many common components. These
are a small PCB with a pin header that can be plugged into a breadboard,
and from there into a Pi using jumper wires.

We’re going to be using the TMP102 temperature sensor chip in this book.
It’s available in a breakout board from both Sparkfun and Adafruit. The
chapter on GPS was tested using the Adafruit Ultimate GPS Breakout, though
any GPS unit that puts out NEMA over a serial connection should work.

Pi Camera

The chapter on taking pictures will be using the Pi camera. It uses a
special connector on the Pi, and is generally higher quality than any
USB webcam you could buy for the same price.

Perl Raspberry Pi ebook sample chapter: intro

With the campaign for the Perl Raspberry Pi ebook underway, we’re releasing a few sample chapters. First one for today is a short intro chapter. Tomorrow, we’ll be releasing one with more meat, covering the basics of the Raspberry Pi range and some other tools you might need to get started. For now, here’s the intro:


Introduction

Welcome! If you’re reading this, you’ve probably been using Perl for a while
and heard about the wonderful Raspberry Pi. If you haven’t programmed Perl
before, I suggest starting with Learning Perl by Randal L. Schwartz, brian d
foy, and Tom Phoenix. With that said, we try to keep the examples in this book
as simple and concise as possible, sticking to the most basic of expressions
wherever possible. Even without previous Perl experience, you
should be able to grasp the concepts we’ve presented within if you’ve done
any programming in other languages.

No previous electronics experience is necessary. We’ll be covering some of the
basics where appropriate. Most of the projects can be completed without any
soldering. In others, minimal soldering skills may be necessary. However,
being able to solder is a valuable skill to have if you are to grow further
in the field of electronics.

About the Raspberry Pi

In 2012, the Raspberry Pi Foundation was started by employees of Broadcom,
intending to use one of Broadcom’s inexpensive System on a Chip (SoC) fabs
to create a low-cost computer for education. Hobbyists quickly grabbed them
up and started hacking away. Today, the line has expanded to several models,
ranging from the $5 Pi Zero, to the $35 Pi3 (most recently, the Pi3B+).

About Perl

Perl was released by Larry Wall in 1987 inspired by a combination of several
other programming languages. A major revision to Perl 5 was done in 1994. Work
on Perl 6 began in 2000 as a completely separate language from Perl 5 (often
referred to as its “sister” language). Since then, work on Perl 5 has
continued. We’ll be using Perl 5 in this book. We recommend ensuring the most
recent version of Perl is installed before attempting the projects outlined in
this book, that version being 5.26 at the time of writing.

The language was important for the early web, being used in many of the first
CGIs, a simple way to write server applications. Since then, it has evolved
with the rest of the web and is still used by several large companies today.

It also continues as popular language for managing systems, and is installed
by default on the Raspberry Pi.

Keep WiFi Alive on Raspberry Pi

So solving my connection problems to a Pi by turning off power savings didn’t work. I’m now using a crude but effective method where the Pi pings the gateway every 2 minutes.

Start by adding this script to /etc/keepalive.sh:

This script works in an infinite loop, sending a single ping every 120 seconds. Call chmod 755 /etc/keepalive.sh to make it executable. Next, call it from /etc/rc.local:

Which will run the script on the next reboot. Call /etc/keepalive.sh & in a shell to start it running, and you’re done.

So far, this seems to make ssh connections start immediately every time. I’d like to figure out what the actual problem is, but this will do.

Running Apache files on a Raspberry Pi ramdisk with mod_perl

When used as a web app server, the Raspberry Pi often hosts a small number of static files that rarely change. Although the Raspberry Pi Model B(+) only has 512 MB of RAM, using 10MB for a ramdisk is usually more than enough.

Files will be copied by Apache at startup. If you make changes to these files, you’ll either need to copy them manually, or restart Apache.

Start by making the directory mount point. I used /var/www-ramdisk:

Modern linux systems mount ramdisks through ‘tmpfs’, so add an entry to /etc/fstab:

Replace [UID] and [GID] with the respective uid and gid that Apache runs under for your system. On the default Raspbian install, this will be the www-data user and group.

Run mount -a and the ramdisk should appear (use df to confirm).

Next comes the Apache config. Somewhere in the config, you’ll need a PerlConfigRequire:

Create /etc/apache2/mod_perl_post_config.pl, an write in:

This shells out during startup to recursively copy everything from the default Apache docroot to the ramdisk.

Now grep through your Apache config, and change all paths to /var/www and replace it with /var/www-ramdisk. Lastly, restart Apache with:

Check the files with ls -l /var/www-ramdisk and you should see everything that’s in /var/www.

Edit: Forgot to credit domoticz.com, where I got much of the fstab setup.