Wednesday, November 16, 2016

Internet of Things (IoT): Raspberry Pi + DS18B20 + Eclipse KURA + Mosquitto + Eclipse Paho MQTT—PART 4

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.


Saturday, October 15, 2016

Internet of Things (IoT): Raspberry Pi + DS18B20 + Eclipse KURA + Mosquitto + Eclipse Paho MQTT—PART 3

Next to complete our environment, we set up an MQTT message broker: Eclipse Mosquitto, on a our development machine or even on a separate machine. There are some publicly available message brokers as well, for example, there is one instance of Mosquitto by Eclipse. IBM Bluemix also includes an instance of MQTT broker.

So Eclipse Kura running on Raspberry Pi, will read the temperature from DS18B20 and sends the data as MQTT message to the broker , MQTT clients, which have subscribed for the topic of the message, will receive the data. Following link describes MQTT and provides a good basic understanding.


MQTT primarily belongs Machine to Machine communication and was specifically developed for the purpose, its reliable and resource efficient. I found following resource about the preference of MQTT for Internet of Things (IoT) projects:


Since I am working on Ubuntu I just had to do sudo apt-get install mosquitto to install Mosquitto, that’s it!! There are installers for other OSes here: https://mosquitto.org/download/

In my case on Ubuntu, I created a password file under /etc/mosquitto/ using following command:

sudo mosquito_passwd -c mosquito.pwd user_name

This user is used to configure Eclipse Kura

I also set ‘Protocol’ in /etc/mosquitto/mosquitto.conf as following:

protocol mqtt

Try to run Mosquitto using the following comment:

mosquito -c /etc/mosquitto/mosquitto.conf

If it runs fine, you should a similar output as following:

1476551631: mosquitto version 1.4.8 (build date Fri, 19 Feb 2016 12:03:16 +0100) starting
1476551631: Config loaded from mosquitto.conf.
1476551631: Opening ipv4 listen socket on port 9001.
1476551631: Opening ipv6 listen socket on port 9001.
1476551631: Opening ipv4 listen socket on port 1883.
1476551631: Opening ipv6 listen socket on port 1883.

Next we need to configure Eclipse Kura with Mosquitto, instructions for doing so can be found on the following link:

Wednesday, October 12, 2016

Internet of Things (IoT): Raspberry Pi + DS18B20 + Eclipse KURA + Mosquitto + Eclipse Paho MQTT-P2

Next we do Eclipse Kura. For an introduction of Eclipse, check following link:


Eclipse Kura is an OSGi based IoT gateway framework, in our case since Raspberry Pi will be acting as Gateway, therefore, we will install Eclipse Kura on Raspberry Pi. Different versions of Kura have been classified based on Raspberry Pi model. In my case, I chose Raspbian (Model B+).

Notice that another classification is Non-Net and Net(Not having Non-Net as part of those name). Net version provides the facilities to configure the network setting of the gateway device where Kura is running. I first tried Net version and I lost all my network configuration on Raspberry Pi which I didn’t want, so I tried Non-Net version that worked for me and is still running successfully.
In my case I chose Raspbian Model B+ No Net Stable.
Use following link to choose and download a suitable version.

The steps for installing Eclipse Kura on Raspberry Pi are listed on the following link, follow all the steps as it is, however, use the debian package downloaded from the above link:


If everything goes fine , you should be able to access Kura through browser at http://127.0.0.1:8080 if Raspberry Pi has display. Externally it may be accessed in browser using RasPi’s ip address (on default porti.e. no port required), for example, in my case it was 192.168.1.103.

To develop applications to run on Kura Framework, we need to set up the development environment. Eclipse Kura provides an workspace which contain all the necessary components required. Please see the following link for details:


There are some tutorials with example applications e.g. HelloWorld Example, Configurable Application. One called Deploying Bundles describes how to install the application (in OSGi terminology called Bundle) on target device in our case its Raspberry Pi.

There is one more tutorial on the same page called Publishing Application, it describes the a demo Application: Heater, its actually this application’s code we will use for our working setup. Pay special attention to this: http://eclipse.github.io/kura/doc/heater_demo.html

Sunday, October 9, 2016

Internet of Things (IoT): Raspberry Pi + DS18B20 + Eclipse KURA + Mosquitto + Eclipse Paho MQTT-P1

These posts describe a working set-up of following component:
  • Raspberry Pi 3
  • DS18B20 (1-Wire Digital Temperature Sensor)
  • Eclipse Kura (Eclipse Project: Framework for IoT Gateway)
  • Eclipse Mosquitto (Message Broker for MQTT)
  • Eclipse Paho MQTT (Eclipse Project: MQTT Client)

The post primarily discusses how to make above components work, however, we will try to provide the relevant resources throughout the posts.


We start off with connecting DS18B20 and Raspberry Pi, there are a number of online resources which helps to understand 1-wire sensor and how to connect them to Raspberry Pi, following are the two which I used:



Above links explains it in a detailed way, we instead look at another issue: in some cases, we come across an error when following command is executed: (discussed on the above links)

cat w1_slave

Error: w1_slave: No such file or directory

Generally when sensor is connected successfully and is working properly, upon executing following commands:

cd /sys/bus/w1/devices/
ls

The list should show your sensor(s) as a directory with its name starting with ‘28-’, the name itself is a combination of characters and numbers. I experienced that in situations where sensor is not correctly working in fact not connected properly, the sensor directory name is although, a combination of characters and numbers but doesn’t start with ‘28-’. In these situation command: cat w1_slave returns the above error.

The source of the error, as mentioned earlier, could be incorrect connections, connecting wires not working properly, incorrect GPIO connections (Caution: As always be careful with GPIO connections).

I have attached some images from my working set-up below. I used 10K ohm resistor in my setup and Dallas Digital 1-Wire Temperature sensor.