How to make an Amazon Echo Dot work with your old HiFi system (with a little help from a Raspberry Pi)

So I imagine many people connect their Echo to a modern Bluetooth speaker which achieves the convenience of always being on and the eco-friendliness of not using much energy when idle. Well, I haven’t got one of those. I have an old JVC stereo which you have to power on manually. Luckily I also have a Raspberry Pi which, as you will know, is able to solve pretty much any real world problem.

1. Convenience

I do not want to have to press the power button on the HiFi to hear the Echo.

So you’ve plugged the Echo into your hi-fi (and not even bothered to remove the Airplay device which you never wanted but couldn’t afford Sonos), now what?

You need to use the awesome power of the Raspberry Pi to….control an infrared LED. I admit that controlling a single LED seems a waste of the Pi’s abilities but at least it’s cheaper than forking out for a Logitech Harmony hub.

Don’t have an infrared LED? No problem. Just desolder one from a remote control.

Then follow some instructions on how to get your Raspberry Pi transmitting IR signals.

If you can’t find the Lirc codes for your remote then you can use Lirc to record the signals from your remote. Of course you’ll need an IR receiver to do that. Don’t have one? Just desolder one from a set-top box.

Eventually, you’ll end with something that looks like this:

I should stress that electronics is not my forté.

I followed Amazon’s tutorial on how to create a Smart Home Skill. If you want to do it in Java and need some inspiration then look in my GitHub repository. I’ve even started creating the domain model which doesn’t seem to exist in the Alexa skill jar. ProTip™: When using Java in an AWS Lambda start an instance with a lot of memory so it loads faster.

I did have a problem enabling the skill from the Alexa app on my phone. In the end, I got it to work by using Firefox and pre-logging into my Amazon account (it took quite a few attempts though).

The last piece of the puzzle is to “web-enable” the IR functions of your Raspberry Pi so that the AWS Lambda function can call it. To get going quickly I used a Python project from this guy. You’ll need to create a hole in your home network by mapping a port to the Pi. I will deal with this security issue another day (I don’t want China turning on and off my home electronics).

At this point, I can now say “Alexa, turn on the kitchen hifi”. Convenience achieved.

Bit of delay switching on the hi-fi as the AWS Lambda takes time to start.

I have also considered attaching a PIR sensor to the Pi so that it turns on the hi-fi when you walk in the room.

2. Eco-friendly

I often forget to turn off electronics when I go to bed – I don’t want to leave the HiFi on all night

Now that all the hard work has been done this part is pretty simple. The simplest approach I could think of was to use systemd timers to schedule a curl call late at night. One minor issue being that I wouldn’t know whether the HiFi was currently on or off. Luckily sending the ‘AUX INPUT’ command over the infrared switches on the HiFi. So I can guarantee that the HiFi will be off by first sending this command followed by the ‘POWER’ command a few seconds later. This means the HiFi will briefly be turned on even if it was already off but that’s acceptable.

Another way might be to set up an Alexa “scene” so that I can turn off lots of electronics when I go to bed.


For my next project, I need to buy another Echo and another Raspberry Pi so I can control my TV. If a can control my Roku then hopefully I can make Alexa play a movie from my:- Netflix; Amazon; Now TV; Plex services.

Java 8: A poor man’s functional language

It’s all about immutability

I was having a pint with some Java peeps the other week and was hopelessly trying to explain why Java 8 is not a proper functional language (I’m thinking about
Haskell and Scala…not Clojure because this text is already in brackets and I don’t want any more of them).
We were 2 pints in and I think I opened with something like, “it’s all about immutability”. This statement provoked absolutely no response from those gathered. In fact, I whittled on for another 2 minutes without eliciting so much as an eyebrow raise. I tried to backtrack with, “you remember what Josh Bloch says in ‘Effective Java’ about immutability. It never made sense to me at the time but now I see why”… still nothing.

