We need Java applications components to be deployed in Eclipse KURA, to read the readings from DS18B20 via 1-Wire, we have very helpful APIs available to accomplish this, we have famous Pi4J (http://pi4j.com/) library which is specific to Raspberry Pi Platform, the website provides detailed instructions on performing different tasks using the library. After installation of this library, you will find a sample application that shows how to use the library for the 1-Wire protocol.
Here, however, we are not going to use Pi4J library, rather we will use another library for accessing 1-Wire protocol developed by Marcus Hirt. The library includes functionalities for Humidity sensor and Temperature sensor.
In our case, we are only using Temperature Sensor. We are going to use library without any changes, so if you are using Humidity sensor while doing this example, you don’t have to do any additional thing (except that your Humidity Sensor is up and running), this example should be able to collect data from Humidity sensor as well (possibly with a little tweaking as I haven’t tested it)
To utilize the above library, we need to include the jar file (for Temperature sensor) and Shared library (for Humidity sensor) into our component so that they are bundled into the package. With regard to Java code, as it was mentioned earlier, we just made some minor changes in the sample application: Heater that comes with Eclipse KURA while setting up development workspace. I named the project as iot.tempsensor, in Eclipse Workspace looks like below:
There is no noticeable change in code in comparison to the Sample OSGi Bundle: Heater, we are only calling the above-discussed APIs. Like Heater’s bundle, this bundle is also started and stopped through OSGi Declarative Service, which is accomplished through a Component Definition file: ‘Component.xml’ in above example, the file can have any name (not necessarily ‘Component.xml’). We define some important properties in this file and also give the component a name.
Since the component is configurable, we need to provide some initial configuration and allow a mechanism to update the configuration for the bundle at a later stage. To accomplish this, we have to provide a configuration file at location: OSGI-INF>metatype. As per documentation, the name of the file should be the same the component name, hence we named file named as iot.tempsensor.TemperatureSensor.xml. KURA API Interface: ConfigurableComponent documentation says:
“...The ConfigurationService assumes that Meta Type Information XML resource for a given ConfigurableComponent with name abc" to be stored under OSGI-INF/metatype/abc.xml. This is an extra restriction over the OSGi specification: the Meta Type Information XML resource must be named as the name of the Declarative Service Component.”
The Meta Type Information XML resource file (iot.tempsensor.TemperatureSensor.xml) contains two main elements:
and . Element: contains an attribute named: id, which takes the value of the Java Class which contains component’s configurable properties. Element: contains an attribute which named: pid which takes the value of Component name defined in Component.xml. Element: contains another element named:
Under Referenced Libraries, w1.jar (For Temperature Sensor) file and libdht.so (For Humidity Sensor) under sharedlib came from Marcur Hirt Library.
See the complete code on GitHub
Once all is done, you may deploy the component on Raspberry Pi running Kura Gateway, please see the instruction on the following link under the section: Remote Target Device:
Upon running the Eclipse KURA framework, the application may be seen in Kura’s Web Interface.