Magic Candles – fun with Alexa and IoT

A while back my wife had picked up a packet of flameless candles from costco. These come with an infrared remote which you can change colors, switch modes between glow and flicker and schedule the candles to turn off after a predetermined time interval.

I thought it’d be cool to be able to have my Alexa controlling these, you walk into the room, turn off the lights and turn on these candles thereby creating a perfect environment for a romantic candle lit dinner. Right?

I got some time over the weekend to work on this and here it is, in all it’ s glory for you to enjoy.

If you’re not interested in the technical details involved in setting this up, then you can stop here. Here’s the hardware that I’ve used –

  • Raspberry Pi (This plays a dual role, it hosts the server that communicates with the IR blaster and it’s also the IoT device that Alexa talks to)
  • Blackbean Mini (This is the IR blaster, it mimics the remote)
  • Flameless Candles and Remote (For obvious reasons)

The software components are –

  • broadlink-http-rest – HTTP server, running on Raspberry Pi to Communicate with the IR blaster
  • AWS IOT SDK – This is also running on my Raspberry Pi to make it an IOT device which listens on an MQTT topic to which the Alexa Skill publishes messages. This reads that message and makes a REST call to our HTTP Server
  • Alexa Skills Kit SDK – For the Alexa Skill, to publish a message to the MQTT topic
  • AWS Lambda, to host the Alexa Skill – I don’t think I’ll ever spin up another web server for my hobby projects

Here’s a visual diagram that represents the interaction between the pieces to paint a clearer picture –

And for those of you who want to dive deeper and play with the code, you can find it on Github.

I’ve also jotted down some notes and learnings based on developing this –

  • The AWS IOT suite is really good, MQTT is a secure way to interact with IOT devices. Going forward I expect to see more devices use this, the current IOT scene is quite messy with every manufacturer spinning up their own HTTP version, with base64 encoded communication which provides no security. With this you can rest assured that it wont be easy for hackers to use you IoT device for the next DDOS attack.
  • The MQTT/HTTP performance is really good, as you can see in the demo; there’s no lag between my wife talking to Alexa and the action being performed, despite this going through multiple systems each running on different infrastructure and written in different languages. The IR Server Bridge is written in python, the Raspberry Pi runs the AWS IOT Javascript SDK, the Alexa skill is written in Java and is run on demand on a serverless platform.
  • REST is awesome, this build upon the previous point. This seamless integration is possible because all these pieces talk REST
  • AWS IOT SDK documentation is atrocious to the point that it almost negates all the great features that it exposes. It took me more than a few hours to read up the documentation, source code and even some Javascript examples to understand how to open a line of communication with my device using the Java SDK. This despite my familiarity with a bunch of other AWS Services. Writing good services and software isn’t enough, if your users can’t figure out how to use it. I hope that my code helps someone else battling similar issues. I have a lot more to say about this, but if I was to go on, it would end up being a rant. I’ll reserve that for a future post(maybe).

I had a lot of fun building this, maybe this can give someone else inspiration for their own IoT project. It’s really easy to extend this to control other devices in your home – TVs, ACs, Game Consoles and a bunch of other things are all controlled with IR remotes. The IoT domain is so much fun these days, the fully automated smart home even on a budget is now a reality.


Smart DNS to sidestep geolocation checks

One of the HDMI ports of my TV is powered by Raspberry Pi, I use this to stream content that isn’t available in my current geography. The most obvious way to bypass geo-restrictions is to use a VPN, these are great in theory but a high performance VPN provider is hard to find and quite expensive as well.

Recently I discovered that Smart DNS is an alternative to VPNs, this is a very clever implementation, which intercepts only a small portion of the traffic that corresponds to the geolocation checks.

I tried out CactusVPN, they’re one of the rare providers that provide a truly free service. Shout out to you guys at CactusVPN, I was able to set this up easily on both my mobile devices as well as the raspberry pi, they have apps for all major platforms/oses(Windows, macOS, Android and iOS) as well as documentation for setting up DNS on linux; which is really nice! The DNS service is available for locales like US, Canada, UK, Germany, Poland and Sweden. Here’s a list of websites that can be unblocked using their services. I hope that they expand their services into other regions as well – India would be swell!

If you want more control, then you could setup your own Smart DNS server in the cloud. Netflix-Proxy looks really promising and I’m going to set it up in India to see if I can unblock Hotstar.

Do note that Smart DNS may not be the best solution for the more adventurous who use P2P, in those cases VPN may be better. But Smart DNS works great for cases where you want to access freely available content or content that you actually pay for and are traveling or are locked out due to poorly implemented checks.

