2013
Running Java on Raspberry Pi
Author: Praveen Kumar R
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
nd
Introduction: Lot of people asking me whether it is possible to run java on raspberry [Link] it is [Link] 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 [Link] 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)[Link] can download it from the [Link]/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 [Link] the Java Platform(JDK) as per the image shown below.
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
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,[Link]
nd
Step 3: Then download the ([Link]) as per the image given below.
Step 4: After downloading the file extract it using tar [Link] the follwing command in command [Link] will take few seconds to complete. tar xvzf ~/[Link]
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
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,[Link]
nd
Thats all the Oracle JDK is installed and we can run the java [Link] 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/[Link] 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,[Link]
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 [Link] creating UI for pi using java it easy for you to access the GPIO [Link] 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 [Link] build using the following command prompt. wget [Link]
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
nd
Perform the installation sudo dpkg -i [Link]
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,[Link]
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/'*' [Link] 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 [Link] 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 [Link] 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,[Link]
nd
Code: //package [Link]; import [Link].*; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; public class Pigui implements ItemListener{ // Definition of global values and items that are part of the GUI. JToggleButton toggleButton; JPanel totalGUI; final GpioController gpio = [Link](); final GpioPinDigitalOutput pin = [Link](RaspiPin.GPIO_01, [Link]); public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. totalGUI = new JPanel(); [Link](null);
toggleButton = new JToggleButton("OFF"); ImageIcon icon = new ImageIcon("/home/pi/[Link]"); [Link](icon); [Link](10,10); [Link](210,207); [Link](this); [Link](toggleButton); [Link](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,[Link]
nd
// after the action and perform changes to the GUI accordingly. public void itemStateChanged(ItemEvent e) { if([Link]() == [Link]) { [Link](); [Link]("ON"); ImageIcon img = new ImageIcon("/home/pi/[Link]"); [Link](img); } else { [Link](); ImageIcon icon = new ImageIcon("/home/pi/[Link]"); [Link](icon); [Link]("OFF"); } } private static void createAndShowGUI() { [Link](true); JFrame frame = new JFrame("Raspberry Pi"); //Create and set up the content pane. Pigui demo = new Pigui(); [Link]([Link]()); [Link](JFrame.EXIT_ON_CLOSE); [Link](235,240); [Link](true);
public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. [Link](new Runnable() { public void run() { createAndShowGUI(); } }); } }
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
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/'*' [Link]
Execution: sudo java -classpath .:classes:/opt/pi4j/lib/'*' Filename
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
nd
After executing the command you will get the following UI.
#9/3,Shree Lakshmi complex,2 Floor,Opp,To Vivekananda Park,Girinagar,[Link]
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,[Link]
nd