Thursday, March 6, 2014

GO Cross-Compile for Raspberry Pi

Compiling GO on the Pi isn't simple or reliable, the easiest way I've found is to use cross-compilation.  This is where you compile on one system (e.g. mac) intended for another (e.g. raspbery-pi).  The compiled binary won't run on the originating system, but will run on the target system.

On the originating system, firstly you need to ensure GO's own libraries are prepared to perform the cross-compilation (normally it only prepares the ones for the system you're on):

Find where you downloaded GO and within the src folder run:
GOOS=linux GOARCH=arm GOARM=5 make.bash

You should see a number of additional libraries being compiled, you shouldn't need to do this again.

Now, go back to your GO project and run:
GOOS=linux GOARCH=arm GOARM=5 go build

The binary you just created can now be copied to the raspberry pi for execution

Monday, March 3, 2014

Cost breakdown for my Raspberry Pi Garage door opener

As a follow-on from my earlier post here is the breakdown of cost for phone garage door control.

Garage Door Opener

Garage Door Opener £100 (Wickes)

Raspberry Pi setup

Raspberry Pi        £29   (Amazon)
Pi Case              £5   (Amazon)
16Gb SD Card        £10   (Amazon)
Edimax EW-7811UN     £9   (Amazon)
Micro USB charger    £5   (Amazon)

=======================
Total               £58

Garage Control parts

Relay                £7   (Amazon)
Breadboard wires:
 male to male        £4   (Amazon)
 female to female    £2   (Ebay)
Sensors              £5   (Amazon)
Resistors (300pk)    £4   (Ebay)
Transistor BC547C    £1   (Maplin)
Small Breadboard     £4   (Maplin)
Shoe Box             £Free (wish the kids shoes were!)
Annoyance to Wife    £Expensive 

=======================
Total               £27

Overall Total of £185. Since I had already purchased the door opener and Pi the additional was only £27!

Thursday, February 27, 2014

Raspberry Pi - Edimax WiFi configuration

Raspberry Pi/Raspbian has excellent support for the Edimax EW-7811UN USB WiFi adapter.

The only file you need to edit is at /etc/network/interfaces by adding the following configuration:
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid <SID e.g. VirginMedia123456>
wpa-psk <passcode e.g. abcd1234>

However, it's never that simple! If your router reboots and although I'd expect it to, the Pi doesn't auto-connect.
The solution? Luckily someone has already thought of it. WiFi_Check is a simple bash script that will restart the WiFi adapter until the connection is re-established.

Tuesday, February 25, 2014

Static file web server on Raspberry Pi for development/testing

Maybe like me you're practicing your Dart or Angular-Dart web programming, you have no server side code, just static html, css and javascript files.

A very simple way to get this served up on your Raspberry Pi (or any other linux dist) is to navigate to the folder which contains your files and execute:

python -m SimpleHTTPServer 8080

Your server is now listening on port 8080, just point your browser to http://:8080

Monday, February 24, 2014

Binding to restricted ports with regular user

There are plenty of security reasons to avoid running applications and services as root. This also includes those times when you need to bind to restricted ports like 80 (http) or 443 (https). So what should you do? If you administer the system, use IP tables, the built-in Linux firewall to setup local port forwarding. Works perfectly on a Raspberry Pi running Raspbian.

The initial configuration needs to be performed with privilege to use the sudo command. Alternatively login as root and execute the same without sudo.
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -A PREROUTING -t nat -p tcp --dport 443 -j REDIRECT --to-port 8443

Will route port 80 to 8080 and 443 to 8443. Your app can now bind to port 8080 and/or 8443 without needing elevated privileges.

It was the technique I used for the Google OAuth Reverse Proxy, a part of my Garage Door Opener architecture.

How do you undo? Call the same commands again substituting -A (append) for -D (delete).

Thursday, February 20, 2014

Raspberry Pi Garage Door Opener

The main purpose for this project was to ensure I didn't leave the garage door open either due to forgetfulness or from accidental opening.  This blog post is what started me off but my implementation, design and experience was slightly different so thought it still worth a write up.

Features:
  • Accessible remotely via smart browser app
  • Sign in with Google account (Auto sign-in from Android phones!) 
  • Maintain current status and ability to offer specific 'open', 'close' and 'stop' operations  
  • Audit log
  • Geo-Location based authorization for 'open' command preventing accidental opening while away
  • Email alerts for door activation and door remaining open for set period
  • REST Service - accessible from other applications

Design



Raspberry Pi


There are other hardware options but I chose the Pi - great if you're familiar with Linux and can co-host other projects/services too.

It's small and quiet - mine was put in a small shoe-box and placed on top of the door opener itself.

It's running Raspbian and connected to my home network with an Edimax wifi adapter (I'll do another post to detail how easy it was to setup the AP configuration).

