Eclipse Kura

Overview and Setup

The Raspberry Pi can be used as an IoT Gateway to communicate with the ClearBlade Platform. We make use of Eclipse Kura to achieve this.

Eclipse Kura is an Eclipse IoT project that provides a platform for building IoT gateways. It is a smart application container that enables remote management of such gateways and provides a wide range of APIs for allowing you to write and deploy your own IoT application.

Kura runs on top of the Java Virtual Machine (JVM) and leverages OSGi, a dynamic component system for Java, to simplify the process of writing reusable software building blocks. Kura APIs offer easy access to the underlying hardware including serial ports, GPS, watchdog, USB, GPIOs, I2C, etc. It also offer OSGI bundle to simplify the management of network configurations, the communication with IoT servers, and the remote management of the gateway.

Setting up Kura on Eclipse

First of all you will need to download Eclipse and set up your workspace for Kura. Please follow the instructions given at http://eclipse.github.io/kura/doc/kura-setup.html

Setup Kura on the Raspberry Pi

  • Download the Kura image for the Raspberry Pi from https://www.eclipse.org/kura/downloads.php
  • Select the Raspbian (Model 2, No Net, with Web UI) - Stable in the Kura Extended Downloads section
  • Copy the downloaded file to your Raspberry Pi
  • Check whether Java is installed on your Raspberry Pi by executing the following command:
    java -version
  • If Java is not installed, please install it with the following command:
    sudo apt-get install java
  • Now cd to the directory where you copied the Kura image and install Kura with the following commands:
    sudo apt-get update
    sudo dpkg -i your_kura_image_filename.deb
    sudo apt-get install -f
  • After successful installation of Kura, reboot your Raspberry Pi:
    sudo reboot
  • After reboot, you can check whether Kura is running by going to http://your_raspberrypi_IP in your browser and the Kura console should show up. The default username is admin and the default password is admin

Developing a Kura Plugin for the Raspberry Pi

Now we will create a Kura plugin in Eclipse, bundle it as a JAR and deploy it to the Raspberry Pi. This plugin will use the ClearBlade Java SDK to communicate with the platform. The plugin will initialize with the ClearBlade Platform and send a message over MQTT.

This plugin contains the source code of the ClearBlade Java API, Eclipse Paho MQTT and and Activate class which runs when the plugin is deployed on the Raspberry Pi. The ClearBlade Java API depends on the Google Gson library so we will create a separate dependency plugin for it and deploy it separately to the Raspberry Pi.

Deploy the Google GSON Bundle (Dependency for the Plugin and ClearBlade Java SDK) to the Raspberry Pi

  • First of all, clone the ClearBlade Java SDK repo git clone https://github.com/ClearBlade/ClearBladeJavaSDK.git and import this repo into Eclipse
  • Open Eclipse and expand the target-definition folder and double click the kura-equinox_X.X.X.target file

  • Click the Set as Target Platform link
  • Now, click File -> New -> Other and select Plug-ins from Existing JAR Archives

  • Click Next
  • Click Add and expand the ClearBlade Java SDK that you imported and expand the libs folder within that SDK. Select the gson-2.3.jar and hit Ok

  • Give your project a name and uncheck the Unzip the JAR archives into the project option and click Finish

  • Next we will create a Deployment Bundle of the google gson library, right-click your newly created project (com.google.gson from the screenshot above) and Export -> Plug-in Development -> Deployable plug-ins and fragments (Note: If you get an error regarding Java SE 1.8 during Export, go to your MANIFEST.MF and delete the line Bundle-RequiredExecutionEnvironment: JavaSE-1.8 , save and try again)

  • Click Next and choose the directory to save and click Finish
  • Next step is to transfer the Google GSON Deployment Bundle to the Raspberry Pi. For deploying bundles to the Raspberry Pi, you will need the mToolkit in Eclipse. If you have followed the Eclipse Kura setup instructions, you will have installed it already. If you have not installed it, please follow the instructions at http://eclipse.github.io/kura/doc/kura-setup.html
  • Select the Eclipse menu Window | Show View | Other
  • Select mToolkit -> Frameworks entry to open the mToolkit Frameworks view.
  • Enter a name for the framework definition and the IP address of the target device
  • Close the dialog by clicking the OK button
  • Right-click the framework icon name and select Connect Framework. The list of installed bundles and deployment packages should be retrieved shortly

  • Right-click the Framework name and select Install Bundle and select the JAR Deployment bundle that you just created for the google gson library
  • If everything goes well, if you expand the Bundles folder in your framework, you should see your newly deployed bundle running. If its not running you can right-click the bundle and choose to start or stop it. You can see any errors related to the bundle in the Console

