The keyMote: a simple wireless remote for computers

The keyMote
The keyMote

Note: this post about an electronics project has been sitting as a draft since 2011 so some of its content might appear outdated. Regardless, I’ve been using the device since then so I figured I should commit it to posterity.

To my computer, its simply a USB keyboard, nothing less, but to me its a remote I can use on any platform with no line of sight. I decided to name it the keyMote. Sounds a bit odd to my ears but its a fitting name.

Here is how it works. There are two parts to this system, the remote, which is battery powered, and the base, which is hooked up to a computer. The remote is a simple keypad (In the case of the prototype, its a numeric keypad, but really, it could be any interface) with a transmitter hooked up to it. The base, the other end, is a receiver with USB Human interface device functionality, in other words, a vanilla USB keyboard. When a button is pressed, the remote sends the identifier of that button to the base which then looks up in a table the keystrokes this identifier is mapped to and sends those to the computer via USB. What button is mapped to what keystroke is entirely configurable using a serial terminal interface (shell) to the base. It can be anything, Ctrl-C, Alt-Shift-F, PageUp, etc. Getting it to interface with your program is then simply a matter of configuring keyboard shortcuts.

The use case

Here is the actual use case that spawned the idea. I was doing some plumbing work in my kitchen with the computer playing songs in the background. It then hit one in the playlist that I especially hate (but keep for nostalgia), but I was in no position to change it as it would have implied bending out of underneath my sink, walking to my computer and then hitting Ctrl + Shift + Right to skip to the next tune. I use Linux and at that time, the media buttons on my keyboard were not operational, I knew very few remotes would work on my platform and finally, I had no line of sight with the computer, so infrared was out of the question. Agreed, a wireless keyboard would have done the trick, but those tend to be limited in range, and well, they are still keyboards, so you have to do the key combinations yourself and are somewhat cumbersome to lug around. So I thought: Why not get something that will emulate my hands typing the keyboard shortcuts?

I looked around for possible off-the-shelf solutions, but none of them were well supported under Linux; basically they all required drivers and were proprietary and as we all know, anything with a driver is dependent on company support an will get dumped sooner of later. The keyboard idea was starting to get some traction. It was the obvious medium to go with, the USB human interface device standard has been around for a decade or so and will likely stick for a few more but more importantly, it is understood by most OSes. Failing to come across anything that would suit my purpose, I set out to develop one from scratch and after a bit of work (or a lot, depending where you come from), I had what I wanted: a simple, rugged self-contained system with no drivers attached, plenty of battery life and a way to make it type whatever I want. Currently, its mapped to my MP3 player, sound controls, Video player controls and some programming IDE functions. It could very well launch applications, toggle the visibility of certain windows (via the window manager), start scripts; it can do anything a keyboard can do.

The system

The main hardware components are an arduino, a wireless receiver and transmitter pair and another microcontroller hooked up to a battery in the remote. I started off with trying out wireless communication on the arduino IDE but soon converted to C/C++ for the added flexibility once I was past figuring out the libraries.

Base

The base software is mainly an interface between two libraries, VirtualWire and V-USB along with a shell to configure the mapping between remote key codes and key strokes. VirtualWire is a library that makes exchanging bits (a lot harder than the datasheet implies, what you send on TX is quite what you get on RX) between vanilla wireless interfaces very straightforward. V-USB is a marvelous implementation of the USB protocol on the AVR using nothing but bit-banging. It’s quite limited in capacity but it’s extremely lean and makes USB accessible for anyone with the patience to understand the protocol. While the actual serial shell is a fair chunk of code, it is needed only for configuration, so this project is mainly composed of glue code and basic electronics. The arduino’s USB connector is used for serial shell functionality, where using commands it is possible to consult statistics about the system, get debugging data, but more importantly map key Ids to key strokes. The other USB connector is the actual keyboard.

The base schematic
The base schematic

Since I had an arduino to spare, I decided to implement the USB and wireless interfaces as a shield rather than build a specific circuit. Really, the only hurdle was the weird spacing between pin 7 and 8 (why?), but that got solved by soldering angled headers to the side of the perfboard at an offset with the holes they would normally fit in. The wireless receiver was a breeze to connect but the USB interface was slightly more complex, with a couple of zener diodes and resistors to get the correct line levels.

Two versions of the base were created but they only differ in the way they look.

