Using the Rasp Pi to make Halloween really spooky

Installing wiringPi

The wiringPi library gives you easy access to the GPIO interface of the Rasp Pi. Gordon Henderson wrote the software and continues to maintain it. WiringPi contains simple commands and a C API for reading from and writing to the I/O ports. WiringPi also has drivers for the I2C bus, as well as the SPI bus.

To add the library to the system, use the commands shown in Listing 1. The Rasp Pi needs some time for the build. Afterward you should test whether the installation was successful (Listing 2). For more detailed information on wiringPi, go to the homepage for the project [6].

Listing 1

Add wiringPi Library

$ sudo apt-get update
$ sudo apt-get upgrade
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build

Listing 2

Test Installation

$ gpio readall
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 ||  2 |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 ||  4 |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 ||  6 |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 ||  8 | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 |  1  | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 |  4  | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 |  5  | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 |  6  | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  |  8  |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  |  7  |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  |  1  |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+

Setting Up Java and Pi4J

Once you have installed the wiringPi library on the Rasp Pi, the next step is to install the current Java Development Kit (JDK) from Oracle. This can be downloaded directly from the Oracle homepage [7]. Using a program like SFTP, Linux users can transfer the package onto the Rasp Pi. Windows users should perform this step with WinSCP [8].

To install the JDK on the Rasp Pi, you should first create a new directory (Listing 3). The Unix filesystem hierarchy standard (FHS) designates the /opt/ directory for additional software. Lines 1 to 3 copy the JDK to precisely this directory. Now, you should tell the system where it will find the new Java version (line 4) and, for the final step, that this is what you want to use from now on (line 5).

Listing 3

Install JDK

$ sudo mkdir -p -v /opt/java
$ gunzip jdk-8u51-linux-arm-vfp-hflt.tar.gz
$ sudo tar -xvf jdk-8u51-linux-arm-vfp-hflt.tar -C /opt/java/
$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.8.0_51/bin/java" 1
$ sudo update-alternatives --set java /opt/java/jdk1.8.0_51/bin/java

At this point, the Oracle JDK is ready to use. You should begin with a small test to determine whether the fresh installation works error free (Listing 4). One small item needs to be modified: Some programs expect a correctly set JAVA_HOME environment variable. You can modify this manually in the current session using the following command:

$ export JAVA_HOME="/opt/java/jdk1.8.0_51/bin"

Listing 4

Test JDK

$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b07)
Java HotSpot(TM) Client VM (build 25.51-b07, mixed mode)
$ javac -version
javac 1.8.0

For the first attempts, it is sufficient to include the variable in your own ~/.bashrc file instead:

$ echo 'export JAVA_HOME="/opt/java/jdk1.8.0_51/bin"' >> ./.bashrc

After the next login, the variable will then be in the environment for your account. To set the variable system-wide for all users, use your root rights to adapt the /etc/environment file accordingly.

Now it is time to install the Pi4J library. The quickest way to do this is to load the library directly from the Internet, compile it, and create a class file:

$ curl -s get.pi4j.com | sudo bash
$ pi4j -c <Java program>
$ pi4j -r <Class file>

The Pi4J library developers have written the Pi4J utility script to make life easier for users. It sets the class path correctly so that the small Java program can be speedily compiled and tested. The command pi4j --? brings up extensive help information. The pi4j -c command compiles programs. If no error message comes back, the command will create a class file of the same name in the current directory for the compiled code. This file can then be immediately executed.

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content