Deploy the Plug-in to the Raspberry Pi

Now we will create a new Plug-in project and copy the ClearBlade Java SDK and Eclipse Paho source files to it and create an activator class containing the code to make the hardware communicate with the ClearBlade Java API.

  • Clone the Eclipse Paho Java repo git clone https://github.com/eclipse/paho.mqtt.java.git
  • In Eclipse, click File -> New -> Other and select Plug-in Project

  • Click Next
  • Give your project a name and select the Standard OSGI framework as shown in the screen capture below
  • Click Next and make sure that the Generate an activator is unchecked and click Finish
  • Now copy and paste the ClearBlade Java SDK and Paho MQTT packages in your plugin’s src folder. The end result should look like this:
  • First, you will use the Manifest Editor in Eclipse to add some dependencies. Click the Dependencies tab at the bottom of the editor screen and then click the Automated Management of Dependencies heading to expand it and make sure that Import-Package radio button is selected
  • Click Add and make sure you add the dependencies as seen in the screen capture below:

  • Now you are ready to start writing a simple Java class. Right-click your plugin project. Select New | Class. The New Java Class window appears. Give your class a name and click Finish
  • Copy and paste the following code in you newly created class and replace YOUR_CLASS_NAME with the class name you just created and replace the system key, system secret, email and password with yours:
    ```java
    private static final Logger s_logger = LoggerFactory.getLogger(YOUR_CLASS_NAME.class);

    private static final String APP_ID = “ClearBlade Test”;

    protected void activate(ComponentContext componentContext) {

    s_logger.info("Bundle " + APP_ID + " has started!");
    
    s_logger.debug(APP_ID + ": This is a debug message.");
    initCB();
    

    }

    protected void deactivate(ComponentContext componentContext) {

    s_logger.info("Bundle " + APP_ID + " has stopped!");
    

    }

    private void initCB() { InitCallback initCallback = new InitCallback() {

@Override public void done(boolean results) {

System.out.println(“ClearBlade platform initialized”); Message message = new Message(“clientID-test”, 1); try { Thread.sleep(2000); } catch(InterruptedException ex) { ex.getMessage(); }

message.publish(“hellothere”, “yes kura works”); }

@Override public void error(ClearBladeException error) {

String message = error.getMessage(); System.out.println(message); } };

String systemKey = “YOUR_SYSTEM_KEY”; String systemSecret = “YOUR_SYSTEM_SECRET”; String userEmail = “YOUR_EMAIL”; String userPassword = “YOU”; String platformURL = “https://rtp.clearblade.com"; String messagingURL = “tcp://rtp.clearblade.com:1883”; HashMap options = new HashMap(); options.put(“email”, userEmail); options.put(“password”, userPassword); options.put(“platformURL”, platformURL); options.put(“messagingURL”, messagingURL);

ClearBlade.initialize(systemKey, systemSecret, options, initCallback); } - Switch back to the Manifest Editor. Under Automated Management of Dependencies, ensure the Import-Package option button is selected. Click the add dependencies link to automatically add packages to the dependencies list based on the “import” statements in your example code. Save changes to the Manifest again. The end result should look like this: <img src="/static/img/kura/raspberrypi/kura11.png" width="80%"></img> - Next, go to your MANIFEST.MF and delete the lineBundle-RequiredExecutionEnvironment: JavaSE-1.8``` and save
- Now we need to create a component class. Right-click the plugin project and select New | Other. From the wizard, select Plug-in Development | Component Definition and click Next

- In the Class field of the New Component Definition window shown below, click Browse. Search for your class and select it and hit Ok. - In the Enter or select the parent folder field of the New Component Definition window, add ”/OSGI-INF” to the existing entry (e.g., org.eclipse.kura.test.clearblade.TestCB/OSGI-INF). Then click Finish.

- After the Component class has been created, it will open in the Workspace. In the Overview tab, the Name and Class point to our Java class. Set the Activate field to activate and set the Deactivate field to deactivate. Doing so tells the component where these OSGi activation methods are located. Then save the Component class definition file.

- Next we will create a Deployment Bundle of the plugin, right-click your newly created plugin project and Export -> Plug-in Development -> Deployable plug-ins and fragments

- Click Next and choose the directory to save and click Finish
- Now we need to deploy the exported plugin to the Raspberry Pi. Right-click on the framework that you use to connect to your Pi and select Install Bundle - Choose the newly created plugin bundle and hit install. This should deploy the plugin to the Raspberry Pi and start it. Any errors or logs can be seen in the Console - If all goes well, you will see a message published on the ClearBlade Console