Thursday, May 25, 2017

Camera Module Raspberry PI

One of the top uses of Rapi is to use it as a motion detector device by using camera module. It is not that complicated, it is really easy though.

I simplify the process into steps to make it easier to understand.

1) Update and upgrade package management system.

sudo apt-get update
sudo apt-get upgrade

2) Activate and enable camera module
 sudo raspi-config

It shows selection window, and we select Interfaces and then camera. After confirmation, it installs camera module. Now, we can capture still image or video if camera is properly installed.

3) Installation of camera

The camera is connected as shown above. The camera interface is located beside the network port. Note that the shiny part of the ribbon should always point outward from the network port. (See the pic above).

raspistill -o image.jpg
raspivid -o video.h264 -t 10000 

The time is in milliseconds. First command capture an image while second captures a 10 seconds video. 

4) Streaming and Motion Detection

Now, we go further and implement our Raspberry Pi device as a motion detector device. A very nice package is there called motion  which is highly recommended.

sudo apt-get install motion
sudo systemctl status motion.service
sudo systemctl start motion.service 

After completion, we have to modify some settings according to our requirements. We also need to enable video module for video device to work.

sudo modprobe bcm2835-v4l2

Everytime, to automaticlly enable video module, we put this command in /etc/rc.local(sudo is not needed there)

Again we need to enable motion daemon in
/etc/default/motion file.


Now, we carry out settings in /etc/motion/motion.conf. The file is really big, but we have to focus on a few things:

width 1200
height 800
framerate 20
target_dir /var/lib/motion
picture_filename %Y_%m_%d/%v-%Y%m%d%H%M%S-%q
movie_filename %Y_%m_%d/%v-%Y%m%d%H%M%S
stream_port 8081
stream_localhost off # stream connections to other systems
webcontrol_localhost off

The above mentioned parameters quite easy to understand. After change, we need to restart motion.

Now, we see the motion detected pictures stored in the directory /var/lib/motion and the define directory and file structure. And also we can access the live stream from the link:

Thats it!

It is interesting, is not it?

Raspberry Pi Settings

Hello guys, today, I am gonna share with you guys something different from programming, however, would be an interesting. Yes, Raspberry Pi is a computer, somebody would rather say "mini-computer". This is tiny but really powerful machine, on the other hand, consumes less power and resources. So, the best application of this device is to run 24/7 as embedded. I have looked around the online markets and the latest version is pi 3 (2015) costs around 38.00 €. This is powerful enough to carry out general tasks. It is suggested to buy memory card and housing also for better installment and other accessories based on requirements.

1) Operating System Installation

I have installed Raspbian Jessie Lite which is mini operating system and needs less space to install. I prefer mini version because we can later install whatever we want, rather than having unwanted application installed. The downloads are located here

 The extraction of zip file gives an image which we have to write into our memory card. (Raspberry pi 3 needs micro SD card).

Raspberry Pi recommends Etcher tool to write images into the SD card, it is really cool tool for writing to SSD cards and it is available for Linux system also.

After installation, we can run Etcher, then select image and drive and flash button. Just three step, and wait for a couple of minutes until flashing finishes,

2) Configuration

After image writing completes, we are ready to run Raspbian in our Raspberry Pi. Please note that the installed operating system has not GUI and we need to remotely access it and use secure connection tool (SSH) to connect it. To carry out this,we have two challenges, first, by default, the new operating systems have disabled SSH, second, for wifi connection, we need to set the wifi credentials also. So, lets begin:

a) Set WIFI

Via NIC card, the connection is automatic, we need to do nothing. The are nice tools (I used from available and we can find out the IP address of the device. The default host-name is raspberry.

For WLAN, we have to provide WIFI information. We create a file wpa_supplicant.conf with the following information:


b) Enable SSH

There is a boot folder in the written image and we just create a file "ssh" (WITHOUT extension) in this folder, at the time of booting, it enables SSH so that use pi can access this with Secure Shell.

Default user name: pi
Default password: raspberry

For security reasons, it is advisable to change the password after you logged in.