Do let me know if you know of other Smart DNS providers, I’d love to take them out for a spin(specially if they have Indian endpoints)

Path Tracker an Alexa Skill for local train timings

I’ve had the Amazon Echo for a while, recently I had some time on my hands and decided to build a skill that at least I would find useful.

Typically my wife and I talk with Alexa to ask her for the weather, we use it to set timers when doing the laundry or short term reminders and occasionally to play music or audiobooks.

Before leaving home for office, my wife and I generally check the public transit timings. I thought it’d be cool if Alexa could tell us that instead, with this in mind I searched around for PATH schedules and how to access that information programatically. Turns out the Port Authority have this available to developers in the GTFS . After reading the GTFS Reference and playing around with the PATH feed it took me a couple of hours to wrap my head around and to come up with a simple way to find train timings between any two stations.

After I had a working version of the train timings algorithm coded, I proceeded to read the documentation for the Alexa skills programming model, the documentation is a little dry but starts making sense as you code up and test your skill. I found the Big Nerd Ranch youtube videos to be very helpful in getting a general feel of the programming model.

I had initially planned to host the backend as a service on one of my raspberry pi’s but due to the number of hoops that Amazon makes you jump through, I ended up leaning towards AWS lambda. It was a bit of a pain to get started with lambda as the support community is rather limited, you’ll need to figure out some of the things yourself by trial and error. (I had no luck at AWS forums and very little at StackOverflow, overall though StackOverflow seems to be a much better place to ask your questions and get some help)

Once you overcome the initial challenges with running your skill on AWS lambda, it’s actually a fun platform to develop on, I was no fan going into this but came out a believer the Zero Infrastructure and configuration to host code is certainly going to catch on. I do have some complaints about performance during a cold start but I reckon that this is only going to get better with time.

After everything was done and I had tested the functionality with my local echo device, I submitted the skill for certification.
A few days later I heard back from the certification team that my skill was rejected. I got this response

Issues with skill in English (US)

1. When attempting to invoke the skill with your second and third example phrases, the skill fails to recognize the phrases. The example phrases must function without error since these are interactions that users are most likely to try. Please see test case 3.1 from our Submission Checklist for guidance on example phrases.


User: “Alexa Ask Path Tracker When is the next train between Exchange Place and Newark”/ “Alexa Ask Path Tracker Train to Exchange Place from Newark”

The skill fails to recognize the phrases.

I spent an hour or so trying to figure out why the skill won’t recognize the phrase and came to a conclusion that maybe Newark wasn’t the best station to list on the sample interaction, I recalled that I didn’t know how to pronounce it either before I’d moved to the US, so I changed the sample to –

Alexa Ask Path Tracker What time is the next train from Grove Street to World Trade Center

And submitted the skill for certification again and voila after a couple of days I got an email to let me know that my skill was now live.

The source code is available at Github, feel free to share your feedback or certification experience at either place.


Je Suis Charlie

It’s been a week and the dust has started to settle after the horrific acts of terror by the islamists again, this time against France. Acts of terror are no longer rare, they keep happening all over all the time most people are at a state where there reaction is like -‘Oh no, not again! How many innocent people lost their lives this time?’

By those standards the attacks at Charlie Hebdo wouldn’t be too appalling and yet these shook the world, these most certainly did shake me up. The difference in this case is that  whereas most terrorist attacks target a place and the goal of the terrorists is to kill as many people as they can, this one was an assassination with surgical precision.

But just that, is not the reason the world was shaken up. We were all shaken up because this was an attack at the conscience of freedom, freedom of thought and expression. It was an attempt to kill the mocking bird and that is what shakes me.

The other thing that saddens me now that all of this is over is that this will happen again and again and again, we still haven’t learned our lessons from all these attacks. Isn’t it funny how humans are supposed to be the smart creatures whereas the machines are supposed to be one dimensional, unable to think and to learn. If you were to run these incidents through the simplest of classification algorithms, you would stare at the common denominator and know what to fix to make sure that this didn’t repeat. Political correctness however wouldn’t allow society to contemplate on the real solution for it would be too radical and too obvious.

With all that being said, let us come back to Charlie Hebdo; I must admit that I didn’t know too much about this obscure French satirical comic. I did check their work out after this and I must admit it was pretty funny and surprisingly accurate factually which is why cartoons can do what bombs can do, shake the foundation of a fickle faith. The artists who illustrated those cartoons may not live anymore but others will come and you will be an inspiration, your memory and your art will live on and a time will come when we finally win this war, it may not be in my lifetime but I will remember and thank you for your invaluable contribution in this fight and in our shared victory.


New Year Ramblings 2015

