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

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/


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.

SCryptPasswordEncoder

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.


ForwardAuthenticationSuccessHandler

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.

ForwardAuthenticationFailureHandler



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.


FormLoginConfigurer.successForwardUrl

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.

FormLoginConfigurer.failureForwardUrl

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.

DelegatingLogoutSuccessHandler

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 https://github.com/shazin/spring-security-examples and following image shows the example in action.



Sunday, March 13, 2016

Home Security using Raspberry Pi + Web Cam + PIR Sensor and Telegram Bot

I saw a post in Hackaday.com which was about using Telegram API to control LEDs attached to a Raspberry Pi. Telegram is a messaging service similar to Watsapp but it allows a unique bot API to support its non human machine to machine communication. 

Because of this feature it is hacker friendly because the possibilities are only limited by imagination. I had a PIR sensor and a USB web camera lying around and thought it would be cool to put together something related to Telegram bot API using those.

In Sri Lanka CCTV cameras selling like hot cake and office and home security surveillance has become a high priority in the last couple of years. Because of this I thought it would be cool to put a Home security service for me using the aforementioned components. 

Thus came up with the following schematic.


And attached the USB camera to the Raspberry Pi's USB port. 


Finally a little bit of coding 




After installing all important Telegram messenger and creating a bot by going through the Documentation of Telegram, finally I managed to get the whole thing working together. You can see the application in action in the following video.



References
  1. Telegram on Raspberry Pi
  2. Raspberry Pi + PIR
  3. Telegram Bot Python Library
  4. Telegram Bots Introduction
  5. Standard Web Cam usage in Raspberry Pi

Thursday, March 3, 2016

Temperature and Humidity Measuring using Arduino + DHT Sensor

Oven, the somewhat correct word which can be used explain Sri Lankan climate at the moment. It becomes so hot during the day and somewhat similar in the nights. Sleeping is impossible without taking a shower. Ideal time to do a project to measure the Humidity and Temperature.

Had the DHT sensor and a 8x2 LCD (GDM0802A) lying around in my electronics collection and thought to put up a small project. 

Unfortunately the LCD didn't have pins so I had to do a bit of soldering. To be honest not the most experienced man when it comes to soldering, so was a bit worried to get started and going. But anyway went on with it and finally managed to do a pretty decent job with the hot iron and it came out well enough to work.


And managed to get the following schematic in place for the project. 

And a bit of Arduino coding to get the sketch done. 

Finally the whole project in working mode.


And video of humidity changing when a cold water bottle is placed near.



References
  1. DHT Library - https://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor
  2. GDM0802A Datasheet - https://www.robot-r-us.com/pololu/sensorlcd/532-gdm0802a-fl-ybw.html






Thursday, February 25, 2016

Asynchronous Streaming Request Processing in Spring MVC 4.2 + Spring Boot

With the release of Spring 4.2 version, Three new classes have been introduced to handle Requests Asynchronously of the Servlet Thread. Which are;
  1. ResponseBodyEmitter
  2. SseEmitter
  3. StreamingResponseBody
ResponseBodyEmitter enables to send DeferredResult with a compatible HttpMessageConverter. Results can be emitted from threads which are not necessarily the Servlet Request Thread of the Servlet Container.

SseEmitter is used to send Server Sent Events to the Client. Server Sent Events has a fixed format and the response type for the result will be text/event-stream.

StreamingResponseBody is used to send raw unformatted data such as bytes to the client asynchronously of the Servlet Thread.

ResponseBodyEmitter and SseEmitter has a method named complete to mark its completion and StreamingResponseBody will complete when there is no more data to send. 

All three options will be keeping alive a connection to the endpoint until the end of the request.

StreamingResponseBody is particularly useful for streaming large files such as Media Files as writing of the bytes to the Response's OutputStream will be done asynchronously. StreamingResponseBody has a writeTo(OutputStream os) call back method which needs to be overridden inorder to support streaming.

I wrote a small Spring Boot Application to showcase the StreamingResponseBody capabilities in terms of Streaming large files. The application source code can be found at www.github.com/shazin/itube. Below is a screen shot of the application.




In order to send the Video files streaming to the Projekktor player in the web page following code snippet is used.


And a Custom Web Configuration to over ride default timeout behavior to no timeout and finally configuring an AsyncTaskExecutor


References

Tuesday, February 16, 2016

The New Buzz Word in Information Technology Space - Microservices

Microservices, a word which is buzzing around information technology space almost everyday. What is this means? How did we get here?

Well to start off we need to go back to good old days to see how enterprise applications were developed. Most of the application developed at that time where Monolithic. Monolithic applications are collection of functionalities grouped together which will eventually be compiled and packaged into one single application. In Java terms a Jar/War/Ear. In a monolithic application if an application had a Web Tier, User Registration and Account Registration functionalities. All these will be packaged and deployed as a single unit. Let's see the pros and cons of this approach.

Pros of Monolithic Applications

  1. Easy steps to build (can be automated).
  2. Ease of monitoring.
  3. Centralized code repository.
  4. Code is monoglot.
Cons of Monolithic Applications
  1. As the Application becomes complex and large building can take significantly more time.
  2. During deployment of newer versions there are down times for the application.
  3. Less Flexible for change.
  4. Difficulty in scaling out. 
These cons are blockers for High Availability/Low Latency, Rapidly Evolving applications. In order to address these Software Architects tried Modular/Component programming where each functionality is developed as a separate module/component. This in fact managed to reduce the build time drastically. But the applications were still monoliths. Deploying a newer version of a module/component meant that the whole application had to stop and restarted as they are run in one process.

Enter Microservices, According to Martin Fowler microservices means;

The term "Microservice Architecture" has sprung up over the last few years to describe a particular way of designing software applications as suites of independently deployable services. While there is no precise definition of this architectural style, there are certain common characteristics around organization around business capability, automated deployment, intelligence in the endpoints, and decentralized control of languages and data.

His illustrations clearly distinguishes a Monolithic application from a Microservices architecture.

Figure 1


Having a Microservices based architecture means that your applications can;
  • Evolve rapidly and independently.
  • Deploy and test a service easily.
  • Scale out or in for demand.
  • Be polyglot (Each Microservice can be developed using separate Programming Language/Database).
  • Be developed by Small, Independent, Easy to Manage Teams
Lets have a look at Hypothetical Microservices Application. The company MS have business cases for User Registration and Account Registration. User registration has sub use cases (Persisting User, Sending Email Account Verification, Verifying Email Account) of their own which are complex and time consuming but must be flexible for change. Account Registration means maintaining the financial aspect of the User. Furthermore there is a REST API web tier open to users and partners. The following microservices architecture can be used to implement MS company application.


For implementation we can use Spring Boot as it allows to create both Web Container based as well as Stand Alone Production Ready applications using minimal coding. There are more alternatives if you are not a fan of Spring at https://github.com/mfornos/awesome-microservices

When Rabbit MQ is installed successfully, this microservices based application is ready to run. The applications are so independent there is no order to start the microservices. Just running the Main methods of all three projects would spin up the required service.

When a Create New User request is sent to the Web REST API it is enqueued in ms-user queue which will be dequeued and processed by User Registration microservice, which after finishing processing will enqueue another message in ms-account queue. Those messages will be handled by the Account Registration microservice. 


Request to REST API

Message Dequeued from Rabbit MQ queue ms-user


Message Dequeued from Rabbit MQ queue ms-account

All the microservices run on their own process and can be modified, scaled out and/or in transparently. The implementation source code can be found at https://github.com/shazin/ms

References