I’ve been wanting to play with NFC for ages so when I got an SGS4 recently, my chance finally came. I ordered a pack of 10 stickers from Shop4NFC.com in China for $8.99 and they arrived about a week later.
To try them out, I installed the NFC Task Launcher and Tasker Apps. The former enables you to do things like change Wifi settings, volume, display, sync email or even Tweet when it detects a configured NFC sticker. One of the most common use cases is for people to have a sticker at work which puts the phone on mute when you lay the phone on top of it.
Tasker is a general automation app that is not reliant on NFC and can do a ton of different complex things based on context and actions. The reason I installed it is that it can do more complex stuff with Bluetooth than NFC Task Launcher.
I was really disappointed to discover that Bluetooth on the SGS4 is as buggy and glitchy as every other Android phone I’ve owned. Google’s announcement at I/O yesterday that they are going to improve things this year doesn’t convince me. They’ve had 4 years to get it right. The fact that they broke access to Wiimotes etc in 4.2 tells you just how bad they are at Bluetooth.
The place where this causes me the most grief is in the car. All I want is for the bloody phone to auto-connect over A2DP to my Lidl Car stereo when the car is on so I can play my podcasts and listen to music. In theory the SGS4 can do this but simply fails to, 9 times out of 10. HTC’s Android was similarly bad. AOSP/Cyanogen in some versions worked perfectly but it was a crapshoot whether any version would work or not.
So I thought NFC might solve this. Rather than letting Android itself auto-connect, use Tasker to force a connection. And whaddya know, it seems to work. I tap the phone off the sticker after I turn on the ignition and it connects over Bluetooth to the car stereo. It’s only failed once in 10 and I’m pretty sure that’s because I didn’t hold the phone to the sticker long enough.
Actually, that’s really the only downside of NFC that I’ve seen so far. A swipe or momentary tap is not good enough, it requires a very distinct touch and hold for a moment before the tag powers up and sends its ID info to the phone. You can tell by the audio cue on the SGS4 whether it worked or not.
Now I need to find uses for the other 9 stickers. Suggestions?
I love watching ideas bounce back and forward between people on Twitter and see them grow. The best for me recently started when The Verge posted a story about an OpenSource Android App which basically turned a Wii Balance Board into a Withings weighing scales, including RunKeeper and Fitbit integration. I retweeted it and it was picked up by Joe Desbonnet. He realised you could also do some interesting things with the Balance Board and a Raspberry Pi and started hacking. Which of course made me realise I could control the i-racer with it!
Here’s a wee video of it in action:
Apologies for the mobile interference in the audio. I was using my new SGS4 to SSH to the Raspberry Pi to kick off the Python code. The ConnectBot guys really need to fix the microscopic font on screens like the SGS4!
The car isn’t very fast in the video as the Lego added a lot of weight and I hardcoded the speed to 50%. We’ll try again next weekend using a lighter shell, full speed and tarmac instead of cobblelock.
Technical Nitty Gritty (not too Gritty!)
My initial experiments with the Balance Board did not go well. Eventually I discovered that the version of the cwiid Wii library that is available on Raspberry Pi (and Ubuntu etc) is not able to deal with the Balance Board. See this bug. I applied the patch and re-built my own deb files. To use them, just unzip them on the raspberry pi and install like so:
The rest of the time was just spent tweaking the numbers so that I could interpret someone leaning in various directions irrespective of their weight. It’s still not great but works for me, a 7yo and a 6yo.
All the old i-racer Bluetooth code carried over unchanged from our Cheese Controlled Car. Follow the instructions there for installing various Bluetooth bits and bobs on the Raspberry Pi.
I was very surprised that I could talk to both the i-racer and the Balance Board over the same $2 Bluetooth adapter. I was sure I’d end up having to use two adapters.
You can, as ever, grab the code from Github. The only changes you need to make in the code are to set the Bluetooth MAC addresses of both the Balance Board and the i-racer.
Sidenote: The Android App was a dead end. It doesn’t work on Android 4.2 because Google decided to upgrade their broken Bluetooth stack and actually managed to make it worse.
Any questions, leave a comment.
Only if you really need it: Building those cwiid libraries yourself
sudo nano /etc/apt/sources.list
Add the following line:
deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
Then do the following:
mkdir ~/build
cd build
apt-get source python-cwiid
apt-get build-dep python-cwiid
cd cwiid-0.6.00+svn201/
Then edit these three files and add the changes from https://launchpadlibrarian.net/115501163/balanceboard.patch (or run “patch” if you are familiar with that)
After the enormous attention that the Cheese Controlled Car got, my thoughts moved to what else we could use with the i-racer. Two immediately came to mind: the Wiimote and a mousepad. But both went in directions I didn’t expect. I’ll write up the mousepad at a later stage but this post is about the Wii.
The Wiimote is an amazing device. I don’t think the average person realises that it has a VGA infrared camera at the front, has a bunch of accelerometers inside and communicates with the Wii using Bluetooth.
The bit I didn’t know until recently was that it also talks to the Nunchuck using I2C (I squared C, not I two C). This is a brilliant low-level communications protocol invented by Philips many years ago to make it very easy for inter-IC communication using just two wires. I spent a lot of the 1990s writing and debugging I2C drivers.
This made me realise I could dump the full Wiimote and just use a Nunchuck. Both Raspberry Pi and Arduino support I2C and there is tons of code on the web for both platforms.
TL;DR – If you want to jump straight to the outcome, here’s a really rough video of it in action:
Whilst there are adapters out there to get at the Nunchuck’s pins, I decided to just cut the cable and go direct. A lot of our Nunchucks have rusty connectors due to the kids blowing on them to get a “better” connection. So they are basically spittle-filled. Gross I know.
A few minutes of soldering and I had the Arduino happily reading the joystick, buttons and accelerometer of the Nunchuck. But then I sussed that the simple serial Bluetooth dongle I had bought on DX to talk to the i-racer was Slave-mode only (HC-06) whereas I needed the more expensive Master-Slave one (HC-05). I managed to find the right one on Elec Freaks.
Using Raspberry Pi and Python
Whilst I waited for that to arrive, I decided to return to the Raspberry Pi and use that. Its I2C interface is available on some of the external PINs as follows:
Pi pin 1 is 3.3V and connects to the Nunchuck Red Wire
Pi pin 6 is Ground and connects to the White wire
Pi pin 3 is the I2C Data Line and connects to the Green wire
Pi pin 5 is the I2C Clock Line and connects to the Yellow wire
Then you need to do some simple installs on the Raspberry Pi to enable I2C and access it with Python:
Byte Description Values of sample Nunchuk
1 X-axis value of the analog stick Min(Full Left):0x1E / Medium(Center):0x7E / Max(Full Right):0xE1
2 Y-axis value of the analog stick Min(Full Down):0x1D / Medium(Center):0x7B / Max(Full Right):0xDF
3 X-axis acceleration value Min(at 1G):0x48 / Medium(at 1G):0x7D / Max(at 1G):0xB0
4 Y-axis acceleration value Min(at 1G):0x46 / Medium(at 1G):0x7A / Max(at 1G):0xAF
5 Z-axis acceleration value Min(at 1G):0x4A / Medium(at 1G):0x7E / Max(at 1G):0xB1
6 Button state (Bits 0/1) / acceleration LSB Bit 0: "Z"-Button (0 = pressed, 1 = released) / Bit 1: "C" button (0 = pressed, 1 = released) / Bits 2-3: X acceleration LSB / Bits 4-5: Y acceleration LSB / Bits 6-7: Z acceleration LSB
However, despite multiple attempts, I haven’t been able to read reliably from the Nunchuck on the Raspberry Pi. An appreciable percentage of the data comes back as 0xFF. I have no idea if it’s the wiring, the RPi CPU, the I2C driver in Linux, the I2C library in Python, timing or the code above. I don’t believe the problem is the Nunchuck as I now have it working 100% reliably on the Arduino. In any case, the RPi was only ever going to be the proof of concept platform as we obviously need something more portable and less power hungry if we are going to chase a remote control car around the house.
Using Arduino and HC-05
Last week the HC-05 module finally arrived from China and I was able to start playing with it on an itty bitty Arduino Nano yesterday. Using the basic instructions and code from here, after a lot of misunderstanding what pin did what, I was able to connect to the HC-05 and it was able to connect over Bluetooth to the i-racer.
I pulled in the Arduino Nunchuck code from here and converted my Raspberry Pi Python to Arduino Processing (basically a C variant). My code maps the wide range of movement in the Nunchuck joystick to the basic 6 degrees of freedom plus speed on the i-racer. It took quite a few hours of Googling before I finally sussed that “arccos” was the mathematical function I needed. So the code splits 360 degrees into 6 segments of 60 degrees. That gives us Stop/Right Forward/Forward/Left Forward/Left Backwards/Backwards/Right Backwards. Speed ranges from 0 to 15.
It was a real delight to see the i-racer responding with no delay to my thumb movements but the code definitely needs tuning and I’m pretty sure the Nunchuck is not giving a full range of motion. It’s pretty ancient and so hard-left does not give the same value as hard-right for example.
Fionn was thrilled as I’ve been promising him I’d get it working for the past month! Interestingly, his first instinct was to use the accelerometer in the Nunchuck instead of the joystick. This must be how most Wii games work. It shouldn’t be too hard to switch the code over to using the accelerometer but we’ll use this for a while.
After a few more goes, both Fionn and Oisín pointed out that their natural action was to move the joystick in the direction they wanted the car to go, relative to them. I think I’ll leave that for another day
Being able to run the whole thing off 4 rechargeable AA batteries is a real plus. I’m also a huge fan of the Shrimping It project which is a minimalist Arduino setup using little more than the ATMega itself. I have two and the idea is to replace the Arduino Nano with one and make the whole thing even smaller again. The i-racer runs out of juice way faster than the remote ever will so I may be able to try AAA rechargeables or maybe a little Lipo battery.
Next up is probably a left-over Nintendo DS touch-screen as the controller. I also think we’ll start adding “headlights” and an ultrasonic detector for collision avoidance. These will require changes to the i-racer firmware itself so you’ll probably hear from me again in 2017!
When Santa told me that he was getting an i-Racer remote controlled car for Fionn, I was very excited. This is a remote controlled car that, on the surface looks very crude. Heck it doesn’t even come with a remote control.
That’s because it’s Bluetooth-controlled! You can install a simple Android App, pair with the car and control it either with on-screen controls or using the accelerometer in the phone.
As a standalone present it is pretty neat but like all remote controlled toys, I’m sure would be discarded after a few days use. However Santa didn’t want to spend a fortune on a full-blown Arduino-based robot platform or an even bigger fortune on Lego Mindstorms.
It’s when you start thinking about what is possible over time with the i-Racer that things get really interesting. Forgetting electronics for a minute, you could have huge fun making new shells for it out of different materials. Imagine a Foldify template for it so you could make printed paper shells. Lego might be a bit heavy but K’Nex plus some Sugru could work very nicely. Or what about bamboo food skewers or ice-pop sticks? Maybe just some stickers.
Then there is the code running on the car. The source is available so you can play around with it. Now this isn’t the easiest thing in the world to do since the car is not Arduino so you need compilers and special programming dongles, but the option is there.
Taking that one step further, you could clip on other bits of electronics. I have a temp sensor, a PIR detector and an Ultrasonic distance detector all winging their way from China. You could use these to give the car a lot more “intelligence”. There is also someone working on porting the car’s code to Arduino since the microcontroller is compatible.
But that stuff is hard and more long term and wouldn’t actually involved Fionn doing much. Always have to keep your focus on the “customer”! So I thought about it some more and realised that you could have enormous fun with how you control the car. The use of Bluetooth gives you a giant range of pretty *cough* easy possibilities.
So I started playing with some Python code and after a ton of dead-ends, Linux bugs and system problems I was finally able to talk to the car from my Ubuntu PC with a $2 Bluetooth dongle. (Details below)
Then I realised that this would be no use over Christmas as we spend it travelling to family houses around the country without the Ubuntu PC. My Windows laptop should work with tweaks but that’s not for games! So I started looking at the Raspberry Pi. Whatever problems I had run into on my PC were 10x worse on the Pi. (More details below)
After a ton of messing I got keyboard control working the way I wanted over USB on the RPi. Which leads to the piece de resistance – MaKey MaKey control. MaKey MaKey is a beautifully simple idea. It’s an Arduino-compatible board you plug into a PC and it emulates various key-presses and mouse clicks. You then connect whatever you want, as long as it is vaguely conductive, and use that for input. It famously uses bananas, pencil drawings and buckets of water as inputs to various fun software.
The Cheese Controlled Car (CCC)
So Fionn finally had the ultimate car controller. 5 Pieces of Christmas Cheese connected to a Raspberry Pi via MaKey MaKey talking to a car over Bluetooth. Check it out in action:
If you want to try this yourself and run into any problems, pop a comment in below.
Other possibilities include grapes (GCC):
And Barbie (BCC):
Now for the nasty details. Whilst these steps are quite short and the code is very simple, it took a week of evenings cursing, hacking and googling to get everything working right.
Figure out how to connect to car over Bluetooth using Python with PyBluez
Sort out bad bug in Bluetooth on Debian (and therefore Ubuntu)
Realise that most cheap micro Bluetooth dongles from China have duplicate MAC addresses and cannot talk to each other.
Discover that Bluetooth dongle works on USB hub on Raspberry Pi at low-level but Bluetooth stack cannot see it. So connect it to one of USB ports on Raspberry Pi
Discover that cheap Chinese Bluetooth dongles hang the Raspberry Pi after random amount of time
Switch to using older bigger more expensive Bluetooth dongle
Realise that Bluetooth dongle must be plugged in after booting Raspberry Pi in order for it to be detected
Repeatedly curse the totally messed up implementation of USB on the Raspberry Pi
Use a powered USB Hub to connect the MaKey MaKey and anything else like WiFi to the Raspberry Pi
Figure out basic keyboard reading in Python
Figure out reading keyboard in Python without carriage-return
Try PyGame for keyboard reading but discover it needs a screen attached
Figure out reading directly from USB with auto-repeat using Evdev and python-evdev (must be run as sudo)
Now you need to work around a nasty bug in Debian and Ubuntu. They have accidentally compiled in code for Nokia Series 60!
sudo nano /etc/bluetooth/main.conf
add this line to it:
DisablePlugins = pnat
Now plug in the Bluetooth dongle, power up the i-racer and run:
hcitool dev
It should detect your dongle and report its MAC address. If it does, then run:
hcitool scan
This should find the i-racer as a Dagu Car. Note its MAC address and then pair to it by running:
bluez-simple-agent hci0 00:12:05:09:94:26
where you replace 00:12:05:09:94:26 with the MAC address of your car. The PIN is either 1234 or 0000
In a Python shell, (sudo python) run the following to find out your Keyboard or MakeyMakey ID:
from evdev import InputDevice, list_devices
devices = map(InputDevice, list_devices())
for dev in devices:
print( '%-20s %-32s %s' % (dev.fn, dev.name, dev.phys) )
My MaKey MaKey comes up (with KB/Mouse/Joystick disconnected) as:
/dev/input/event0 Unknown USB IO Board usb-bcm2708_usb-1.3.3/input2
The Code
Finally run this code as sudo to control the car using the MaKey MaKey:
import sys
import select
import tty
import termios
import bluetooth
import time
from evdev import InputDevice, categorize, ecodes
if __name__ == '__main__':
dev = InputDevice('/dev/input/event0')
bd_addr = "00:12:05:09:94:26"
port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
key_pressed = str(categorize(event))
if 'KEY_LEFT' in key_pressed:
# 0x5X for left forward. 0x51 very slow. 0x5F fastest
sock.send('\x5A')
if 'KEY_RIGHT' in key_pressed:
# 0x6X for right forward. 0x11 very slow. 0x1F fastest
sock.send('\x6A')
if 'KEY_DOWN' in key_pressed:
# 0x2X for straight backward. 0x21 very slow. 0x2F fastest
sock.send('\x2A')
if 'KEY_UP' in key_pressed:
# 0x1X for straight forward. 0x11 very slow. 0x1F fastest
sock.send('\x1A')
if 'KEY_SPACE' in key_pressed:
#stop
sock.send('\x00')
Note that this is extremely simple control with fixed speed just to prove the idea works. Real code will go up on GitHub when it has much more usable controls with automatic acceleration/deceleration etc and after I make it configurable with any i-Racer and with the MaKey MaKey on any USB port.
What’s next?
Apart from extra sensors and new outer shells, I want to do more playing with controllers. I have an Arduino Joystick shield, Arduino Nano and serial Bluetooth breakout board coming from China. It’d be cool to make some sort of interesting custom controller case with the Arudino controlling the car. I may also get an Arduino Esplora.
The other really obvious one is to stick with the RPi and pair a Wiimote to it too. That could be the ultimate controller for the car.
One downside of the i-Racer is that Arexx, the Chinese manufacturers, have not released the source-code to their Android App, only to the car software itself. Given how trivially simple the control protocol is, it should be very easy for someone to create Open Source equivalents for Android and iOS or even WP8 or BB10. I’m currently poking around inside PhoneGap’s Bluetooth support (this plugin) to see if a simple cross-platform Mobile Hybrid App could be created.
If this is a success, with success being defined as Fionn remaining interested, then I’ll bite the bullet and start getting the parts for one of the good Arduino robot kits out there. A good intermediate step might be the Sparkfun Protosnap Minibot Kit. After that, the sky is the limit:
The lovely people in Mobile Fun in the UK, home of a bajillion mobile and iPad accessories, contacted me recently. I’m a long-time customer of theirs and I’m a huge fan. Their Dash Genie in-car phone holder is a wonderful thing that just works.
They asked if I wanted to review anything on their site and I cheekily requested a pair of Motorola S10-HD headphones. They shot a pair over to me for free immediately.
I’ve had them for a few weeks and they are just superb. Like a fool, I bought a pair of S9′s on eBay a few years ago. I didn’t suss that the only way they could be so cheap was if they were knock-offs. They fell apart in no time. These real S10-HD’s are in a different league altogether.
The design is unusual in that they basically clamp on to your head. Some people with particularly big noggins report that they are uncomfortable but I found after two uses that they fit perfectly.
The reason I wanted them was for running. I hate the normal in-ear wired headphones that fall out constantly and tie you in knots with the wires as you run. With these, I pair via Bluetooth A2DP to my HTC Sensation strapped to my arm and I listen to all my podcasts and music. It has volume and track selection buttons on one side.
However it also works as a handsfree for the phone. If you are listening to music when an incoming call arrives, it mutes it and you can press a button to accept the call. I tested this paired to my laptop and made a few perfect quality Skype calls with it.
The big test was this morning where I ran a sweaty Bandon 10km and listened to Kermode and Mayo’s movie reviews for an hour. I was chuffed that they worked perfectly, never slipped and did not hurt after an hour. I assume the same will be true when I do the Cork Half Marathon in June. Run Keeper is also able to temporarily mute the podcast and tell me my pace every 1km. Nice.
Not much more to say. They work perfectly. They come with a variety of ear-plug sizes and they are great value. I’ll be using nothing else from now on when running.
Thanks again to the Mobile Fun crew. Still the first site I go to when I want to get something for my phone.
UPDATE June 5th 2012: I ran the Cork Half Marathon yesterday with the S10-HD connected to my HTC Sensation. They worked beautifully. No battery issues, no drop-outs and my head wasn’t sore afterwards. Which is more than can be said about the balls of my feet.
Yes I know it’s a stupid thing to want to do but dammit, if it’s technically possible, we should at least try.
One of the most annoying things about the HTC Sensation is how HTC has hamstrung it with a brain-dead Bluetooth stack instead of the one provided by the chipset provider. This means it cannot do the HID profile to support keyboards, mice and Wiimotes. The only reason they do this is to shoehorn the horror that is HTC Sense on top of Android. Sense can’t even do landscape orientation of the home screen FFS!
If you switch to one of the AOSP-based ROMs like Cyanogen, you gain a proper Bluetooth stack but, for the moment, you lose MHL, which means you cannot connect the phone via HDMI to your TV to play movies/games.
But something changed recently with the latest HTC ROMs and I learned that the Android Revolution HD ROM, which I have used many times in the past, now supports both MHL and HID. It is a community variation of the official Sensation ROM so it still has the Sense crap but it seems to be rock solid otherwise and has lots of benefits you miss with the official ROMs.
So this is why I wanted to get both working:
Not my best ever video work, to put it mildly. But you get the idea.
UPDATE: I also started investigating USB OTG on the Sensation this week. This is where you can use the USB interface to plug USB sticks, cameras, hard disks etc into the Sensation rather than the usual reverse. Yet again the hardware is capable of it, the standard drivers support it, but HTC has messed up the implementation.
Why the hell would you create devices with such amazing technical specs and then disable those features due to an obsession with a shitty software skin that offers no added-value to the end-user and which no-one has ever bought a phone for? Don’t get me started on the fact that the CPU in my phone is actually designed for 1.5GHz but ships clocked at 1.2GHz.
Yesterday I finally got both MHL to the TV and Bluetooth to the Wiimote working with a ZX Spectrum Emulator on my HTC Sensation (video coming over the weeked). I was amused by the fact that I was emulating a cheap old computer with an expensive mobile phone.
Then I wondered – How cheap was the Spectrum compared to the Sensation? Some googling for inflation adjusters later and I realised I was completely wrong. It wasn’t cheap at all.
My original 1982 16K Spectrum cost £125 in WH Smith. Adjusted for inflation that’s £377.50 which is €461! Ouch, I really had no idea. An unsubsidised Sensation on Expansys is cheaper at €439.
If my parents hadn’t paid that serious chunk of money back then, who knows where I would have ended up in life…….
UPDATE: All of which makes the Raspberry Pi a staggering bargain and utterly transformational. Seeing someone on their site beg for early samples so he could show them to a African country’s government is just one example of how important RPi is. Far beyond its technical specs or size.
I blogged about this before ages ago but had some recent questions on it so I thought I’d do a quick update. If, like me, you drive an old car, your car stereo probably doesn’t have an audio jack and almost definitely doesn’t have bluetooth.
If you have a Ford from 1999 onwards then you have the added problem of a double height stereo whose CD player is probably knackered at this stage.
It turns out to be quite easy to replace any of the above with a dirt cheap stereo from Lidl or Aldi which gives you A2DP high-quality bluetooth audio through your car speakers when you pair your smartphone to it. As an added bonus you get an acceptable hands-free system built in too.