Google AIY Thoughts

Like many people I was very excited to see the Google AIY Raspberry Pi addon. Before my copy of the MagPi magazine arrived with the kit I had already installed the software on a linux laptop. One of the things I liked about the project is that, whilst it is to some extent targetted at the Raspberry Pi, it is actually a very good example of using standard modules and frameworks to produce something that is very cross platform and should work very easily on a variety of systems with very little work.

One of the aspects I would like to talk about today is the method used for the LED status indicator. Unlike the speech processing the LED is rather Raspberry Pi specific, as it depends on GPIO locationsupport which is not likely present on other platforms. However the way that this has been implemented allows you to ignore or reimplement it quite trivially in some other way.

How did they do this? Simply put they used a system called mknod to create a FIFO that can be accessed as a file. This file is then used as the input source to the led.py python script. Anyone that wants to change the led state simply writes to that file.

I have produced a simple example that illustrates how this can be done quite easily.

First the python file which will receive messages.

This simply loops until it sees user input that is equal to “Hello 9” printing what if not a:
continue
ever is input in reverse (note this is for python 2.7, if you are using python 3 you would replace raw_input with input).

Next the producer

This writes to a file called myfifo.txt , flushes the write after each line and then sleeps.

If you just run this as it is you will end up with a file containing lines from Hello 0 to Hello 9
The secret sauce is in the next bit.

This will compile the C code for the producer
use mknod to create the fifo
Launch the python script in the background redirecting myfifo.txt as user input
and then launch the producer which will write to the fifo.

There you now have a nice simply way of sending a message from one process to another.

Rolling my own Home Automation

I have been quite keen on the whole home automation trend for quite a while. I had a Ninja Block, and signed up for the Ninja Sphere too! Unfortunatly things didn’t really pan out with the Ninja platforms in the end, I am not sure what happened in the end, those of us who signed up for the Ninja Sphere, got our boxes with the system in, but the platform just didn’t seem to be ready and phizzled out 🙁

However things have moved on and home automation is picking up more mainstream interest now. There are several off the shelf solutions to choose from, however I still have bits and pieces from the Ninja Block system and don’t feel like splashing the cash to buy a whole new system. So I have been slowly getting things back to the state where I have my own system!

Step 1 Controlling the Light

My lights are some of the cheapest available, http://www.limitlessled.com/ as well as being pretty darn cheap, the wifi hub is accessible to developes and you can find information about the API online, which is handy as it means there are plenty of libraries available for working with them. My version (https://github.com/buxtonpaul/myha) is heavily based on a few others, but with some changes/additions to make it work the way I want. Namely

  1. Allow use of a messaging system, I wanted to be able to control the lights potentialy through a number of different routes. To do this I have a a service running on my home server which receives rabbitmq messages and turns them into events to control the lights.
  2. Configurable. The decision of what triggers are connected to what lights should be done through simple config file, which can potentially be used later for other things. e.g. display of state of lights/bridges?

With the server up and running I can then control lights quite easily with a bash script (which simply calls a python script that sends the rabbitmq message to the server).

Once I had control of the lights here it was pretty easy to add control of these to HA-Bridge

https://github.com/bwssytems/ha-bridge

This is a service that also runs on my home server that pretends to be a Hue bridge. This allows things like the Amazon Echo to see my lights and be able to control them.

Step 2 Sensing stuff

One of the side effects of having been a user of the Ninja Blocks platform, was that I ended up with a couple of sensors that worked with the platform, these used a 433MHz RF signal. As it happens I also have the receivers for this from the original ninja block (for some reason I decided to canabalise this). Hooking this up to a Raspberry Pi was pretty straightforward, using these instructions

Decode 433 MHz signals w/ Raspberry Pi & 433 MHz Receiver

The only thing I really had to do to get things working how I wanted was to change the way the RFSniffer program output the codes so that it flushes stdout after every line rather than buffering it up (it took me a while to realise that was the problem). I then simply piped the output of rfsniffer into my lightsender.py and it now automatically triggers lights.

Future things for me to look at involve perhaps adjusting the lights based on time of day, and linking it up with the alarm clock I am developing (more of this later!).