Remote

The base is mostly software but the remote is mostly electronics. The code is essentially a loop that intercepts keypad presses and sends them out through the transmitter to the base. Since it’s battery powered, the complexity of the firmware lays essentially in the power management code as the electronics cannot be left on running at full power all the time.

Remote prototype schematic
Remote prototype schematic

I built two different versions of the remote. The prototype which used another ATMega328P (same MCU as the arduino) featured an off-the-shelf keypad and a USB connector with a MAX 1555 for charging. The MCU is left in its deepest sleep mode all the time and is only waken up when a key press occurs. As a result, power consumption is in the order of nano amperes which yields a battery life of a couple months under normal usage when hooked up to a 900 mAh LiPo battery.

Remote version 2 schematic
Remote version 2 schematic

The second version goes even further into increasing battery life. The ATMega328P, overkill for this application, is replaced with an ATTiny84 and the keypad interface is hooked up a circuit that turns on the micro controller and the transmitter from a fully off state only when a key is pressed. It’s using the same battery as the prototype but the power usage is so minute that after close to two years I have not yet exhausted the battery and measuring the drain is beyond my multimeter’s range. Granted, usage has been on and off due to me leaving for extended periods of time but still, that’s a pretty decent statisctic. So much so that I had plans to convert it to a solar cell and supercapacitor design but sadly, all my prototyping efforts led to failure.

The result

It was quickly put together and yielded a result of fairly high quality and convenience for the effort. I’m not saying there is no room for improvement, but it was refreshing to do a project that was not plagued by unforeseen complexity and feature creep. I’ve been using both remotes for the past two years and have been getting months of battery life for the prototype and for its successor, well, it’s still running after two years. Range wise, I can send commands at a distance of about 10 meters through several walls so it more than sufficient.

Since I have no plans to revisit the project in the near future, I’m publishing the source code and the schematics (made with gEDA) up for download on a GNU GPL license. Take note that some of the third-party libraries use a different license which is specified in their respective folders. Should you have any questions, do not hesitate to reach me through the comments or the contact page.

Vietnam – the end

Like this telephone pole, my mind is a mess.
Like this telephone pole, my mind is a mess.

Not much happened during those two days that I had to kill in Ha Noi before my flight. My hostel, shitty; the city, visited for the most part; the weather, rainy and my overall motivation level, low. I did check out the museum of fine arts, the Temple of Literature and the botanical garden but otherwise, my days were spend wandering aimlessly around the city and working in front of my computer. My thoughts were occupied with the coming med school interviews and planning for a life that would potentially change drastically in the near future. It seemed all the accumulated fatigue of the last couple of months had now all came down on my body and mind, this trip was over.

I had a few interesting encounters but not of them led to friendships that would endure past an hour or two. One of those was at a nearby bar, where for the better part of the night I conversed with a Vietnamese barmaid with the aim of improving her English. Another one was with Antoine, the French motorcycle enthusiast I met back in Sa Pa, but sadly, we only had a few minutes to catch up, he was on his way to the bus. Turns out he did not go to the north, the day after he had left, the seemingly unending flow of trucks got the best of him and he turned around.

Monday night I arrived at the airport quite early to find it already overcrowded with hordes of travelers but I managed to find a quiet spot to sit down and read in the arrivals area. A flight to Tokyo, a couple of hours spent waiting at the airport; across the Pacific to New York, more waiting and then I landed in Burlington, Vermont, where a friend picked me up with my car to drive me to Montreal. It was a long journey, but thankfully I was flying with Japan Airlines and got the exemplary service that is to be expected of anything Japanese plus a pretty nice in-flight entertainment system aboard a recently refurbished plane.

Thoughts on Vietnam

I have not seen all of South-East Asia, but so far I can confidently say that Vietnam is my favorite place of them all, You buy something?, motorbike?, traveling around can a serious pain in the butt on occasions, but everywhere in between, you come across genuinely nice encounters. Then again, you have to realize where the Vietnamese come from as a nation and instantly you become more tolerant towards the constant harassment. Landscapes are sort of the same story, the cities and the countryside can be downright ugly, Vietnam remains a poorer Asian country, but not far out lies absolutely spectacular sights of rice paddies surrounded by mountains and ethnic villages of wooden houses and water buffaloes herds. The still omnipresent communist architecture and propaganda get an honorable mention as well and so does the cultural variety of its people.

