If you found the contents in this blog useful, then please make a donation to keep this blog running. You can make donations via Skrill with email address

Tuesday, January 10, 2017

Spring data JPA with dynamic where clause

It may be a Common Use Case where you need to dynamically submit a WHERE Clause to a Spring Data Repository to get an Entity instead of Writing findBy* methods or @Query annotated methods. You can do the following;

import java.util.Map;

 * Created by shazi on 1/11/2017.
public interface IBaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    T findOne(String filter, Map<String, Object> params);

And Implement it as follows.

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.Map;

 * Created by shazi on 1/11/2017.
public class BaseRepositoryImpl<T, ID extends Serializable>
        extends SimpleJpaRepository<T, ID> implements IBaseRepository<T, ID> {

    private final EntityManager entityManager;

    private final JpaEntityInformation entityInformation;

    public BaseRepositoryImpl(JpaEntityInformation entityInformation,
                            EntityManager entityManager) {
        super(entityInformation, entityManager);

        // Keep the EntityManager around to used from the newly introduced methods.
        this.entityManager = entityManager;
        this.entityInformation = entityInformation;

    public T findOne(String filter, Map<String, Object> params) {
        final String jpql = "FROM " + entityInformation.getEntityName() + " WHERE " + filter;
        Query query = entityManager.createQuery(jpql);
        for (Map.Entry<String, Object> value:params.entrySet()) {
            query.setParameter(value.getKey(), value.getValue());
        return (T) query.getSingleResult();
And configure it as follows
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
public class RepoConfig {
or in XML
<repositories base-class="….BaseRepositoryImpl" />
Finally you can use it as follows;
User found = userRepository.findOne("name = :name", Collections.singletonMap("name", "name"));
But you have to make sure that your query WHERE is such that the Query will always return 1 result only.


Monday, December 26, 2016

Hello, World of Google AndroidThings IoT Platform with Raspberry Pi 3

Earlier this month Google Announced AndroidThings a Platform with a Stripped down version of Android Operating System which can be run on multiple existing IoT Enabled Hardware like Raspberry Pi 3, Intel Edison and NXP Pico. On the same day I took this picture;

AndroidThings is primarily focused on reducing the development and deployment time when it comes to IoT. Android being the most popular Mobile Operating System thus far has now made its way into the IoT arena with AndroidThings IoT Platform.

The purpose of this article is to try out the AndroidThings platform and evaluate its Developer friendliness.

Flashing the AndroidThings Operating System

This can be considered as the First Step of the whole process of developing apps for AndroidThings IoT Platform. With your Hardware Device Raspberry Pi 3, Intel Edison or NXP Pico you can download the correct System Image from

And you can follow the Hardware Device's instruction on how to flash the System Image into the Hardware Device. I used a Raspberry Pi 3 so I followed the Instruction available for that.

Setting Up Development Environment

If you were an Android Developer already just like me, you probably have Development Environment setup already and you can skip this step. If not you can Download Android Studio from and install it. Make sure to download the package with Android SDK if you are not comfortable with configuring PATH manually.

Configuring the AndroidThings Operating System to use Wifi

This is the third step as it enables to connect to your device over Wifi for Easy Deployment and Debugging of Apps. I followed this as I used a Raspberry Pi 3 but you can follow the correct link based on your Hardware listed here

Connecting to AndroidThings Operating System via adb command

After finding the Ip Address of the Device you can issue the following command to connect to the AndroidThings operating system via adb command

adb connect <ip-address>

This will connect the your development machine and Hardware device. Now it is just like developing any Android App. You can just use the Android Studio IDE to create apps and run it just like you run an app in an Android Mobile Phone.

Demo (Blink and Button App)

Evaluation of the AndroidThings IoT Platform


  1. Hardware Device Agnostic
  2. Very Gradual Learning Curve (If you are familiar with Android App Development)
  3. Ability use many many Android Tutorials available
  4. Great Community of Developers backed by Google


  1. Limited no of Hardware Devices supported yet (Only 3 when writing this)
  2. Lack of Libraries for reading Sensors like DHT22 and many more in Java. (It would take some time to on board all sensor libraries to Java)
  3. A Steep Learning Curve (If you are not familiar with Android App Development)


Google AndroidThings IoT Platform is definitely a step in the right direction. There are so many Hardware Devices and Operating Systems for IoT it is almost too easy to get lost. With this Platform Google has tried to bring in the "Write Once, Run on Any Hardware" concept to IoT arena which must be commendable. But as far as I think it is still a work in progress and I am pretty sure with the resources Google has within couple years time this platform has the potential to become a Game changer and backbone of IoT.


Tuesday, November 8, 2016

RFID Parking Solution using Raspberry Pi

Sri Lanka has a lot of Parking Lots Managed by RDA (Road Development Authority) but almost all use paper receipts to maintain Timings of Parked Vehicles. This process is error prone and difficult to keep on days where vehicle come and go out frequently.

This motivated me to do a small PoC on a Low Cost RFID based Parking Solution. I had a HZ-1050 RFID Reader lying around in my Hack Space and 1 RFID Card with 2 RFID Tags so I decided to use those.

Above is the Setup which is really simple, Just need to wire up Raspberry Pi 5v -> HZ-1050 5v, Raspberry Pi Ground -> HZ-1050 Ground, HZ-1050 D0 -> Raspberry Pi Pin 14, HZ-1050 D1 -> Raspberry Pi Pin 15

Finally the Code.

Reading RFID Card/Tag and Storing in Database

I used PiGPIO Library with a Small a Python Script modified from their Example to read the Wiegand Format RFID No and Store it in a SQLite3 Database.

Displaying in Dashboard

I wrote a small Dashboard which has a PHP backend and runs on Light Web Server and connect to the same SQLite3 Database to display data. The Front End Bootstrap with JQuery and Javascript.

Following is a Demonstration on everything working together. I will upload code once I clean those up. This can store different Vehicle Types and their Charges for the First Hour and Additional Hours and Calculate the Duration and Charge. Which will be great for the Parking Operator.

Parts can be bought at IoT Researcher Shop
  1. HZ-1050 RFID Reader -
  2. RFID Card -
  3. RFID Tag -
  1. Hackaday -
  2. BricoGeek -

Tuesday, November 1, 2016

Real Time Streaming Data Processing with Arduino + Raspberry Pi + Apache Kafka + Apache Spark + MQTT

World is constantly going towards a fully automated environment where Smart Industries to Self Driving Cars coming into the scene everyday with the hype of Internet of Things (IoT). Almost all of these automation generate Gigabytes if not Terabytes of sensor data which needs to be processed to make sense of what is happening in the automation.

So for me the most important thing of this would be the ability to develop Scale-able backend systems which could process these data and make them into information which can be used to make strategic decisions whether it be in a Smart Industry to predict which part needs to be replaced and for Self Driving Cars when to hit the Garage for a Service.

With this in mind I researched on how to build these types of backend and found out that Apache Spark is a go to open source framework tailor made for this. It has the capability to process data in Batch, Real Time and to make sense of those data with its Machine Learning and Graphing capabilities.
This made me intrigued to learn more about it and this is my attempt on trying to implement an end to end system from pushing sensor data to real time processing of it.

For this I am using a hypothetical Smart Home with a Thermostat which sends data to a backend  server which then processes and understands what's going on. For Demo purpose I will be sending Temperature and Humidity readings from My home hall to the Backend Server via the following flow and calculate in real time the minimum, maximum and average readings of Temperature and Humidity in every 30 second window. 

System Architecture 

Arduino with Ethernet Shield and DHT22 Sensor


In the demonstration I show how the DHT22 Sensor Readings are coming into Apache Kafka and then finally being processed in Apache Spark. To differentiate the Temperature and Humidity I placed a Tub of Ice Cream near the DHT22 Sensor.


Saturday, September 24, 2016

Advanced Arduino Programming : Threads, Semaphores and Mutexs with ChibiOS-Arduino

Arduino is a great tool for rapid prototyping. It has a good Hardware and Software platform to quickly get started. But when it comes to Advanced Software concepts like Threads, Semaphores and Mutexes the Vanilla Arduino IDE doesn't have much to offer.

That is where ChibiOS comes in. ChibiOS is Real Time Operating System (RTOS) which is intended for Embedded system. Likely ChibiOS has a port for Arduino which can be used in Arduino IDE as a Library.

Using this allows Parallel code execution, Synchronized code excution and many more possible in Arduino. Previously I have done a small project to show Temperature and Humidity on 8x2 LCD Display.

This project has two parts, a Sensor Input Reading and LCD Output Writing both of which are done inside the void loop() section of the Arduino Sketch. But as we all know Input and Output are tasks can sometimes take longer than expected. For Ex:- During Network Input and Output.

So It is better to Parallelize the Input and Output. This can be achieved with ChibiOS-Arduino Library.

So I used the same setup from the aforementioned post with Threads this time to separate Read and Write. Plus Reading will take place every 200 Milliseconds but Writing will take place every 1000 Milliseconds (1 Second). And Read Thread and Write Thread communication is done via a Semaphore. Write Thread will wait on the Semaphore for data to be available and Read Thread will Signal on the Semaphore whenever data is available. So the Serial output for the Arduino Program will look like following, 1 Output Thread for 5 Input Threads

The full Arduino Sketch is following;


  1. ChibiOS
  2. ChibiOS-Arduino
  3. Getting Started With ChibiOS

Sunday, September 4, 2016

Quick Hack Arduino Marine Fish Feeder

Anyone who owned a pet would know that the biggest worry you have is when you leave your pets at home on vacation is how to feed them.

I am a novice Marine fish keeper with a 32 Litre Nano Saltwater Fish Only Aquarium. The livestock is not much but I have 2 Clarks Clownfish (Amphiprion clarkii) which are common in Sri Lankan waters, 1 Ocellaris clownfish (Amphiprion ocellaris) which is actually not so common in Sri Lankan waters but imported from Pacific Oceanic waters specifically Indonesia and 1 Sand Gobi which usually stays under the rock. 

Sri Lanka has strict policies on handling Live Rock and Corals in order to protect our shores, so it is almost impossible to find a legal pet store having those. Importing also strictly controlled.

Anyway I wanted a way to feed the fish while I was away on Vacation for 2 days and 1 night. So with this intention I put up a quick hack fish feeder within couple of hours which actually managed to do the job and keep my fish fed and alive. I had to come up with this because Automatic Fish Feeders are also not so common in Sri Lanka and one have to import it from online shops if they really want one.

I had some servos and an Arduino UNO Rev 3 hanging around and managed to put a small step up where Arduino pin 9 is connected to Servo's actuating pin and 5v to power pin and ground pin to ground.

With this small setup I took the Example for Servo Sweep and Managed to modify it so that the Servo does 180 Degrees rotation 5 times every 6 hours.

I took a candy container, cut it to make it shorter and put some holes near the bottom with a hot nail. Then went in the fish food. Finally I used to box lid to hold the servo in place and managed to secure it with rubber bands, powered the Arduino using a 12v power adaptor and voila! I have a Quick hack fish feeder.

You can see the feeder in action below in video,



Tuesday, April 26, 2016

Spring Security 4.1.0 Features implemented by Me

Recently I got the chance to contribute to an open source project which I have been using for a really long time. Spring Security is one of the de-facto projects when it comes to protecting Web Applications as well as some Standalone applications.

It provides many features with regards to Authentication and Authorization out of the box and it is highly customizable and extendable to work with any third party, proprietary Security implementations (SSO, OAuth, Openid etc).

I took the dive in by forking the Spring security project available at github and went through the issues and found issues that are interesting to me and sent pull requests. I must thank Rob Winch who is the Spring security Project Lead who instructed me on how things need to work.

Before I knew it I have submitted 4 pull requests which have been merged with of course minor changes based on Project Lead's review. The Spring Security version 4.1.0.RC2 is now available to use if you are using the Milestone repository of the Spring project. Which means the features I implemented are almost available to the rest of the world.

Well I took that opportunity to write about the features I implemented which are also documented in the Spring Security reference guide. Following are the features I partially/fully contributed to Spring Security.


Spring Security ships alot of Password encoders such as MD5, SHA which the developers can use to encode the password before storing in the database. This gives added security in case of Security breaches because all the hackers will be getting is an hash not a clear text password.

Scrypt is a similar encoding algorithm and I implemented the PasswordEncoder implementation using the Bouncy Castle library to be used in Spring Security.


AuthenticaionSuccessHandlers give the ability to extend the capabilities of the Spring Security to allow the developers to do a task after a successful login by a user. This can be auditing, etc. In order to forward to a URL after the successful authentication the user had to write custom implementation of AuthenticationSuccessHandler. Well not any more.


AuthenticaionFailureHandlers give the ability to extend the capabilities of the Spring Security to allow the developers to do a task after a failed login by a user. This can be auditing, locking out etc. In order to forward to a URL after the failed authentication the user had to write custom implementation of AuthenticationFailureHandler. Well not any more.


Since creating and initializing ForwardAuthenticationSuccessHandler can be cumbersome at times. The lead wanted a convenient method to set successForwardUrl in a fluid API way. Now the same thing can be accomplished using XML as well using authentication-success-forward-url under form-login XML tag.


Since creating and initializing ForwardAuthenticationFailureHandler can be cumbersome at times. The lead wanted a convenient method to set failureForwardUrl in a fluid API way. Now the same thing can be accomplished using XML as well using authentication-failure-forward-url under form-login XML tag.


LogoutSuccessHandlers are an extension in Spring Security where an action can be performed based on Success of the logout. For this there can be different use cases for example. If a request came from a web browser the logout may redirect to a web page after logout. But if the request came from within an AJAX can there seems to be no point of redirecting to page but better to return a status code alone. This implementation takes care of that in a configurable way. A request matcher and logout success handler is mapped based on the request criteria.

The complete source codes for the examples is available at and following image shows the example in action.


Spring Blog -