Exciting Journey with Perl on Raspberry Pi

Exciting Journey with Perl on Raspberry Pi

Recently, during my talk at SVPerl (Silicon Valley Perl Meetup) about running the Perl language on Raspberry Pi, someone asked me, “I’ve heard that Raspberry Pi should use Python, is that true?” I was happy to answer him; this is a common misconception. Raspberry Pi can support any language: Python, Perl, and other languages that come with the official Raspbian Linux installation.
— Ian Kluft
This article navigation

Raspberry Pi, at your command.

Recently, during my talk at SVPerl (Silicon Valley Perl Meetup) about running the Perl language on Raspberry Pi, someone asked me, “I’ve heard that Raspberry Pi should use Python, is that true?” I was happy to answer him; this is a common misconception. Raspberry Pi can support any language: Python, Perl, and other languages that come with the official Raspbian Linux installation.

It seems impressive, but it’s actually quite simple. The creator of Raspberry Pi, Professor Eben Upton from the UK, once said that the ‘Pi’ in Raspberry Pi was intended to sound like Python because he liked that language. He chose it as the introductory language for children. However, he and his team created a general-purpose computer. Open-source software imposes no restrictions on Raspberry Pi. We can run whatever we want, entirely at our discretion.

In addition to this point, I also want to introduce my “PiFlash” script in this talk and article. Although it is written in Perl, you don’t need to know much about Perl to automate the flashing of the Raspberry Pi system onto an SD card under Linux. This is more user-friendly for beginners, preventing them from accidentally wiping their entire hard drive while flashing the SD card. Even advanced users can benefit from its automation, including myself, which is why I developed this tool. Similar tools exist for Windows and Mac, but the Raspberry Pi website does not provide similar tools for Linux users. However, now there is one.

The open-source software community has long had a tradition of reinventing the wheel because they always advocate “scratching their own itch” to solve problems. This approach was mentioned in Eric S’s 1997 paper and 1999 book “Cathedrals and Bazaars[1]” which defined the methodology of open-source software. I wrote this script to meet the needs of Linux users like me.

Download the system image

To start your journey with Raspberry Pi, you first need to download an operating system for it. We call this a “system image” file. Once you’ve downloaded it to your desktop, laptop, or even another Raspberry Pi, you need to write or “flash” it onto your SD card. The details can be found in the online documentation. Doing this manually requires some skill; you need to flash the system image onto the entire SD card, not just one partition. The system image must independently contain at least one partition, as the Raspberry Pi boot process requires a FAT32 file system partition to start the system boot. Other partitions can be of any type supported by the operating system kernel.

In most Raspberry Pis, we run certain distributions that use the Linux kernel. There are a series of commonly used system images for Raspberry Pi that you can download. (Of course, nothing stops you from reinventing the wheel yourself.)

The Raspberry Pi Foundation recommends the “NOOBS[2]” system for beginners. It stands for “New Out of the Box System,” and it sounds like the term “noob,” which colloquially means “newbie.” NOOBS is a Raspberry Pi-based Linux system that provides you with a menu to automatically download and install several other system images on your Raspberry Pi.

Raspbian Linux[3] is a customized version of the Debian Linux distribution for Raspberry Pi. It is the officially supported Linux distribution developed for Raspberry Pi and maintained by the Raspberry Pi Foundation. Almost all Raspberry Pi drivers and software are first tested on Raspbian before being released to other distributions. Its default installation includes Perl.

Ubuntu Linux (and its community edition, Ubuntu MATE) also supports Raspberry Pi as one of its platforms for ARM (Advanced RISC Machines) processors. RISC (Reduced Instruction Set Computer) Ubuntu is a commercially supported open-source branch of Debian Linux and also uses the DEB package manager. Perl is also included. It only supports Raspberry Pi 2 and 3 with 32-bit ARM7 or 64-bit ARM8 processors. The ARM6 Raspberry Pi 1 and Zero have never been supported by the Ubuntu build process.

Fedora Linux[4] supports Raspberry Pi 2, and Fedora 25 supports 3. Fedora is an open-source project under Red Hat. Fedora serves as a foundation, with the commercial version RHEL (Red Hat Enterprise Linux) adding commercial software packages and support on top, so its software, like all Red Hat-compatible distributions, comes from RPM (Red Hat Package Manager) packages. Like other distributions, it also includes Perl.

