Raspberry PI and JAVA: a closer look

    Recently on Habrahabr published an article about java on raspberry pi, seeing the name of which was a lot of expectations, but under the cut appeared trivial Hello World!

    The fact is that my raspberry just came to me and wanted to get answers to the following questions:

    1. Is the speed of java on a raspberry and a desktop computer comparable?
    2. How convenient is it to work with java on raspberry?
    3. Are there adequate libraries for working with GPIO?

    I’ll try to give answers to these questions in this article.

    Who cares: welcome to kat (graphics and photos of naked raspberries will not be there)

    Configuring access to Raspberry PI via ssh without password


    I really love my native laptop and prefer to work on other Linux machines via ssh.
    Therefore, first of all, for the sake of convenience, we configure access to the raspberry by key.
    To do this, on the computer from which the connection will be made, we generate a key pair using the ssh-keygen command.
    Then copy the public key to the raspberry

    $ scp /home/user1/.ssh/id_rsa.pub pi@raspberry_server:~/
    $  ssh  pi@raspberry_server
    $ mkdir .ssh
    $ cat ~/id_rsa.pub >> ~/. ssh /authorized_keys
    

    Press Ctrl-D to exit the session. Trying to connect again - profit. Connecting without asking for a password.

    We look under the hood


    First of all, I'm interested in what kind of equipment I got. You can, of course, look at the documentation, but it will not always tell the whole truth.
    Therefore, we connect and enter the command
    $ cat /proc/cpuinfo
    


    Interested in the following line:
    Features: swp half thumb fastmult vfp edsp java tls

    Well already interesting. You can hope that raspberry will make me happy.

    Install JAVA SE Embedded


    The previous article described how to install openJDK. Who cares - look.

    But I was interested in installing java from Oracle (anyway, I like to compile java code on my laptop in my favorite IDE), which I did:
    So, go to the Oracle website, download the java se embedded package (ARMv6 / 7 Linux - Headless - Client Compiler EABI, VFP, HardFP ABI, Little Endian) and fill it in the / home / pi folder.

    We go into the raspberry console and
    1. Unpack the archive in the / opt folder
    $ sudo tar -xvf ejre-7u45-fcs-b15-linux-arm-vfp-hflt-client_headless-26_sep_2013.tar.gz -C /opt
    

    2. Next, add the path to the java file in the PATH variable and set the JAVA_HOME variable
    $ sudo chmod a+w /etc/profile
    $ echo 'export PATH=/opt/ejre1.7.0_45/bin:$PATH' >> /etc/profile 
    $ echo 'export JAVA_HOME=/opt/ejre1.7.0_45' >> /etc/profile 
    $ sudo chmod a-w /etc/profile
    

    Reboot via ssh and the command
    $ java -version
    

    make sure that the virtual machine is installed.

    We test the speed of work


    Now it's time to find out how slow / fast java is on the raspberry. The test does not pretend to be any comprehensive objectivity, but merely aims to show the approximate order of the difference in the speed of a virtual machine on a raspberry and a desktop computer.
    For the test, I selected my netbook with an AMD E-300 APU processor with a clock frequency of 1.3 Hz (i.e., almost twice as much as that of raspberry).
    For the test we use the program for finding prime numbers using the sieve of Eratosthenes.

    Anyone interested can see the source code:
    public class RaspTest {
        public static void main(String[] args) {
            int maxPrimesCount = 40000;
            int currentPrimesCount = 1;
            long prevTime, execTime;
            prevTime = System.currentTimeMillis();
            long[] primes = new long[maxPrimesCount];
            long currentNumber = 3;
            boolean isPrime = false;
            primes[0]=2;
            while (currentPrimesCount < maxPrimesCount) {
                isPrime = true;
                for (int i = 0; i < currentPrimesCount; i++) {
                    if (currentNumber % primes[i] == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime) {
                    primes[currentPrimesCount] = currentNumber;
                    currentPrimesCount++;
                }
                currentNumber++;
            }
            execTime = System.currentTimeMillis() - prevTime;
            System.out.println(execTime);
            System.out.print(currentNumber-1);
        }
    }
    



    Total:
    Netbook showed the result of 89 seconds, and raspberry - 444 seconds.
    Total: on a raspberry almost five times slower. Well, quite expected, given the difference in clock speed and architecture.
    Surprise will befall us if we change the type of numbers from long to int.
    In this case, the netbook showed a result of 38 seconds, and raspberry - 65 seconds.
    I was pleasantly surprised.

    Conclusion: the speed of a virtual machine on raspberry pi is comparable to that on desktop computers.

    Work with GPIO


    In one of the lectures at the Joker conference, the speakers programmed the GPIO on Java Embedded ME (micro edition).
    Standart Edition, unfortunately, does not have the corresponding classes, so I turned to Google and found the Pi4J project (www.pi4j.com). The stable version is now 0.0.5, but the project is developing and version 1 is being developed at the moment.
    Nevertheless, I recommend using the stable version, because on version 1, not everything worked for me.
    It should also be noted that the port numbers are slightly different from the standard ones, so I recommend that you familiarize yourself with the documentation on the Pi4J website.

    I catch the LED on the first port, on the second button, I write the following code:

    public class Test1 {
        public static void main(String[] args) throws InterruptedException {
            GpioController gpioController = GpioFactory.getInstance();
            GpioPinDigitalOutput gpioPinDigitalOutput = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
            GpioPinDigitalInput gpioPinDigitalInput = gpioController.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN);
            gpioPinDigitalInput.addListener(new GpioPinListenerDigital() {
                @Override
                public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent gpioPinDigitalStateChangeEvent) {
                    System.out.println("GPIO Pin changed" + gpioPinDigitalStateChangeEvent.getPin() + gpioPinDigitalStateChangeEvent.getState());
                    System.out.println("Sleeping 5s");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Unsleep");
                }
            });
            while (true) {
                gpioPinDigitalOutput.toggle();
                Thread.sleep(500);
            }
        }
    }
    


    I collect the package, copy the jar file and libraries to raspberry pi, run it and ... Does not work.
    It turns out that you need administrator rights to manage I / O ports.
    But in order for the team to work
    $ sudo java
    

    in the / bin directory there should be a symbolic link to the executable file of the java machine. Create it:

    ln -s /opt/ejre1.7.0_45/bin/java /bin/java
    


    We start again - it works. The lamp blinks, when you press the button and the process stream falls asleep, the lamp continues to blink, i.e. event processing from the button starts asynchronously.

    Conclusions:
    1. Raspberry pi is not a toy, but a computer with performance and capabilities suitable for solving many problems.
    2. The performance of the java virtual machine is comparable to the performance in desktop systems, although somewhat lower.
    3. Management of external equipment using java and raspberry pi is a real and quite easily solved task (which I am going to do in the future).

    Thanks for attention.

    Also popular now: