Monday, May 9, 2016

Intel Edison and Java

I have worked for a long time with Java, and probably Java is the language that I better manage.

But, Java is not frequently used on IoT devices and boards. The main reason is that it is considered a resource hog. It requires a lot of memory.

Anyway, Intel Edison is not so resource-constrained as some other boards (Arduino), with its 1 GB Ram, and therefore it is perfectly justified the decision to try to test Java and UPM libraries to see how they works.

The development environment that I’m going to use is the one provided by Intel: The Intel Studio IoT Edition (ISS) based on Eclipse.

But, I don’t want to use the Java JDK normally distributed with Yocto (OpenJDK) and, therefore, the first step that I’m going to do is to install Oracle JDK 8 on Edison.

mkdir /usr/java

Then downloads Oracle JDK 8 on Edison; The version I have downloaded from the Oracle site is:

jdk-8u91-linux-i586.tar.gz

(be sure to download the X86 version and NOT the X86_64, Edison is 32-bit).

Then, extract the tar file with the command

tar -xvzf jdk-8u91-linux-x64.tar.gz

after, move the exploded directory under /usr/java

mv jdk1.8.0_91 /usr/java

At this point, you only need to setup correctly two environment variables

export JAVA_HOME=/usr/java/jdk1.8.0_91

 

export PATH=$JAVA_HOME/bin:$PATH


Then, you can check that Oracle Java JDK 8 is correctly installed with:

 

java -version

 

java version "1.8.0_91"

 

Java(TM) SE Runtime Environment (build 1.8.0_91-b14)

 

Java HotSpot(TM) Client VM (build 25.91-b14, mixed mode)

 

Ok, the first step is done, the JDK 8 is installed and working.

 

Now, you need to install the Java libraries for MRAA and UPM.

 

The latest version of the Java (JNI) libraries from IOTDK 3.0 can be downloaded from

 

http://iotdk.intel.com/repos/3.0/java/iotdk-java-latest.zip

 

I have downloaded this zip file, extracted the contents in 

 

/home/root/javaprograms/lib


This directory have to be put inside the classpath, when you run your program.


To test I have created a simple Java program, made by a single Java Class. The program uses two sensors from the Grove Starter Kit Plus: Temperature and Light.


import upm_grove.*;


public class Test1

{

        private static int PIN_TEMP = 0;

        private static int PIN_LIGHT = 1;

        private static int SLEEP_TIME = 3000;

       

        public static void main(String[] args)

        {

                System.out.println("Starting program...");

               

                GroveTemp tempSensor = new GroveTemp(PIN_TEMP);

                GroveLight lightSensor = new GroveLight(PIN_LIGHT);

               

                int i = 0;

                while (i < 100)

                {

                        float temp = tempSensor.value();

                        float light = lightSensor.raw_value();

                        System.out.println("Temperature is : " + temp + ", Light is: " + light);

                       

                        try

                        {

                                Thread.sleep(SLEEP_TIME);

                        } catch (InterruptedException e)

                        {

                                e.printStackTrace();

                        }

                }

        }

}


The java class is called Test1 and it is packed inside test1.jar file.


To run the program:


java -cp .:/home/root/javaprograms/lib/*:test1.jar Test1


where Test1 is the name of the main class

 

It works !!. Simple. 

 

In the next blog post, once again, I will integrate with MQTT library Paho and I will test sending msgs with readings from sensors to a Mosquitto MQTT broker.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Library for java on Edison: https://github.com/nsoft/edisonj

    ReplyDelete
    Replies
    1. Ok, interesting. But mraa and upm have much more features

      Delete