The reason it never made sense to me is because it was wholly error prone. The developer had to make sure all fields were final and that getters and setters were defined properly. Then you had to make sure equals and hashCode methods were correctly implemented. Admittedly you could get the IDE to generate all this boilerplate for you but you still had to go through the bore of doing it. Enter Scala’s case class. No need to generate boilerplate. No temptation to make introduce mutability into the class – because you can’t.

Why is immutability key? Well, there’s the quick response how it makes concurrency safer and hence easier. But why is that? Well if concurrency is safer then it’s most likely because your code is less side-effecting. I would go on to argue that if there are less side-effects then your code is most probably easier to read and easier to reason about. I’m sure many would disagree as Scala code can have the unfortunate propensity to never declare variables. A lack of variables can create dense code which takes more than a cursory glance to understand – but you do get used to this, trust me.

If code is easier to reason about then, I would argue, it follows that more complex business logic can be written. As Scala achieves higher levels of abstraction (e.g. with Futures) then it is possible to write code that does more, faster and better.

Immutability makes even more sense when you consider microservices or technologies like Akka/Spark. If units of data are being sent to remote systems then they must have gone through some serialisation/deserialisation process. This marshalling and unmarshalling of data is inadvertently enforcing some kind of immutability as memory pointers must reference some finite object graph. So what? If you are writing code that works with immutable data then you are most likely writing code that will work with all these new fandangled data processing systems. Which is a good thing!

An imperative programmer could counter with the argument that immutability is not “performant” and causes extra garbage collection. “Sorry, I see you are working with a list of a few thousand elements. Are you having some performance issues? Can your 486 not handle all that object creation and GC? …..oh no wait you have a 4Ghz i7. Yes I forgot it’s not 1990 anymore”. I have no doubt that working with immutable data incurs much more object creation and copying. But if you consider my point about how more complex business logic is easier to implement and understand then which is more expensive?

  1. New Xeon core server
  2. Team of developers trying to work out how some low-level, imperative code works (i.e. Java)

Gas! Gas! Gas!

Continuing from my how to create your own (gas) smart meter post…

Now that I have all the meter reading values I can produce a D3 graph comme ça:

Not massively useful I’ll admit but it’s a start. What I’d like to see is how much gas I’m using a day. So I’ll need to group the values by day and map them to another form. Map? Group? That sounds like a job for Scala!

The stored data is very simple:

A little bit of Scala allows us to workout the amount of gas consumed each day. I’m not saying this is the best way to do it – more just an excuse to use Scala.

Actually, as the boiler runs in the morning and the evening, I’d like to see the difference between am and pm usage.

(I have another version of these methods which are curried but I thought they were a bit nuts to show publicly).

At which point the data becomes a bit too structured making it difficult to feed to D3. So we flatten it again to:

Then we can get our daily usage graph:

Gridlines correspond to the right-hand axis so you can see cost. That’s a lot of gas being burned.

“No SQL” with Liquibase and jOOQ

Not NoSQL but accessing a conventional database with “no SQL”. I don’t hate SQL but I do avoid writing it if possible.  Java and SQL don’t go well together. Large volumes of SQL can become a maintenance nightmare if you’re changing your domain model around.

Yes, I know. The model should hardly change as it should have been perfectly architected from the very beginning of the project……..don’t get me started.

I have always used Hibernate/iBatis in conjunction with Liquibase but wanted to try something else. Even the infallible Hibernate can become a little tedious sometimes. My friend Google led me to jOOQ. It’s a fully-featured database framework with all the bells and whistles you can think of. Any database architect will appreciate its SQL-centric approach.

I had a whirl with it’s code generation feature and was impressed. With Liquibase versioning the database and jOOQ reverse engineering it to generate Java objects I felt assured I had a solid, fault-free build (as far as mapping Java objects to database tables goes). One minor niggle is that if you’re using Maven you will find that the jooq-codegen-maven plugin will run at the generate-sources phase whereas the liquibase-maven-plugin normally runs at process-resources. The problem being that we want to manipulate the database before reverse engineering it.