Some Questions:

1) What is the power supply for Raspberry PI?
Raspberry Pi is really really cool, because it is suitable for everywhere regarding power supply. The smartphone charging cable completely fits. We can use our computer USB port or separate device(that comes with mobile charger) to provide power . That is enough!

2) Is there any chances of overheating CPU?
The short answer is no, because Raspberry PI manages itself to save from overheating and burning. It automatically detect the overheating and accordingly manages the frequency of CPU to reduce the temperature. So, basically saying, the temperature of RaPi never reach above 65 degree and you need not worry about temperate of Rapi which is above normal. The normal temperature is assumed to be 40-45 degrees. My RaPi always around 50 degree with normal operation and it reaches to 60 degree when I implement camera streaming application(motion). However, somebody prefers some cooling mechanism( head sink or fans). Heat Sink is okay, not recommended though, but use of fan makes simple Raspberry complicated, even a bit noisy.

Disclaimer: I have tested with Raspberry PI 3 version 1.3 (2015), may be the process is not exact for other Pi devices. 

Saturday, May 13, 2017

Unit Testing in Java

Why Testing? 

Testing is necessary to deliver a good quality product. Nobody is perfect and the development phase of a software is prone to error. So, if we find the software bugs at the beginning phase, we can reduce the maintenance cost. So, we need proper testing while we do programming and the developer can himself carry out some tests whether his function is working as desired. Basically, it is tedious to manually test all possibilities and the developer himself has tendency to test best case scenarios. But the output product is used by users who never know the details about it. So, a third party user has higher possibilities to discover bugs. But we do not get this chance until we deliver the product, and the customer also do not like buggy software, so the final delivered product should be Bug-Free.

JUnit Testing

In this article, I am sharing the steps on how we can use JUnit framework to test java classes.  To use JUnit testing, we need to include library file in class path. It can be downloaded from and include it as reference library.


1) The unit testing files should be normally separated, you know why? These files are testing files only needed to developer, not the user. So, it is good practice not to include these test files in the production mode. Thus we create a separate folder to store test files.

2) Packaging makes it everything clear. So, we create the same package, and test classes with suffix Test.

Suppose, we create a class

package com.krishna.math;

public class MathFunction{
       private  int A;
       private int B;
       public MathFunction(int A, int B){
public int getSum(){
     return this.A+this.B;

public int getDiff(){
    return this.A-this.B;

Now, we create a test class. The above mentioned class MathFunction resides in src folder, while we create another folder test to store test files. Gradle and maven projects default structure is:

source: /src/main/java
test: /src/test/java

So, under test folder, we define a class to test the above-mentioned class functions:

package com.krishna.math;

import static org.junit.Assert.*;

import org.junit.Test;

public class MathFunctionTest{
     //Write test methods here
     public void testSum(){
       MathFunction mathFunction=new MathFunction(12,34);
       assertEquals(46, mathFunction.getSum());

   public void testDIff(){
    MathFunction mathFunction=new MathFunction(13,12);
    assertEquals(1, mathFunction.getDiff());

So far so good, we can run it directly from eclipse, just right click and run as JUnit. That shows the test result. 

Running Multiple Tests (TestSuite Class )

The above mentioned method runs one test case (test class) at at time. But we are interested to test all test cases as a whole. So, the need of test suite class arises.

We define a test suite class as follows:

import org.junit.runner.RunWith
import org.junit.runners.Suite;

import org.junit.runners.Suite.SuiteClasses;

@SuiteClasses({ LinkedListTest.class, MyStackTest.class })
public class AllTests {


After, we run it from eclipse IDE, it works like a charm. You see the result with proper coloring also, Eclipse helps you!

Test Automation 
So far, we have created test cases and test suite which is run by eclipse. What if you wanna run and get result and do some process. For that you need a test runner. It is an independent runner and you have control over the result of the test. You may create a separate class for test runner or just add it in TestSuite class. 

I create a separate class as follows:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner{
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {



The examples presented are just sample, the real test cases tend to have many methods.