It’s exhausting to visit, but it’s well worth the effort, but be quick if you want to go, Vietnam has embraced modernity with both arms and like its neighbors, it’s increasingly capitalizing on tourism of the more destructive type a great deal to boost its economy. For that matter, the country’s principal tourism circuit, already quite limited, is starting to feel bland and denatured. There is still plenty to see and do not very far outside of it, get your own set of wheels and adventure will await around the corner. However, I suspect its only a question of time before the hordes of resort goers get there too.

Motorbike Motorbike in Northern Vietnam – Conclusion

Safe and sound in Ha Noi, its time to reflect back on this exhilarating experience. To put it in short terms, it’s now the benchmark by which I will judge my future travels. I had done other motorcycle adventures in the past, but through this one, it has really dawned on me that this is where I get my adrenaline (and though diving too of course) and truly is one of the best ways to enjoy a country and see it more for what it really is. There will surely be more backpacking in the future for me, but I’ve realized that I’m thoroughly tired of public transportation and hoping from hostel to hostel so I’ll definitely attempt to make my future visits to foreign countries more adventure motorcycling oriented. I never came across any close calls. Yes, I brushed on oncoming vehicles countless times and had to swerve to avoid all sorts of animals and obstacles, but that is what driving is like in Vietnam. Otherwise, I was in control of my machine to whole time. It was not for the inexperienced driver and got quite painful and tiring on occasions, but I thoroughly enjoyed every minute of it. Especially in the Ha Giang province, which presented me some of the most beautiful scenery I had ever seen in my entire life.

The route that I took
The route that I took

The Honda Win 100

Really a 107cc, but that does not matter very much because with compression issues, it had trouble moving its own weight on steep hills so I’ll avoid commenting on power. Otherwise, to make things more simple and visual, here is a breakdown of my appreciation of the bike:

Plusses

  • light
  • easy on gas
  • easy to fix
  • inexpensive
  • handles well (when the frame is straight)
  • fun to drive

Minuses

  • very unreliable (the Chinese versions at least)
  • vibrates like crazy at high speeds
  • treacherous in emergency breaking
  • parts not that widely available
  • very uncomfortable

This being said, if I ever need another ride in Vietnam, I will stay away from the Honda Wins. The vast majority of those that are on the market around hostels are tourist bikes. They have been up and down the country countless times, were treated badly by their owners and have for sure crashed in their past, at least mine had. 99 % of the people I’ve met while traveling have had breakdowns. There is always a garage nearby, but it remains that the bill adds up to quite a bit over time. Plus, the Vietnamese rarely do a good job at fixing things, they’ll simply mend it so it will run fine for a couple more hundred kilometers and will break again but by this time, you’ll be too far to come back to complain about a repair poorly done. While the Vietnamese themselves used to rely on them a lot and still do so up north, they are moving away towards semi-automatic Honda Dreams, which are much much more reliable, perform better and are more comfortable. They might not be as fun to drive, but they are the workhorses of this country and can take quite a severe beating. Parts will also be readily available even at the smallest most remote garage out there. So if I do come back in this region to bike through Laos and Cambodia, I’ll get a Honda Dream.

Motorbike Motorbike in Northern Vietnam Day 9 – Mộc Châu to Hà Nội

Weather: Cloudy then Overcast
Departure: 11h00
Arrival: 18h30
Date: 19/04/14


View Larger Map

Not sure if I wanted to go straight to Ha Noi or make another stop along the way, I still woke up pretty early to have breakfast and wish good bye to my buddies of yesterday night. Then, I went back to my room to pack my things, but tiredness got the best of me and I slept for another two hours. As soon as I got up though, I was on the road in no time and flying at a good pace through hilly terrain but on very uneven pavement.

The view not long after Moc Chau
The view not long after Moc Chau
Fixing my motorcycle
Fixing my motorcycle

On a break to buy a can of Coca-Cola, I realized the bumps had disjointed my breather tube from my carburetor again and on passing a ridge and noticing that the weather on the other side was a thick humid fog,  I decided to stop for lunch and fix my air box on the same occasion. That sparked the interest of a bus load of Vietnamese tourists taking a break nearby and next thing I knew, I was surrounded by them. Quickly they were gone but soon replaced by another bus of high school teenagers on a field trip. There, some of them took the opportunity to practice English but myself not really being in the mood for that, I packed up quickly and went on my way.

