Category Archives: pi

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.

 

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 pi.nikhilbhardwaj.in 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, babypi.ssh22.net 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 pi.nikhilbhardwaj.in

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

;; QUESTION SECTION:
;pi.nikhilbhardwaj.in.      IN  A

;; ANSWER SECTION:
pi.nikhilbhardwaj.in.   13171   IN  CNAME   babypi.ssh22.net.
babypi.ssh22.net.   60  IN  A   106.51.135.186