Sunday, 19 January 2014

Efficiency: Coding and German

Rosie, AJAX and JSON

I've been working in modernising the software side of Rosie. At first, I wanted a module which would tell me whether my lights were on or off. As a rudimentary first attempt, I just had a module which loaded the current state of each light and visually displayed it at the top of the page. I then used some JavaScript to refresh it every second.

This works well, especially on wi-fi. You can't see the refresh and as soon as a light comes on or goes off, the little graphical bulb goes off. The problem is, a) it's horrendous for battery usage, especially on a tablet or phone, b) it doesn't work well over 3g, with the module visually disappearing and then reloading and c) it's an awful solution I'm definitely not proud of.

So I set to work yesterday to return an array encoded into json. Then I read that response back from my AJAX query and use that to update the lights. The response comes back as an array containing the ID of the light and the state it has switched to (on or off). I can then use this to update the styling of the particular light to show it as on or off.

Next I need to do an occasional check on the database to see whether another device is controlling the lights. This is rare but if I set a delay on the light e.g. in 5 minutes turn XX off, then I need to control that as well.

Motorbike

Today was a glorious day so rather than sitting on the sofa watching "background tv" whilst coding AJAX, JSON and PHP, I decided to get on my motorbike and go for a ride. I rode from Bramley, Hampshire to Streatley, Oxfordshire. I took a few pictures up there for around 30 minutes and then headed back, completing a circular route. It was a great route and great ride.


This week

This week is a bit busy. I'm practically touring Germany with a trip back to the UK in the middle. 

On Monday, I travel to Frankfurt. I am there all of Tuesday and travel back Tuesday night. I then go into the office on Wednesday morning before heading back to Heathrow Wednesday lunchtime to fly to Berlin. I stay in Berlin all day Thursday and then head back. Friday, I aim to stay in one place all day - Reading.

I have a feeling I'm going to be tired on Friday. It's not quite the craziest week I've ever had though and it's definitely going to be interesting. It'd be easy if I didn't have some heavy-going meetings whilst at these locations.

Fortunately, I use a great app called Trip Case for managing my trips. Usually, it's just handy to have the entire trip in one app, but this week it's necessary. Different airlines, times, hotels and terminals would have me thoroughly confused otherwise.

Netflix... conforming 

I, by default, am a non-conformist. I think there is 20% of me which naturally conforms (I'm not unfashionable, I like my house to be tidy, I follow social etiquette etc) but the other 80% likes to do my own thing.

For example, I tend to have the latest gadgets, but that's not so that I look cool in the street. Actually, I typically end up trying not to show them off because I only buy them because I love gadgets. 

So, what happens is I sometimes stay away from the latest fads, because I don't want to be one of a crowd. This has it's disadvantages, for example I am very late to Breaking Bad, I haven't watched any Game of Thrones (but now want to) and am late to joining Netflix. I have tried it this weekend and it's brilliant. I really like it. I'm using it through my Xbox 360 but since I can use it everywhere, I'm impressed. Whether I use it enough to justify it's cost is another question.


Sunday, 5 January 2014

Meet Rosie (Automating the lights at home part 3)

Meet Rosie
Introducing Rosie, my assistant. She manages my lights for me.


I can make a request of Rosie and, providing she understands it, she completes it.

Example:

Me: Rosie
Rosie: Yes Sir
Me: Living Room and Kitchen On
Rosie: Turning the Kitchen and Living Room lights on
Action: The lights come on!

Background

Rosie has existed for a while, only she had no name. I initially went with Ralf but then a couple of factors made me decide to go with female and Ralf no longer worked. I decided I wanted to increase the amount my lighting system could do and look to branch out from lighting control... and a name seemed like a good place to start!

How she works...

Beforehand...

As mentioned, Rosie has existed for a while. So let me recap on how she works.

Rosie is made up of:

A Raspberry Pi - Essentially, this is Rosie.
An RF Transmitter - This is used to switch the lights on and off
WiringPi - A really neat package that allows each control of the GPIO pins on the Raspberry Pi.
Strogonanoff - A great project I used which transmits the right signals to the Remote Control Sockets
Remote Control Sockets - The remote control sockets the lights are plugged into
Apache 2 - HTTP web server Software
PHP 5 - The programming language for the interface
MySQL - The database technology I like to use. This holds a database of lights, the schedule and groups them into programs.


That's all been working reliably for several months now, so it was time to extend the functionality a little bit.

The new components are:

VoiceCommand - Created by Steven Hickson, this amazing tool utilises Google's Text to Speech API allowing voice recognition to be used on the Raspberry Pi
Logitech Webcam - A webcam with built-in microphone so that Rosie can hear me
Portable speaker - A speaker so that I can hear Rosie

When I say Rosie, she responds with "Yes Sir?" (this is the default but can be configured). I can now say any command and Rosie will try to interpret it. With VoiceCommand I can choose whether I search Google for a result or look inside the config file. I've chosen to stick with the config file for now. I've added phrases for my lights and can ask Rosie to turn them on or off. 

For now, she's restricted to on and off commands but I'll be adding features to incorporate delays (Turn living room off in 10 minutes for example) and scheduling in future.

She's working pretty well but I'll be working on improving her in future. She's also currently restricted to about a 6 foot radius as the microphone and speaker only reach so far... so that needs thinking about too.

Of course there are times when I don't want to talk to Rosie or times when there is a lot of noise (TV on for example), so I still have the web interface which I access on my Phone, iPad or computer.