Rapidly, the scenery turned from nice to boring as I was passing through village after village with frequent large patches of construction. Things had started to sound and vibrate pretty weird for my motorcycle for a while now and at the intersection to Ninh Bin, I opted to play it safe and go to Ha Noi instead. I had been pretty lucky so far with breakdowns and did not really want to push my chance much further. Once I hit highway one, I was making good progress again but on seeing karst peaks starting to become more numerous around me, I tried taking a detour to search for a road through them but without success. On the way back my chain – which I knew was very loose already – disengaged. It was an easy fix but fearful of potentially worst things were coming my way, I needed not to waste any more time.

From that point on, I was going as fast as I could to Ha Noi. As urban density increased, so did traffic and into the capital, I was greeted by thick rush hour circulation. Driving amid a sea of scooters and cars while it’s raining and at night is quite a sport and will put even the most season motorcyclist’s skill to the test. It’s like being an outcast in a school of fish or a flock of bird. Everyone moves in concert, in one self-organizing group, there really are no rules to bind you, but as a westerner accustomed to a very strict road code, it requires intense concentration and some extremely good defensive driving skills.

Highway to Ha Noi
Highway to Ha Noi

My rental bike returned, I came across my friends from the other night, who it so happens had also just arrived. Why so? That same Norwegian guy who had had his bike fixed yesterday had ran out of fuel and though the panic had lost his only key somewhere on the road and try as he might, he could not find it. Result: he had to get to a mechanic to wire start his bike. I checked in what is possibly the worst hostel I’ve been in so far on this trip and we all went for a burger.

That’s it. I came out alive. Traffic in Ha Noi is so insane I might still die in a motorcycle related accident, but I will not be the one driving…

Motorbike Motorbike in Northern Vietnam Day 8 – Điện Biên Phủ to Mộc Châu

Weather: Sunny
Departure: 11h00
Arrival: 18h30
Date: 18/04/14

Not unhappy to leave Dien Bien Phu, I woke up late anyway due to a couple hours spent awake in my bed with a mind that would not shut down. After a coffee and an apple, I set out on the road. The city lays in a heart shaped valley, but was surrounded by mountains and as soon as I hit them, I was back in motorcycle heaven with great views and windy roads. Bus and truck traffic was denser than before due to increasing proximity to Ha Noi but otherwise nothing really to complain about.

Road to Moc Chau
Road to Moc Chau

Around the middle of the afternoon, I hit a densely populated plain on which circulating no longer was fun so I pushed the handle down to get to my destination as fast as I could in spite of my motorcycle now having a definitive engine compression problem. At one point I passed a four foreigner pack taking a break at a road side stall and figuring they must have come from the capital, one kilometer further I decided to turn around and go ask them if they new a nice place to spend the night in the direction I was going in.

About to go
About to go

They were actually coming from Sa Pa and were going to Moc Chau, my intended stop for the night and were nice enough to let me join their crew formed of two Norwegians, a Spanish and a Dutch. Great, I had not really tried the convoy thing so far and had been alone riding since the beginning so I decided to tag along. With five though, the logistics become more complex, everyone wants to go at a different speed, not every bike is capable of the same performances ans worst, the chances of a breakdown are dramatically increased. Unsurprisingly one Norwegian was having issues with his engine and had to take it slow. His bike, a proper lemon, had had his transmission swapped two days ago and was still acting up. Not long afterwards, the Dutchman’s luggage rack broke and sent his backpack trailing behind him on the road.

Eventually, we arrived in Moc Chau and checked in a three star hotel one dude had scouted for us. We were all quite stunned by him picking such a fancy place, but the prices were okay, the breakfast included and we managed to negotiate a couple of free beers with the rooms. While the rest of the group was at the mechanic to get their bikes checked, me and Sjef, the Dutch shared a discussion around our well deserved brews. Afterwards, we linked up with the others and went out into town for supper at a proper Vietnamese restaurant, which provided us with a nice feast and many drinks – some courtesy of the District Director – at a very reasonable price.

Tomorrow, the Spanish and the Norwegians are heading to Ha Long bay while Sjef is leaving first thing in the morning for Ha Noi to have his bike serviced before his journey to the south of the country. As for myself, I’ve got one day to spare so I might take the long way to the capital or find another destination in between.