This is easily fixed by changing the liquibase-maven-plugin phase to generate-sources and then adding a concise [cough] bit of Maven to copy the required Liquibase files into the target directory (earlier than it would normally):


How to create your own (gas) smart meter

Smart meters are all the rage these days so I thought I would have some fun turning my gas meter into one. As my gas bill is three times as much as my electricity bill this might even have some use (haven’t found much use for my electricity smart meter yet).

Firstly, set up a cheap webcam to look at the meter. Every expense was spared with a £30 Tenvis camera:7894

The quality isn’t that high but it should be good enough for some basic text recognition. Note that I had to carefully position the camera to reduce glare from the IR LEDs reflecting off the meter’s glass. I also increased the contrast on the camera as this would make feature extraction more reliable.

Now all I had to do is run the image through some OCR software and read the characters. Simple, right?…..well….sort of. Although this is a controlled environment I don’t know of any software that will reliably extract text from this image. There were a number of OCR APIs at my disposable (Tesseract, Java OCR) but I first had to simplify this complex scene. I did have the advantage that the meter is under the stairs so the image would not be affected by any lighting changes.

How to extract just the digits from the image? I could have just manually defined a boundary area on the image around the digits. But this didn’t seem particularly robust (if the camera moved position, say) and I was keen to use some computer vision cleverness.

I thought it was most probably a good idea to deskew the image first before running any feature extraction algorithm. An ImageDeskew class (found in Tess4J) provided some Hough transformation goodness. This does rely on your image having some distinct horizontal/veritcal lines in it.deskew

Onto feature extraction, I came across the BoofCV library which had some good examples and got me thinking about the best way to extract the characters from the image. I chose to use its binary segmentation capability to try and find areas of interest in the image –

I ran the binary image extractor using code taken from this example.contours

The white line represents a bounding region and the red lines internal bounds inside the outer one. From this image you can see that 2 of the characters are part of the outer boundary and 2 are part of the inner (plus some other erroneous regions). But these were not the only contours found in the image so how did I classify this image to be the one of interest? Luckily, the other detected contours were completely implausible and had far too few or far too many internal boundaries.

So taking the maximum area of the outer boundary we can extract a pretty good image of just the digits.


Not perfect but hopefully good enough for Java OCR.

I assumed an OCR extractor would want black text on a white background so I created a negative of the image:


Then it was just a matter of running the OCRScanner‘s scan method (with the necessary training images added of course). Java OCR does have a character extractor but I created my training images manually with IrfanView:


These steps might have come across as being pretty straightforward but it did, in truth, require a bit of code tweaking to make it work for my particular setup (especially to Java OCR). In Hindsight manually specifying the region on the image where the digits are would most probably be more reliable/straightforward.

And what is the current reading? I created a RESTful service to get the webcam image, run the OCR and then provide a JSON result. So assuming my service is able to connect to the web camera at my house and that OCR recognition is working correctly you should get an image and corresponding reading below:

It’s a kinda magic!

Now all I need to do is run the stored values through a graph drawing framework (D3!) and see if I can find anything interesting. Comparing it to inside/outside air temperature might be a good start.

Interactive psychological testing with Google Web Toolkit

I recently helped a friend set up on online psychology test. Not being at all familiar with psychological experiments I was directed to an existing software suite for inspiration – PEBL Test Battery.

Any of these tests could be converted into an online version with some nifty Javascript or Flash. But thanks to Google Web Toolkit and HTML5’s canvas feature it’s even easier to create interactive tests.

Although GWT makes it so easy to write web applications it doesn’t help you follow any design patterns (e.g. MVC or MVP).  I used GWTP to implement a Model-View-Presenter front-end (following an MVP pattern with this framework is much easier and it’s well documented). Although GWT now has an API to handle HTML5 canvas it doesn’t seem to be documented anywhere (well I couldn’t find anything other than Javadoc). So I used Vaadin’s GWTGraphics library which is well documented. I think it’s also more cross-browser friendly (or, to be more precise, it fights all the different versions of Internet Explorer – Microsoft like to keep themselves and everyone else busy).

