Donation

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 shazin.sadakath@gmail.com

Monday, June 12, 2017

My Visit to Belgium and Europe

I was invited by my Client in Belgium (Immoweb) for an On Site visit to their Head Quarters in Brussels, Belgium. This was a dream come true for me as I have been wanting to visit Europe since I was a Teenager. I was invited by the CTO as he was about to leave the Company and There were crucial tasks which needed to be handled before he left and which needed my physical presence in the company. During that time I have been working for Immoweb as a Remote Employee for more than one and a half years. The timing was really nice and early notification of this visit from my client enabled me to plan my trip and make some travel arrangements to visit some neighboring countries of Belgium also. I flew to Belgium via transit at Doha, Qatar on Qatar Airways on Friday the 3rd of February 2017 and Landed in Belgium early hours of Saturday the 4th of February 2017. I stayed there till Monday the 13th of February 2017.




I was picked from the Airport by Sudesh, A Sri Lankan Colleague who is residing in Belgium on Saturday and he drove me to Hotel Izan Avenue, Louise in the center of Brussels. It was a great Hotel right in the middle of Brussels' most prestigious area. After I got myself checked-in in my room, Sudesh bid farewell to me. I spent sometime settling down in my room then I went out to roam around the city and to visit a famous park in the City by the name Jubel Park also called Parc du Cinquantenaire. I went to that park spent some time and co-incidentally the park had a Mosque, An Air Craft Museum and An Automobile Museum which I visited during my stay there.




On Sunday the 5th of February 2017, I traveled to Cologne, Germany which was some 221 Kilometers away. I traveled on the Bullet Train which enabled me to travel really fast and get to Cologne earlier than I expected. The city was one amazing experience. The city is built around the Rhine River and has a nice cool vibe to it. The people are super friendly and nice which enabled me to travel roam around the city. It was a day well spent!.

From Monday the 6th of February 2017 to 10th of February I was in Brussels, Belgium working at Immoweb. My Colleagues at Immoweb were super friendly and took me to a different restaurant everyday for lunch. They liked the presence of me around them as I am being the First Sri Lankan most of them have met so everyday the conversations were good. It gave me a nice time to bond with them which has greatly improved my work as well. And after Office Hours, I went through the Brussels City Monuments whenever I could. One night I visited Grand Palace, which is the Central Square of Brusells, an area filled with Good Restaurants, Markets etc. It also has photogenic buildings and some of those are categorized as UNESCO World Heritage Sites. On another night I went to the Atomium which is considered as the Symbol of Brussels City.





On Saturday 11th February 2017 I got the chance to visit Paris, France. This was one of the most memorable days of my life as I was a huge fan and admirer of the engineering behind Eiffel Tower. I was lucky enough not only to see it in person but go to the top most of the tower and see the Paris City from there. It was a great experience which I advise everyone to experience at least once. I also visited Arc de Triomphe.





On Sunday 12th of February 2017, I traveled to Amsterdam, Netherlands which was also amazing City to visit. Amsterdam is a worth traveling if you love Civil Engineering. It shows the Effort the Early Engineers have put in order to Build A highly Thriving City on top of Wooden Piles on ground soil which is too soft to hold huge buildings. The reason behind Buildings in Amsterdam being narrow and tall instead of short of wide was also due to this, The Municipality has taxed Each building based on the width. It was overall an amazing city to visit and learn about.




That was weekend was arguably the longest weekend in my life as I traveled to two new countries and close to 510 Kilometers. And on Monday the 13th of February 2017 I bid farewell to Belgium and Europe and Boarded my flight bound to Sri Lanka via Doha, Qatar on Qatar Airways around 4.15 p.m. Belgium time with a lot memories and thirst to come back again. I learned a lot from this visit and it has opened my eyes to see the World in a whole new perspective. Meeting new people, Going to new places certainly has a deep impact on your soul and I advise everyone to visit these places and more places at least once in their life time.



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 org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by shazi on 1/11/2017.
 */
@NoRepositoryBean
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 org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
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;
    }

    @Override
    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
@Configuration
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
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.

References 


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 https://developer.android.com/things/preview/download.html

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 https://developer.android.com/studio/index.html 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 https://developer.android.com/things/hardware/raspberrypi.html as I used a Raspberry Pi 3 but you can follow the correct link based on your Hardware listed here https://developer.android.com/things/preview/index.html.

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

Pros

  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

Cons

  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)

Conclusion

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.

Trackbacks/Pings

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.



References
  1. http://www.penguintutor.com/linux/light-webserver
  2. http://abyz.co.uk/rpi/pigpio/
Parts can be bought at IoT Researcher Shop
  1. HZ-1050 RFID Reader - http://shop.iotresearcher.com/index.php/en/sensors-c-1/hz-1050-rfid-reader-p-9
  2. RFID Card - http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-card-p-7
  3. RFID Tag - http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-tag-p-6
Trackbacks/Pings
  1. Hackaday - http://hackaday.com/2016/11/21/faulty-parking-meter-tracking-system-rfid-to-the-rescue/
  2. BricoGeek - http://blog.bricogeek.com/noticias/raspberry-pi/control-de-parking-con-rfid-y-raspberry-pi/


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



Demonstration

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.




References
  1. https://github.com/iotresearcher/mqtt-kafka-bridge
  2. https://github.com/iotresearcher/spark-streaming
  3. https://www.infoq.com/articles/apache-spark-introduction
  4. https://kafka.apache.org/quickstart

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;



References

  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,




References

  1. http://www.instructables.com/id/Arduino-Fish-Feeder/