Favorite Social Media Timesink
When you take a break from work, where are you going?
Video clips on TikTok/YouTube
X, Bluesky, Mastodon et al...
Web surfing
I do not get distracted by petty amusements
Open Source / Tech Culture

Off-The-Shelf Hacker: Automating Your Hack

Jun 3rd, 2017 3:00am by
Featued image for: Off-The-Shelf Hacker: Automating Your Hack

Power up an Arduino microcontroller and it will immediately run whatever purpose-built program, called firmware, is loaded into its flash memory.

The program might read a push-button, attached to one of the general-purpose input/output pins (GPIO) and light an LED, attached to another GPIO pin. It’ll loop through the program as long as there’s power. It’s a single-task process and that’s all it does. Turn off the power and the process stops. Plug it back in and the program will start automatically and do the same old thing over and over, just as it did before.

Microcontrollers like the Raspberry Pi, Pine64, CHIP and Artik work differently than the Arduino. They boot into a multiuser, multitasking operating system, generally Linux. Programs reside in either flash memory or on removable media, like a micro-SD card. With Linux, we can download, create, delete, change and run the programs whenever we like.

With these microcontrollers, Linux has to be up and running before we can execute our programs. Although we can most certainly write a “read a Raspberry Pi push button GPIO pin and light an LED” program, using a common programming language, like Python, it won’t just run by itself, like the Arduino, when you power up the Pi.

Today, we’ll learn about a couple of useful ways to make programs run automatically after the Linux boot sequence completes.

Edit /etc/rc.local

The Pi (and most other nano-Linux devices) normally boots into a standard X Windows desktop, when you have a monitor, keyboard and mouse plugged into the board.

But I use a headless (no monitor, keyboard or mouse) Raspberry Pi to power my Steampunk Conference Personality Identification Apparatus. Most people know it as a conference badge:

The badge incorporates a miniature 1.8” color TFT LCD display on the front and doesn’t have a regular monitor, keyboard or mouse. It’s set up to autoplay promotional videos on the tiny screen. The LCD is controlled by a memory-oriented process, called a frame buffer.

You don’t need an X Windows desktop (like LXDE) to run videos with the frame buffer. The MPlayer viewer supports playing videos using the frame buffer, so that’s what I automatically start when the Steampunk Badge boots up.

Here’s the good part. Running MPlayer after power up amounts to nothing more than adding a line to the /etc/rc.local file.

/etc/rc.local runs programs, right after Linux successfully boots into multiuser mode. Just add your programs or scripts to the bottom of the file, save it and the next time you reboot, your program(s) will execute. rc.local is owned by root, so you’ll need to edit it as a regular user (pi, for example) using sudo.

Here’s an example from the Raspberry Pi-powered Steampunk Conference Badge:

In this case, MPlayer starts with the loop option so the video runs continuously. Notice that the -vo option points to the frame buffer device. The -x 160 -y 128 sets the screen size to match the resolution of the 1.8″ LCD display. You’ll also need the “&” at the end so MPlayer runs in the background.

What if we’re using a nano-Linux microcontroller as a notebook machine?

Auto Start with X-Windows

What if we want to automatically start a program, after the Pi boots into a standard LXDE desktop screen? Maybe we’re not running as a conference badge and have the Pi hooked up to a monitor (or one of those cool 7-inch color LCD displays), a keyboard and a mouse.

More good news.

Getting a program to run automatically under the standard X-Windows desktop is straightforward, as well. Assuming you are logged in as pi, the easiest way to get it done, is to edit the /home/pi/.config/lxsession/LXDE-pi/autostart file.

Autostart is part of LXSession, the standard session manager for the LXDE desktop.

Add the program or script, you want to run, to the autostart file, proceeded by an “@”. Here’s an example.

Once, the Pi boots into the LXDE desktop, MPlayer will pop up and run automatically. The “-loop 0” option loops the video forever. the “-fs” option turns on full-screen mode. Hit “ESC” then just click the “X” box, at the top of the window to make the video go away.

Next Steps

We’ve seen a couple of useful ways to start programs automatically on a nano-Linux device. I use the capability to get things going on my conference badge.

The concept could easily be expanded.

A great example might be an auto-stop for the Pi. Everybody knows that you should use “shutdown” to properly power down your Pi. If you just pull the power plug, you risk corrupting the data on the micro-SD card. In rare cases, the Pi may not boot up, if the card is corrupted. Not good.

Why not write a little Python script to run an operating system (OS) call to the “shutdown” command when a GPIO-connected button is pushed. The script could start and loop forever, after boot-up.

You might say that would be “automating to the end.”

Feature image: Gnome screensaver.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.