Trail-making task demo here »

JVM Future Memory

Get the heap space to within an inch of its life and the JVM goes into “Quantum” mode.

JVM Future Memory

JVM Future Memory

As I understand it, the JVM will detect a PermGen dump before it even happens! This will be able in Java v5000.

Sorting a paginated DataGrid

Ever had the problem of a paginated DataGrid only sorting on the page that is currently being viewed? Yeah, pretty useless I know. I needed the whole set of data to be sorted and a quick search on tinernet suggested quite a few other people did too. Unfortunately none of the blogs I read seemed to have the exact answer. So here is the full working solution – I did this quickly so it may not be perfect. The pagination was done using this chaps implementation so if you’re not using that the below bit of code may not make much sense. A little bit of inspiration was taken from stackoverflow.

After sorting the ArrData list I realised that the refreshDataProvider method doesn’t even use it and instead uses some source property which is an Array representation of the list. Amazingly this has to be sorted separately! I don’t understand why ActionScript has this “feature”.

Cairngorm: Tread carefully

A popular choice of framework when working with Flex is Cairngorm which has a nice tutorial to get you started. The reason I’m writing this blog is because of some code I came across recently. A developer had read the Cairngorm guide, taken it as gospel and then implemented it exactly. The only problem is that he wasn’t writing an application for an online shop which required a shopping cart. In particular the unnamed developer should have ignored the advice on the singleton model and in particular how one implements the IResponder interface.

The Cairngorm example is simple and the singleton model binding to your mxml pages is simple – it’s the quickest way to make Flex’s asynchronous behaviour work easily. If your application is a little more complicated then this simplicity becomes highly inflexible. To make my point a little clearer look at how the result method sets properties on the model.

Ok fine. But let’s imagine a user could have more than one shopping basket (unlikely I know but just to make my point). Or even better, what if your model had separate products for his and her shops. With this pattern you’re now stuffed because the model can only handle one set of products or one shopping basket. Yes, you could change the model so it can handle multiple sets of products, multiple shopping carts but that wouldn’t be the right way to fix the problem.

To overcome this inflexibility all you need to do is ditch the singleton model. Ok maybe don’t ditch the singleton model but only store truly global properties in it – the logged in user, a list of countries etc. Now how do you get your changes from an asynchronous service call to show up in your UI? Well without sounding too radical you could just implement MVC/MVP and pass the result of the event to you controller/presenter class.

I’m not entirely sure what Cairngorm’s Command classes are supposed to do. But my Command classes do virtually nothing and I let the Controller classes manipulate any result before setting it in the view.

Your controller class can then set the result on your mxml page (not very MVC but no bindings required) or you can bind the mxml page to a property on your controller class (the MVC way).

Why is this more flexible? The simple reason is that you have a Controller class for every mxml page. So if you 2 shopping baskets you have 2 ShoppingBasket controller classes. If you have his and her shops you have HisOnlineShopController and HerOnlineShopController – whatever granularity works best. It also stops you using the powerful but ultimately horrendous [Bindable] property on loads of properties in one class which every mxml page is looking at. In MVC the view should only be looking for properties where it needs to be – i.e. the controller/presenter that is handling the display of data in it. That way when you’re trying to find a null reference exception you only need to look at one mxml page (plus any nested pages) and one Controller class to work out where it is. I find this much better than putting a finger in the air and hazarding a guess as to which of the million [Bindable] properties in my singleton model is being set to null and which mxml page is bound to it.

And even better ActionScript is a functional language so it’s really easy to invoke a function after an asynchronous call finishes. Java can only achieve this by using interfaces which is not quite as neat (though the flow is quite clear).