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:

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 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

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:

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/

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.

Thursday, May 26, 2011

Pulse Code Modulation: Quantization

The discrete signal i.e. sampled values produced as a result of sampling (discussed in the previous post) has to be quantized, hence the process called Quantization. In simple words, this is the process in which a value from a given set of values is assigned to each sample of the discrete signal. The number of values in the set actually is the number of quantization levels to which samples of discrete signals are assigned to. Digital communication is based on bits and bytes, the number of bits used identify the number of quantization levels,  hence, in this case when the samples are binary encoded that means they are essentially being quantized in one of the fixed number of quantization levels.
In the figure below, the process has been explained where an input discrete signal s(t) has been quantized into a signal sq(t). The input signal s(t) moves between low peak amplitude AL to high peak amplitude AH . This range from AL to AH is divided into M intervals (an interval represent a quantization level, also referred as quantization interval) each of size L such that L =  (AH - AL)/M. In the figure, eight intervals have been shown i.e. M=8. Let As be the peak-to-peak amplitude of s(t)  i.e. total range between high and low peaks i.e. As = AH - AL hence L = As /M. The value of each quantization interval is taken as the center of the interval, in figure, shown as a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 . The range of values for each interval has been shown as l0, l1, l2 , l3 , l4 , l5 , l6 , l7 . Moreover, the midpoints between consecutive quantization intervals are shown by  A01 , A12 , A23 , A34 , A45 , A56 , A67 . Each of these midpoints is at a distance of L/2 from its corresponding intervals values e.g. A67 is the midpoint between two quantization intervals whose values are a6 and a7 and is located at a distance of L/2 from a6 and a7. The distance between two consecutive midpoints and also between two interval values is L. It is, in fact, these mid points which define when the value of the quantized signal sq(t) changes. It should also be noted that the peak values i.e. AH and AL are at a distance of L/2 from their corresponding quantization levels. This results in affecting the peak-to-peak amplitude of sq(t) shown as  Aq and has a value of (M-1)L.
As shown in the figure, when the signal being quantized i.e. s(t) is in the range of l7 , the quantized signal sq(t) maintains a constant value of a7 . As soon as s(t) gets in the range of l6 , sq(t) makes a jump to the value of a6 and maintains this value as long as s(t) assumes a value in the range of l6 . The sudden jump is made when s(t) crosses the midpoint  A67 . Similar changes occur to sq(t) through the rest of the process of quantization. Therefore, at any time, the quantized signal sq(t) holds a value from a0 , a1 , a2 , a3 , a4 , a5 , a6  and  a7 . That means, at any moment, sq(t) exits in one of the quantization intervals that is nearest to  s(t).
As the above description and the figure indicate that sq(t) is not same as s(t) rather it’s an approximation with a difference between the two. This difference is very important and is known as Quantization Error. Quantization Error’s magnitude is always less than or equal to L/2 (We’ll discuss Quantization Error later). The approximation can be improved by making the interval size smaller, hence increasing the number of intervals (quantization levels). In digital communication, high number of intervals requires high number of bits which itself, in turn, require high transmission bandwidth. On the other hand, if interval size is increased using lesser number of quantization levels, approximation is poor. Therefore, there has to be a tradeoff between approximation and bandwidth.

Thursday, March 17, 2011

Pulse Code Modulation: Filtering and Sampling

In Digital Telephony, analog signals have to be converted into digital signals before transmission because of several advantages they have over Analog signals, one of which is their strong immunity against noise. Pulse Code Modulation (PCM) is one of the techniques to achieve the objective. Generally, PCM is a four step process consisting of Filtering and Sampling, Quantization and Encoding. However, Quantization may be performed either of two ways: Linear Quantization or Non-Linear Quantization. For Non-Linear Quantization, sometimes an additional step of Companding is performed after Sampling and before Quantization to prepare the samples for non-linear Quantization.
Here, in this post, we’ll discuss Filtering and Sampling. Later, in the next posts, we’ll discuss Linear Quantization, Quantization Error, Non-Linear Quantization, Companding and Encoding. Our discussion is based on digital telephony.

Analog Low Pass Filtering:
Before an analog signal is converted into digital one, it is passed through an analog Low-Pass Filter (see here). Following figure shows the frequency responses of a Low Pass Filter.

This filtering comes handy later to avoid aliasing when the analog signal is sampled and then reconstructed from the digital signal (see here), hence also called Anti-Aliasing Filter. In simple words, this is performed to filter out the frequencies which are higher than a “certain frequency”. We are interested in the frequency band lower than the “certain frequency” and not the higher.

 “Certain Frequency” is just a made up term for explanation and is not a technical term.

 For Telephony Systems whose main objective is to transfer human voice, that “certain frequency” can be found from high school physics. We have read human voice consists of frequency range of 300Hz to 3000Hz. Due to some constraints in designing an ideal filter, we make this certain frequency 4000Hz instead of 3000Hz. So after filtering, for the next step of Sampling, we know that bandwidth of the signal is 4000Hz i.e. the greatest frequency analog signal consists of is 4000Hz. 

Sampling is the process of creation of a Discrete Signal by taking samples of an Analog Signal. These samples are taken at regular intervals; intervals may be time base (for time domain) or space based (for spatial domain) or any other independent variables. In our case, intervals are generally time based, these regular intervals define the sampling rate--the rate at which samples are taken, also called sampling frequency Fs.
Sampling is governed by Nyquist Sampling Theorem, according to which when a signal is sampled, the sampling rate (or sampling frequency) must be at least twice the highest frequency the signal contains, to avoid aliasing. In other words, sampling frequency must be at least twice the bandwidth of the signal. This means at least two samples are required per cycle of each frequency in the analog signal.
                                                                Fs = 2 x 4000 = 8000 Samples/sec

Filtering and Sampling work in collaboration to overcome a possible artifact of aliasing which is a result of under sampling. Aliasing is an important concept to be understood and an important artifact to be overcome for correct transfer of voice data in telephony systems.
On the other hand, sampling rate, higher than minimum sampling rate of 8000 samples/sec, results in too much voice data to be transferred over the communication channel demanding a high bandwidth. Between the two extremes of under-sampling and over-sampling, above sampling frequency of 8K samples/sec is the threshold which is good enough for the reconstructed analog signal and is good enough for the usage of bandwidth.
A technique often used for sampling is Pulse Amplitude Modulation (PAM) in which a pulse is modulated with the filtered analog signal. This has been shown is the following figure created on Simulink Matlab, pink colored graph is the pulse, yellow colored graph is a sine wave to be sampled and aqua graph is the output of the PAM sampling.

Sampling creates discrete signals not the digitized signals; Quantization is the step to digitize the samples into binary encoded form.