I had neglected by blog for sometime now, lots has happened since my last post about setting up raspberry pi. I had a great vacation in Laddakh. After which I transferred to Audible Inc in Newark, New Jersey and the first few months have been great.  I saw my first snow here, spent some time withe Kids at the Ann St. School for the hour of code. Went around ‘New York’ or ‘The City’ as people call it here.

I got engaged last year and this is the year when I’m getting married, some of my friends tell me that it’s too early whereas the parents think this is the right time, I’m with my parents on this one.

New years resolutions this time are getting fit, seeing a remote part of the world and moving forward in my career and maybe penning down my thoughts more often. Last year was tremendous, this one has the promise of all that and so much more. Hope you guys had a great year last time around, even if not have a great one this time around.

Setting up raspberry pi

Here’s a list of things I do to set up my raspberry pi and a small note, the purpose of this list is to help me remember what all I need to setup if and when I start from scratch the next time around.

1. Finding your pi’s IP on the local network!

I don’t have a dedicated monitor and run my pi headless. I used to do this by running a for loop and finding the ips of all my devices on the local network and then sshing into them until I found my pi but this is easier and um.., more scientific!

2. Setup Vim and fish on pi.

I don’t like editing files with nano and so setting up Vim is essential and fish is an awesome replacement to bash/zsh you may want to give it a spin. It’s history management is way better than ohmyzsh.

3. Setup Avahi or Zeroconf

It’s always easier to remember hostnames than it is to remember ips and this is where Avahi shines. Now I can access my pi with pi.local even if it’s local ip changes.

4. Remove Xorg and the desktop packages

I like to run my pi, lean and mean there is no need for the desktop when I run it headless, this saves both the disk space and data with updates

5. Setup a bittorrent web client

The folks at howtogeek have an excellent tutorial on setting up deluge, there are a couple of other alternatives as well but deluge works for me quite well.

6. Give your pi a public ip/domain

What good is your pi if you can only access it over the local internet. The whole point of setting up a bittorrent web ui was to be able to download stuff remotely and watch it after you get home. You can also point your subdomain to your pi as I did in my last post.

7. Set up your external hard drive and samba to share it over the network.

To watch the TV shows or movies that you downloaded on your pc/mac you need to set up samba and also to connect your external hard drive to pi. Connecting the external hard drive is the same as connecting to any other linux box and so I’m omitting the steps here.

I’ll probably add more stuff here in time but this is my barebones pi setup.

Raspberry Pi tip : Map your Subdomain to Pi

After I had my Raspberry Pi setup, I wanted to create a subdomain which would point to it.
This is trivial to set up if you have a static IP but I’m assuming that like me you don’t have that.

The way that I have set this up is by following this excellent post on superuser. First you need to set up a dynamic dns for your pi. I use DNSDynamic for this, a tutorial on how to set this up for your pi can be found here. With dynamic dns set up, it’s like having a static ip.

All you need to do after this is to create a CNAME record which points to your dynamic dns. This can be created using your domain registrar’s web interface. A CNAME is basically a pointer and can point to any IP or domain. In my case I set up to point to my dynamic dns. If you’ve set that up correctly you can use dig to verify this. Here’s what I get, is my dynamic dns. I’d advice you to choose a better name!

After this it’s a matter of forwarding the right ports on your home router and viola you have a subdomain that points to your pi!


; <<>> DiG 9.8.3-P1 <<>>
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57260
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 4

;      IN  A

;; ANSWER SECTION:   13171   IN  CNAME   60  IN  A

Another partition looms

By the indications that we are getting from the media the state of Andhra Pradesh will be bifurcated, partitioned into two and this saddens me immensely. The parallel with India’s partition at the time of independence is all to evident a state is being carved up in two by the power hungry who will do all that is necessary to remain in power.

India’s partition was engineered by foreign forces as is Andhra Pradeshs’ the former british and the latter italian. It is very easy for a foreigner to divide another persons homeland for they can never understand what it means to those who call that place home, but it isn’t the foreigners who are responsible for both these self inflicted wounds it is us indians who are responsible in both occasions. In 1947 it was Nehru and Jinnah both of whom were power hungry and this time with history repeating itself only the actors are different.

Any partition is a painful ordeal for both parties, be it siblings dividing ancestral property or best friends not being friends anymore. It is never a solution and even after decades the scars of the grave wounds remain, reminders of the futility and foolishness of division.

I’m not from Andhra Pradesh so I can’t claim to be an expert on Telangana but I’ve stayed in Hyderabad and Secunderabad for some time, I have a few friends from the state none of them are radicals so it is possible that my analysis is wrong all together. But I can’t see a single reason for dividing the state except for those motivated by political considerations, the people of the state are mainly from the same linguistic community with no regional community dominating other communities.

