Basics of the Raspberry Pi – Turning a light on and off

Basics of the Raspberry Pi – Part 3
Part 1 – Basic Setup
Part 2 – How it all works 

Turning a light on and off – Part 3


In this blog we will make a small Java application that turns our light on and off. We will need a few things before we get started.

  1. Eclipse
  2. sFTP Client (Filezilla is a great on that is free)
  3. SSH Client/Terminal
    • If you are on windows that I would recommend downloading PUTTY

The first thing that we will do is Open Eclipse and create a new Java Project.  We will need to add one dependency to this project; the easiest way to do that is to convert your Java Project to a Maven Project.  This only takes a second, you simply right click your project, go to the configuration option and then click “Convert to Maven Project”.

After you convert your project to a Maven Project you will notice a pom.xml file is automatically added to your project. All you have left to do is add the following element to this pom.xml file under the Project element.

<dependencies>
  <dependency>
     <groupId>com.pi4j</groupId>
     <artifactId>pi4j-core</artifactId>
     <version>1.1</version>
  </dependency>
</dependencies>

Now right click your project, go to the Maven menu and then click “Update Project”. Now you have the PI4J dependency in your project.  Now lets write some code!

Lets start by adding our Java class.  Right click your “src” folder and select “new” then select “class”.  We will name this new class “main”.

So the goal of this application will be to build a small console application that will turn the light on, wait 5 seconds and then turn the light off.

Now lets setup our Java app to run as a console application.  All we need to do is add this basic code.

public class main {
	  public static void main(String[] args) throws InterruptedException {
	        System.out.println("This is my console app");
	  }
}

Lets add our dependency to the application.

import com.pi4j.io.gpio.*;

Lets just jump in and add all of our code; I will explain what it all does below.

  public static void main(String[] args) throws InterruptedException {

        // intializing our gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        //Turn the LED on
        System.out.println("Turning the LED ON.");
        // Wire up our GPIO 25 pin to a variable in our code
        // We are also turning on the LED with the PinState.HIGH parameter
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, "LED-25", PinState.HIGH);

        // Lets leave the LED on for 5 seconds
        System.out.println("Waiting for 5 seconds before turning the LED off");
        Thread.sleep(5000);

        // turn off gpio pin #
        System.out.println("Turning the LED off");
        pin.low();
        
        // clean up tasks before we exit thie application
        gpio.shutdown();

        System.out.println("goodbye :)");
  }

We only have about 4 lines that do anything significant in the code above.

1.  final GpioController gpio = GpioFactory.getInstance();
This simply initialized our GPIO library so we can start interacting with the 40 pin connector on the Raspberry Pi

2.  final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, “LED-25”, PinState.HIGH);
This is probably the most important thing to not only get right but understand what is going on.  What we are doing is simple; we are binding the “pin” to one of the 40 pins on the board.  This is key on how to understand which pin you are binding to.  Here is a “cheat sheet” that I found on the pi4j website; note that the GPIO number you need to use is labeled next to the value you actually see on the GPIO Extension Board. GPIO Cheat Sheet I would recommend printing this off for future projects

3.  pin.low();
We are turning the light off by essentially breaking the circuit.

4.  gpio.shutdown();
We need to clean up our resources and let go of any control we have acquired over the board so other processes can control this circuit.

We are done writing our code; now we just need to compile our application, transfer it to our Raspberry Pi and run it.


Lets compile our code!
We want to basically export our code into an executable JAR file.  Lets right click our project and select the “Export” option.
You will want to ensure that you are exporting this as a “Runnable Jar File” as show in the image below

When we click next we just want to select a run configuration and location to output our JAR file.  If you don’t have a configuration to select, the easiest was to create one is to just run the application in debug mode once.  Eclipse will automatically create one for you.

Now just click the “Finish” button and Eclipse will create your Jar file!


Lets transfer our application/JAR file!
Transferring the Jar file should be fairly basic as long as you enabled ssh on part 1 of this blog.  The first thing you need to do is figure out the IP Address of your Raspberry Pi, assuming it is on your network.  The easiest way to do this is to open a terminal and type “ifconfig”; you should be able to easily see the IP of your Pi from there.

Now that we have the IP Address of the Pi, on the device that has the Jar file, you need to transfer the file using sftp/ssh.  There are tons of tools for this; I personally prefer to use FileZilla.  When you launch FileZilla you can perform a “Quick Connect” and enter the following information.

Host:  {the IP you found from the instructions above}
Username: pi
Password: raspberry
Port: 22


Lets run our application!
Now that the Jar file is on the Raspberry Pi, lets hop back onto the Pi (or ssh in).  Open a new terminal and navigate to the directory that you transfered the Jar file to.  Now type the following command

sudo java -jar myjarfile.jar

You should now see your LED turn on and off!  The ONLY reason we had to run this with high level permission (sudo) is because the GPIO library requires that to interact with the hardware.

I hope this blog helped you understand the entire process better!  Honestly once you understand the basics taking it to the next level is a piece of cake/pi. (sorry for the incredibly dumb play on words).  My next Raspberry Pi will show you how to use sensors and the camera (Night vision/IR Camera).  In regards to code it is not much more and the concepts are almost identical to what we went over here.

Basics of the Raspberry Pi – Part 3
Part 1 – Basic Setup
Part 2 – How it all works