Media Centre Automation

Today, being Saturday and having a bit of spare time on my hands, seemed like a brilliant idea to have a go at furthering my “skills” with the Amazon Alexa gadget. This time, I wanted to see how an integration between Alexa and the KODI media centre would be like. In simple terms, the workflow is kinda like this:

  • you tell Alexa a command
  • Alexa calls your “skill” built specifically for this occasion
  • this “skill” recognises your “intent” and communicates with an Amazon Lambda function that you have to build
  • the Lambda function sends a message to the webserver you built in your home and faces both the “outside world” and your intranet
  • the webserver interprets the Lambda signal and instructs the media centre to do your bidding
  • done!

Again, as I said, that’s in simple terms. The reality was a bit more complicated, but really exciting, I have to say.

Step I: The Raspberry PI Webserver

Yesterday evening, I dusted my Raspberry PI which was powered of since… a few years ago 🙁 Installed a new, fresh OS, went with the lite version of Raspbian, which is based on Debian Jessie. No graphical interface, no window managers, just a “hardcore” CLI. Some time later, after the OS installation, got nginx as a webserver and PHP5 (the fpm “flavour”) for the scripting interpreter up and running. The conf file for the nginx server looks like this:

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html/xbmc/public;

	index index.php;

	server_name _;

	location / {
		#try_files $uri $uri/ =404;
		try_files $uri $uri/ /index.php$is_args$args;
	}

	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
    		fastcgi_pass unix:/var/run/php5-fpm.sock;
    		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    		include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

Note: I later added a htpasswd protection, since the webserver is web-accessible. Once that was out of the way, it was time to proceed to the next step:

Step II: The Web Application

Ok, the webserver is up and running, time to build a web-app that will allow me to send control messages to the KODI media centre. Now, KODI’s got a decent API that you can use, an easily enough, it’s JSON driven, so pretty simple to use. Since this is just for testing/playing, I didn’t bother to enable basic http-auth in KODI for this, so I can easily send messages to it. Right! Since lately in the office the platform Laravel was mentioned, I thought it’ll be good to step out of my “comfort zone” that is Magento and try different things. Easy enough, “composed” laravel and created a new project. For IDE, I am using PhpStorm – is there anything better, anyway?! 🙂 No expert in laravel, but a couple of hours later was having a functional web application that was sending KODI pretty much the basic remote control signals: arrows (left, right, up & down), the “select” key, volume 1-100 in increments of ten, a “back” and “home” signals and that’s about it. Enough to start playing. The entire codebase for the app is available on my gitHub page: https://github.com/georgeschiopu/kodialexa

Step III: The Alexa Skill

Time now to dive in the Amazon Developer Portal and put the “skill” itself together. It’s not something that’s rocket-science, it’s more a matter of reading a bit of documentation (and they really do have documentation!) and do some trial and error. This is pretty much where you’re defining your Alexa skill interaction. You have your “intents” and your “utterances”. These are kind of how your voice commands are mapped to the application, what functions should be triggered and what parameters should be sent to your (next step) Lambda function.

{
    "intents": [
    {
        "intent": "GoRight"
    },
    {
        "intent": "GoLeft"
    },
    {
        "intent": "GoHome"
    },
    {
        "intent": "GoUp"
    },
    {
        "intent": "GoDown"
    }
]
}

GoRight go to the right
GoLeft go to the left
GoUp go up
GoDown go down
GoHome go home
SelectItem select item
SelectItem proceed
SelectItem engage
GoBack go back
GoBack level up

Step IV: The Lambda Function

The Amazon Lambda is a cloud platform that enables you to run your own functions/apps without the need to run a a dedicated server available online. You’ve got a choice of a few programming/scripting languages (C#, Python, NodeJS), I’ve chosen to go with NodeJS, closer to my day-to-day operations 🙂 I won’t paste the entire function written, it’s a mess to be honest, hey, I’m just getting started with this, ok? 🙂

Step V: The Result

Towards the evening, the pieces were ready to be glued into places and to tweak some bits and bobs. After a bit of trial and error, finally got it working. Planning to extend this to be a rich voice interface for my KODI media centre. For the moment it’s pretty basic, but pretty amazing, when you think of the possibilities.  

Comments