The solution to the problem that people are protesting about does not lie in dividing the state into pieces but does so in better governance and empowerment of the people. I wonder how much longer will politics of appeasement drive the future of our country. It hardly matters now though, tonight is probably the last when Andhra Pradesh will be one state come tomorrow another partiton looms large.


It’s been a while since I last blogged, sort of lost the interest didn’t know what to write about but you don’t really care about that do you! Lets get to the point and I’ll tell you what has me so excited about Meteor.

New programming languages and frameworks keep coming along faster than the speed of light in the world of technology but most of them just whiz by and hardly interest me. There are however a few which stand out amongst the crowd and Meteor is one of them. I’ve had a bittersweet relationship with JavaScript, back in the day when I had only gotten started with programming with C++ in school I found it like a breath of fresh air. It could add that fancy rollover effect to pages or a bouncing ball on a webpage, I was amazed by it. But the cross browser issues, quirky behavior and it being a nightmare to debug I gave up on JavaScript.

For a few years it stayed that way, I taught myself some php got bored by it and moved to ruby tried dipping my feet in rails but without much luck as it was too magical. I boarded the Sinatra bandwagon, it is an awesome framework. No magic, no nonsense simply rest but then again Sinatra itself isn’t enough to make nice websites you need something for the front end too and that’s when I started to dive into jQuery rediscovering my love for JavaScript. I read a couple of books on jQuery embraced the asynchronous and restful nature of the web and it was such a joy to make web applications again.

After that for the last couple of years things have been slowed down, sure there have been performance enhancements in jQuery and node.js came along too. I had a brief look at node.js but really didn’t see how much more of a value add it was over Sinatra. Similarly Knockout.js didn’t really knock me out neither did backbone have that much of a backbone for me. Meanwhile at work I used GWT which in my opinion isn’t the best way to write a web application today(It may have been 5 years ago, not anymore). Had I discovered the ultimate framework? Had I achieved nirvana? I sure thought so.

That was untill a week ago when I was struck by meteor.js, I had watched the introductory video and my head was abuzz with such excitement after a long time indeed. I watched the second video and boy was I jumping up and down. Meteor isn’t just an evolutionary framework it’s revolutionary! It doesn’t split the client and server parts of your web app but presents them together. It’s how you would develop any application, sure you need to know about the security aspects but when you’re developing something it should be fun, you shouldn’t be spending time on downloading jQuery and your favorite bootstrap framework and other trivial details. Ideally you should be able to focus on the core of your application and all the secondary things should be dead easy to implement. I also like the social focus of meteor, it couldn’t be easier to integrate your app with Facebook/twitter or for that matter any other social networking site.

What trumps all those minor features and enhancements is it’s publish subscribe feature, if you asked me what was the one single feature that defines meteor. You don’t need to implement polling to write a fluid always up to date interface, this isn’t the 1980s. Meteor just works where most other frameworks make you do too much unnecessary work! Also the package management is pretty sleek, it compares and in some cases surpasses the ruby gems which I consider the ideal package manager.

Having heaped so much praise on meteor I still have a few doubts, the most important is about how well will meteor scale? This is one factor that will probably decide meteors future in the years to come. Will it end up as a toy for prototyping stuff or will it dislodge rails from its pedestal remains to be seen. From a technical perspective it’ll be curious to see what strategy they eventually take to scale and perform under high load. Another thing that is probably on meteor’s roadmap is to support the various other nosql stores other than mongo db. It’ll be interesting to see how they implement this, right now the syntax is quite specific to mongo it’d be better if it was neutral so that there could be support for stores like dynamo db with no change in the existing application code. One last thing that would be absolutely awesome is integration with Native Mobile Apps, even though web apps work well on mobile devices native apps are miles ahead. It remains to be seen how easily can one write the app in meteor and then hook up the presentation onto the native platforms. Boy would that be sweet!

All in all this makes meteor a very interesting framework to watch out for even if you don’t write too much JavaScript, it has so much potential where it will be in two or three years time is an open question. Regardless of how it ends, the ride along the way promises to be a lot of fun. I don’t know about you but I’m boarding it right now!

Downgrade grub2 to grub

There was this weird issue that I’ve been facing for some time with both ubuntu 12.10 and 12.04, what happens is that my Acer Aspire One 722 netbook doesn’t boot into linux at times. The first couple of times I dismissed it as a one off but that didn’t stop the problem from recurring. Then I blamed my exotic grub2 setup wherein I had installed grub2 to /dev/sda5 and then was using the windows 7 bootloader to chainload it.
Continue reading Downgrade grub2 to grub

The online journal of Nikhil Bhardwaj