Server


Source Code and Instructions

It is written in python and uses several other libraries to manage things like GPIO events and REST URLs.  All the details are at the link above.

Of course, it also needs electronics - as others have found, you can use a simple relay to trigger the door.   I also used 2 reed sensors for checking opened/closed state.  The wiring/circuit design was a little complicated but got there in the end.  Details and pictures here.

Browser client


Source Code and Instructions

The first implementation used GWT - it worked fine but found it was quite heavy weight (to support) for such a simple app.

As a learning exercise I tried re-implementing in Dart. I found it to be a much cleaner code-base and ended up adding some additional features.  This is the version I recommend to use.

Reverse Proxy Authenticator


Source Code/Pi-Binary and Instructions

It's a bad idea to open up services to the internet without authentication and SSL.  This options means I didn't have to implement auth directly into py_garage_server.  The other advantage is the ability to proxy other internal apps at the same time.

I didn't write this from scratch but forked and modified for my usage.  It's written in GO but it's not simple or reliable to compile GO on the Pi.  Fortunately it is easy to cross-compile (say on a mac or windows) for the Pi architecture.  Using the binary linked above should work directly on the Pi so you don't have to do this yourself.

To have this running correctly you'll need to:




Saturday, March 13, 2010

TiVo has to go, what's an acceptable replacement...

Although TiVo left the UK market some years ago, my trusty box has served us well. The TiVo box itself is actually still working, and I would expect it to continue working for a few more years if it had the chance. What failed was the freeview box it used as a source of input for recording. Since Tivo is not an all-in-one machine which requires another source such as sky, cable or freeview it needs to be able to change the channel on that source. The problem I faced is TiVo no longer supports the IR codes for newer freeview boxes so although it can be attached to the TiVo with the scart cable, it fails to change the channel. I did manage to find one Philips freeview box (DTR220) which worked, but the picture quality was awful, so it had to be returned.

So started, the hunt for an acceptable replacement. It would be difficult match the user friendly TiVo interface.

Options considered:

Sky+
Freeview+
FreeSat+
HTPC

I didn't take into account any HD options since most of the programs aired are just not filmed with HD in mind and the programmes just don't seem real. You just feel like you're in a film studio instead! There are some good shows, such as animations and nature programmes, but I can decide to get those on Blu-Ray if I really want to.

Sky+ was ruled out first. Although it does the job, it doesn't do it very nicely. The UI quality is pretty poor and is never particularly responsive. Plus, I don't want to pay for installation or a monthly fee. The only advantage it has over the rest are exclusive channels such as Sky1. However, those programmes are almost always aired on freeview eventually and I can get them via Love Film if I really want them.

Freeview+ or FreeSat+ seemed like a good alternative. No monthly charges and most (maybe all?) have duel recording capabilities. Freeview+ seemed like the best of the two because there is no installation required and I have a pretty good digital signal. However, after reviewing the 2 options, Humax and Topfield, I discovered they had various limitations such as not being able to add padding (a few extra minutes to the start and end of recordings so you don't miss a show) to series recordings. How crazy is that! Since I already had a satellite dish, I wondered if FreeSat+ would be feasible. It would, and apparently the software on the FreeSat+ Humax box had pretty good reviews. Unfortunately, to get duel channel recording you need an extra cable running to your dish - not more wires, who wants that!

After deciding that I would need a new laptop anyway, I thought that my existing Sony VAIO would be a good option to use as a HTPC. It already has a HDMI port which links up nicely with my HDTV. The only thing to sort out was getting freeview and a remote control linked to the PC. The first option I tried was a twin tuner Hauppauge NOVA-TD USB stick. It has 2 tuners for dual recording and also has an integrated IR remote control - seems like a perfect solution right? Wrong! It had major signal issues on the second tuner and the IR remote I found to be almost completely useless. The remote either didn't work at all or kept transmitting double-key presses, plus the IR receiver was actually located on the USB stick and it had to have line-of-sight so positioning wasn't easy and looked pretty ugly with the aerial cables there too. I returned it after a few days. I had a tip from a forum member that using 2 individual tuners would not cause signal issues so I decided to give the HTPC option one more try. I bought 2 AverMedia Volar HD USB sticks and it was true - I had no signal issues at all and plus, I was sure that the picture quality from these were much more crisp than what I was getting from the Hauppaugge. I also bought an Emprex 3009URF remote control and an Emprex 9039ARF wireless keyboard - luckily, as they're from the same manufacturer they can both work from a single USB receiver. I have been running this setup for a few weeks now and had no problems at all. Since it is essentially a computer underneath, you have all the other added features such as web browsing, viewing/editing photos, music and videos all in one place. Plus, the option to upgrade if new/better software becomes available.