RPi and Java Embedded GPIO: Writing Java code to blink LED

So, you've followed the previous steps to install Java Embedded on your Raspberry Pi , you went to Fry's and picked up some jumper wires, LEDs, and resistors , you hooked up the wires, LED, and resistor to the correct pins , and now you want to start programming in Java on your RPi? Yes? ✔✔✔✔✔✔✔!

OK, then... Here we go.

You can use the following source code to blink your first LED on your RPi using Java. In the code you can see that I'm not using any complicated gpio libraries like wiringpi or pi4j, and I'm not doing any low-level pin manipulation like you can in C. And, I'm not using python (hell no!). This is Java programming, so we keep it simple (and more readable) than those other programming languages.

See: Write Java code to do this

In the Java code, I'm opening up the RPi Debian Wheezy well-defined file handles to control the GPIO ports. First I'm resetting everything using the unexport/export file handles. (On the RPi, if you open the well-defined file handles and write certain ASCII text to them, you can drive your GPIO to perform certain operations. See this GPIO reference). Next, I write a "1" then "0" to the value file handle of the GPIO0 port (see the previous pinout diagram). That makes the LED blink. Then, I loop to infinity. Easy, huh?

/*
 * Java Embedded Raspberry Pi GPIO app
 */

import java.io.FileWriter;
import java.io.File;

/**
 *
 * @author hinkmond
 */
public class JerpiGPIO {
    
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    static final String GPIO_CH00="0";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        try {
            
            /*** Init GPIO port for output ***/
            
            // Open file handles to GPIO port unexport and export controls
            FileWriter unexportFile = new FileWriter("/sys/class/gpio/unexport");
            FileWriter exportFile = new FileWriter("/sys/class/gpio/export");

            // Reset the port
            File exportFileCheck = new File("/sys/class/gpio/gpio"+gpioChannel);
            if (exportFileCheck.exists()) {
                unexportFile.write(gpioChannel);
                unexportFile.flush();
            }
            
            
            // Set the port for use
            exportFile.write(GPIO_CH00);   
            exportFile.flush();

            // Open file handle to port input/output control
            FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+"/direction");
            
            // Set port for output
            directionFile.write(GPIO_OUT);
            directionFile.flush();
            
            /*** Send commands to GPIO port ***/
            
            // Open file handle to issue commands to GPIO port
            FileWriter commandFile = new FileWriter("/sys/class/gpio/gpio"+GPIO_CH00+
                    "/value");
            
            // Loop forever
            while (true) {
                
                // Set GPIO port ON
                commandFile.write(GPIO_ON);
                commandFile.flush();
        
                // Wait for a while
                java.lang.Thread.sleep(200);
        
                // Set GPIO port OFF
                commandFile.write(GPIO_OFF);
                commandFile.flush();
                
                // Wait for a while
                java.lang.Thread.sleep(200);
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

Compile the above Java source file into a class file JerpiGPIO.class on a host system (such as your desktop/laptop PC where you have the full JDK). Then, copy that compiled class file to your RPi device (Ex. scp JerpiGPIO.class pi@'XXX'.'XXX'.'XXX'.'XXX':/home/pi)

Now, to run, open a Linux shell on the RPi device and make sure to first sudo -i (become root interactively) to gain the correct privilege to use the GPIO ports:

   sudo -i

Then, in that same shell on the RPi run with Java this way:
   java -cp /home/pi JerpiGPIO

That's it. Easy as Pi!

Comments:

I'm guessing that the Pi doesn't have enough guts to compile Java?

Posted by guest on November 15, 2012 at 03:51 AM PST #

Actually, the RPi Model B does have enough to use the javac compiler directly on the RPi device itself. It's not out yet, but watch for the JDK 7u12 Linux/ARM release from Oracle, where you might find the full JDK for RPi (including javac) and hard float support... Stay tuned...

Posted by Hinkmond Wong on November 15, 2012 at 12:50 PM PST #

Hi, Work Nice on CH25 for me. Thx

Posted by guest on November 18, 2012 at 05:35 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today