RISC OS[5] is a single-user operating system specifically for ARM processors. If you want a simpler desktop with fewer features than a Linux system, you might consider it. It also supports Perl.

RaspBSD[6] is a FreeBSD distribution for Raspberry Pi. It is a Unix-based system rather than Linux. As part of open-source Unix, it continues the functionality of Unix and shares many similarities with Linux, including a similar system environment brought about by open-source software, including Perl.

OSMC[7], the Open Source Media Center, and LibreElec[8], a TV entertainment center, are both based on the Kodi entertainment center running on the Linux kernel. It is a compact, specialized Linux system, so don’t expect it to support Perl.

Microsoft Windows IoT Core[9] is a new member that runs only on Raspberry Pi 3. You need a Microsoft developer account to download it. As a Linux geek, I don’t pay attention to it. My PiFlash script does not support it yet, but if you’re looking for it, you can check it out.

PiFlash script

If you want to see the Raspberry Pi SD card flashing guide[10], you can find tools to complete the flashing task that need to be downloaded for Windows or Mac systems. However, for Linux systems, there are only a series of manual operation recommendations. I have done this manually too many times; it easily triggers a developer’s instinct to automate this process, and this is the origin of the PiFlash script. It is a bit challenging because there are so many ways to configure Linux, but they are all based on the Linux kernel.

I always feel that the biggest potential mistake of manual operation is probably accidentally wiping out some device instead of just the SD card, and completely erasing what I intended to keep on the hard drive. I mentioned in my SVPerl talk that I was surprised to find people in the audience who had made this mistake (and weren’t afraid to admit it). Therefore, one of the purposes of PiFlash is to protect beginners from accidentally erasing devices other than the SD card. The PiFlash script will also refuse to overwrite devices containing already mounted file systems.

For experienced users, including myself, the PiFlash script also provides a convenient automation service. After downloading the system image, I don’t need to extract or decompress the system image from zip format. PiFlash can directly extract it, regardless of the format, and flash it directly onto the SD card.

I have published PiFlash and its guide[11] on GitHub.

Command line usage is as follows:

piflash [--verbose] input-file output-device
piflash [--verbose] --SDsearch

The input-file parameter is the system image file you want to write, as long as it is an image you downloaded from the Raspberry Pi distribution website. The output-device parameter is the block device path of the SD card you want to write to.

You can also use the --SDsearch parameter to list the names of SD card devices mounted in the system.

The optional --verbose parameter can output all program status data, which is useful when you need help or when delivering bug reports and debugging. It is what I used during development.

The following example shows how I used the script to write the still zip archived Raspbian image to the SD card located at /dev/mmcblk0:

piflash 2016-11-25-raspbian-jessie.img.zip /dev/mmcblk0

If you have specified /dev/mmcblk0p1 (the first partition of the SD card), it will recognize that this partition is not a correct location and refuse to write.

Identifying which device is the SD card in different Linux systems is a technical task. The mmcblk0 type in my laptop is based on a PCI SD card interface. If I use a USB SD card interface, it will be /dev/sdb, which is difficult to distinguish in multi-hard drive systems. However, only a few Linux block devices support SD cards. PiFlash will check the parameters of the block device in both cases. If all fails, it will assume that a writable, removable USB drive with the correct physical sector count is an SD card.

I think this should cover most situations. However, if you use an SD card interface that I don’t know, I would love to hear from you. Please add the --verbos --SDsearch parameter to the output information so that I can know your system’s current environment. Ideally, if the PiFlash script can be widely used, we can build an open-source community to help as many Raspberry Pi users as possible.

CPAN modules for Raspberry Pi

CPAN[12] (Comprehensive Perl Archive Network) is a worldwide download mirror containing various Perl modules. They are all open-source. Many modules in CPAN have stood the test of time. For thousands of tasks, you don’t need to reinvent the wheel; you can just utilize code that others have already published. Then, you can also submit your new features.

Although Raspberry Pi is a fully equipped Linux system that supports most CPAN modules, here I want to emphasize things developed specifically for Raspberry Pi hardware. Generally, they are used in measurement, control, and robotics embedded systems. You can connect your Raspberry Pi to external electronic devices through GPIO (General-Purpose Input/Output) pins.

