You are on page 1of 14

2013

Running Java on Raspberry Pi

Author: Praveen Kumar R

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Introduction: Lot of people asking me whether it is possible to run java on raspberry pi.Yes it is possible.In this article you will get a clear idea of installing Java on Raspberry Pi as well as here we are going to create a simple Java UI to access Pi GPIO.For making the bridge between GPIO and UI i am taking the greater advangtage of Pi 4j project. Installation procedure: JDK is not currently compatabile for Raspbian Wheezy so install Raspberry Pi Debian wheezy (soft float image)Recommended.You can download it from the www.raspberrypi.org/downloads/ Installing JDK: Step 1: Here we are installing Oracle Java Development Kit. You can click the below link for the downloads section of Java SE.click the Java Platform(JDK) as per the image shown below.

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Step 2: Accept the License agreement in the LinuxARM JDK7 SE distribution

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Step 3: Then download the (jdk-7ux-linux-arm-sfp.tar.gz) as per the image given below.

Step 4: After downloading the file extract it using tar command.Type the follwing command in command prompt.Extracting will take few seconds to complete. tar xvzf ~/jdk-7u21-linux-arm-sfp.tar.gz

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Step 5: Lets create a new directory so that we will move our JDK file to that directory. sudo mkdir /opt/java Step 6: After unpacking the tar file its time to move the JDK file to java directory that we created already earlier under /opt/java sudo mv -v ~/jdk1.7.0_21 /opt/java

Step 7: And finally to complete the JDK installation we need to let the system know there is a new JVM installed and where it is located. Type the following command to perform this task. sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.7.0_21/bin/java" 1

Step 8: Also we need to inform the system that this JDk is the default run time for the system. To perform this operation type the followign command. sudo update-alternatives --set java /opt/java/jdk1.7.0_06/bin/java

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Thats all the Oracle JDK is installed and we can run the java program.To test the java version type the following command in the prompt. java version

JAVA_HOME ENVIRONMENT VARIABLE Some Java programs require a JAVA_HOME environment variable to be configured on the system. Add the following line to you "/etc/environment" using your favorite text editor(nano/gedit/leafpad/emacs/geany). JAVA_HOME="/opt/java/jdk1.7.0_21"

Also, edit your "~/.bashrc" file using this command and the following two lines.
sudo nano /etc/bash.bashrc export JAVA_HOME="/opt/java/jdk1.7.0_21" export PATH=$PATH:$JAVA_HOME/bin

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Reboot your system for applying the changes. sudo reboot

Pi4J Pi4J act as bridge between the native libraries and Java for full access to the Raspberry Pi.while creating UI for pi using java it easy for you to access the GPIO pins.Installing Pi4J in your Pi will not take more than ten minutes(depends on your internet speed ).Lot of examples come with the Pi4J package itself. Install Pi4J Download the latest pi4j.jar build using the following command prompt. wget http://pi4j.googlecode.com/files/pi4j-0.0.5.deb

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Perform the installation sudo dpkg -i pi4j-0.0.5.deb

This will install the Pi4J libraries and example source files to the following directory. /opt/pi4j/lib /opt/pi4j/examples

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Compilation: While compiling the java code make sure whether you include the Pi4J lib folder in the classpath: javac -classpath .:classes:/opt/pi4j/lib/'*' filename.java Execution: sudo java -classpath .:classes:/opt/pi4j/lib/'*' filename Default examples: If you want to run the default java program go the following directory and run it. We have to compile those examples and run it.If you want to confirm the working of JDK lets go through the simple LED blinking program is there in the /opt/pi4j/examples/ just the compile the ControlGpioExample and run the file using the above command. cd /opt/pi4j/examples ./build

Home Automation Using Raspberry Pi based on Java


Raspberry Pi home automation system that will switch lights on or off .If we are moving towards a smart home it requires a serious investment of money for hardware,but if we have a little power house computer like raspberry pi its getting easier. Here we are creating one toggle button which is intended to control the Raspberry Pi GPIO.Then we are connecting an relay one end to Pi GPIO and another one end to home appliance(here I am using BULB). Circuit Connection

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Code: //package frame.mine; import javax.swing.*; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; public class Pigui implements ItemListener{ // Definition of global values and items that are part of the GUI. JToggleButton toggleButton; JPanel totalGUI; final GpioController gpio = GpioFactory.getInstance(); final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, PinState.LOW); public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. totalGUI = new JPanel(); totalGUI.setLayout(null);

toggleButton = new JToggleButton("OFF"); ImageIcon icon = new ImageIcon("/home/pi/off1.png"); toggleButton.setIcon(icon); toggleButton.setLocation(10,10); toggleButton.setSize(210,207); toggleButton.addItemListener(this); totalGUI.add(toggleButton); totalGUI.setOpaque(true); return totalGUI; } // This is the new itemStateChanged Method. // It catches any events with an ItemListener attached. // Using an if statement, we can determine if the button is now selected or deselected

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

// after the action and perform changes to the GUI accordingly. public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { pin.high(); toggleButton.setText("ON"); ImageIcon img = new ImageIcon("/home/pi/on1.png"); toggleButton.setIcon(img); } else { pin.low(); ImageIcon icon = new ImageIcon("/home/pi/off1.png"); toggleButton.setIcon(icon); toggleButton.setText("OFF"); } } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Raspberry Pi"); //Create and set up the content pane. Pigui demo = new Pigui(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(235,240); frame.setVisible(true);

public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Note:Location of mycode is /opt/pi4j/examples/ . I am in the follwing directory and running the code. /opt/pi4j/examples For button you can put your image wherever you want and mention the path and file name in the code. Compilation javac -classpath .:classes:/opt/pi4j/lib/'*' filename.java

Execution: sudo java -classpath .:classes:/opt/pi4j/lib/'*' Filename

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

After executing the command you will get the following UI.

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

Now your BULB is off state ,after clicking the button your will BULB go the ON state.

#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com


nd

You might also like