Recently, an article about
java on a raspberry pi appeared on a habrahabr
, having seen the name of which had many expectations, and under the cut it turned out to be a banal Hello World!
The fact is that my raspberry just drove to me and wanted to get answers to the following questions:
1. Is the java speed on the raspberry and desktop computer comparable?
2. How convenient is it to work with java on raspberry?
3. Are there adequate libraries for working with GPIO?
')
I will try to answer these questions in this article.
Who cares: welcome under the cut (graphics and pictures of nude raspberry will not be there)
Configure 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, in the first place, 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 pair of keys using the ssh-keygen command.
Then copy the public key on 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. We are trying to connect again - profit. Connection occurs without a password request.
Looking under the hood
First of all, I wonder what kind of equipment I got. You can, of course, look in the documentation, but she 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. Hopefully, Malinka will make me happy.
Installing JAVA SE Embedded
The previous article described how to install openJDK. Who is interested - look.
But it was interesting for me to install java from oracle (anyway, I like to compile java-code on my favorite laptop in my favorite IDE), which I did:
So, go to the oracle website, download the java se embedded (ARMv6 / 7 Linux - Headless - Client Compiler EABI, VFP, HardFP ABI, Little Endian) package and fill it in the / home / pi folder.
Go to the console raspberry and
1. Unpack the archive into 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 to 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 aw /etc/profile
Relocating to ssh and command
$ java -version
make sure 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 any comprehensive objectivity, but is only intended 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, my netbook with an AMD E-300 APU processor with a clock frequency of 1.3 Hz was chosen (that is, almost twice as large as the Malinka).
For the test, we use the program to search for primes using the sieve of Eratosthenes.
Who is 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:
The netbook showed 89 seconds, and raspberry 444 seconds.
Total: on the malinka is almost five times slower. Well, it is quite expected given the difference in clock frequency and architecture.
The 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 the virtual machine on a raspberry pi is comparable to that on desktop computers.
Work with GPIO
In one of the reports at the Joker conference, speakers programmed a GPIO in the Java Embedded ME (micro edition).
Standart Edition, unfortunately, does not have the appropriate 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 being developed and version 1 is being developed at the moment.
Nevertheless, I recommend using a stable version, because in 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 reading the documentation on the Pi4J website.
I cling to the first port LED, 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 compile a package, copy the jar file and libraries to a raspberry pi, launch it and ... It does not work.
It turns out that admin rights are required 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 java-executable file. Create it:
ln -s /opt/ejre1.7.0_45/bin/java /bin/java
Run again - it works. The light flashes, when the button is pressed and the processing flow goes to sleep, the light continues to flash, i.e. event handling from the button starts asynchronously.
Findings:
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 of desktop systems, although somewhat lower.
3. Controlling external equipment with java and raspberry pi is a real and quite easily solved task (which I am going to do in the future).
Thanks for attention.