Modules that can use Raspberry Pi GPIO pins include: Device::SMBus[13], Device::I2C[14], Rpi::PIGPIO[15], Rpi::SPI[16], Rpi::WiringPi[17], Device::WebIO::RaspberryPI[18], and Device::PiGlow[19]. Embedded modules supported by Raspberry Pi include: UAV::Pilot::Wumpus::Server::Backend::RaspberryPiI2C[20], RPI::DHT11[21] (temperature/humidity), RPI::HCSR04[22] (ultrasonic), App::RPI::EnvUI[23], RPi::DigiPot::MCP4XXXX[24], RPI::ADC::ADS[25], Device::PaPiRus[26], and Device::BCM2835::Timer[27].

Examples

Here are some examples of what we can do with Perl on Raspberry Pi.

Example 1: Play video using PiFlash on OSMC

In this example, you will practice how to set up and run a Raspberry Pi using the OSMC operating system.

◈ Go to RaspberryPi.Org[28] download area and download the latest version of OSMC.
◈ Insert an empty SD card into your Linux computer or laptop. The first generation of Raspberry Pi uses a full-size SD card; all others use microSD, and you may need a universal adapter to insert it.
◈ Run the cat /proc/partitions command before and after inserting to see the device names assigned by the system. It might look like /dev/mmcblk0 or /dev/sdb, use the following command to flash the correct system image to the SD card: piflash OSMC_TGT_rbp2_20170210.img.gz /dev/mmcblk0.
◈ Eject the SD card, insert it into the Raspberry Pi, connect it to an HDMI monitor, and power it on.
◈ Once OSMC is set up, insert a USB device with some videos on it. For demonstration purposes, I will use the youtube-dl program to download two videos. Run youtube-dl OHF2xDrq8dY (a Bloomberg introduction to the UK’s high-tech industry, including Raspberry Pi) and youtube-dl nAvZMgXbE9c (CNet’s “Top Five Raspberry Pi Projects”). Download them to the USB, then unmount and remove the device.
◈ Insert the USB device into the OSMC Raspberry Pi. Click on the video option to enter external devices.
◈ As long as you can play videos on the Raspberry Pi, congratulations, you have completed this exercise. Enjoy.

Example 2: Script for random video playback in a directory

This example will use a script to play videos in a directory on the Raspberry Pi in random order. Depending on the videos and the placement of the devices, this can be used for kiosk display purposes. I wrote this script to showcase indoor experience videos.

◈ Set up Raspberry Pi to boot Raspbian Linux. Connect to an HDMI monitor.
◈ Download the do-video script[29] from GitHub. Place it on the Raspberry Pi.
◈ Follow the installation guide on that page. The main thing is to install the omxplayer package, which can smoothly play videos using the Raspberry Pi hardware video acceleration feature.
◈ Place some videos in the Videos directory in your home directory.
◈ Run do-video, and it should start playing videos.

Example 3: Script to read GPS data

This example is more in-depth and targeted. It demonstrates how Perl reads data from external devices. In my GitHub repository mentioned in the previous examples, there is a gps-read.pl script. It can read NMEA (National Marine Electronics Association) data from GPS through a series of ports. The page also includes tutorials, including the parts used from AdaFruit Industries, but you can use any GPS that outputs NMEA data.

Through these tasks, I hope you can use Perl on Raspberry Pi just like any other language. I hope you enjoy it.

Author’s profile:

Ian Kluft – Since school, Ian has loved programming and flying. He has been dedicated to Unix work. He switched to Linux six months after the Linux kernel was released. He has a Master’s degree in Computer Science and holds the CSSLP certification (Certified Secure Software Lifecycle Professional). On the other hand, he is also a navigator and a certified aircraft captain. As a certified radio enthusiast for over twenty-five years, he has experimented with various electronic devices in recent years, including Raspberry Pi.

via: https://opensource.com/article/17/3/perl-raspberry-pi

Author: Ian Kluft[31] Translator: Taylor1024 Proofreader: wxy

This article is originally compiled by LCTT and proudly presented by Linux China

LCTT Translator
Exciting Journey with Perl on Raspberry Pi

Taylor1024
Total translations: 4 articles
Contribution time: 144 days

Recommended articles

< Swipe left and right to view related articles >

Exciting Journey with Perl on Raspberry PiExciting Journey with Perl on Raspberry PiExciting Journey with Perl on Raspberry PiExciting Journey with Perl on Raspberry PiExciting Journey with Perl on Raspberry Pi

Click on the image, enter the article ID, or scan the QR code to go directly

Leave a Comment

×