Eric's Place
Welcome to the DEEP WEB. No... wait... still the regular web.

Let Bluetooth Guide My Bike (Part 1: Overview)

Sometimes I ride somewhere new on my bicycle, and like most people, can get a bit lost along the way (ok, maybe more often than most). I came across something pretty neat called the SmartHalo, which is a complete bicycle navigation solution designed to make sure you or your bike never get lost. Unfortunately it costs over $200 (CAD), and I’m pretty sure my entire bike isn’t even worth that much. Let’s try making our own stripped-down version for say … $10?

Pointing the Way

So how do we get there from here? The SmartHalo has, in addition to the traditional turn-by-turn navigation system, a clever “compass mode” where it acts as a compass whose needle points to where you want to go instead of north. Seems simple (and free) enough. A speed readout and a way of knowing how much distance remains would be nice too. We want:

  • An arrow pointing to where we want to go based on where we’re currently facing
  • A speed readout
  • A distance remaining readout

So why not just clamp my phone to the handlebars and call it a day? Well sometimes it rains so it should be water-resistant, too.

Getting the Data

Just because I don’t want to clamp my phone to the handlebars doesn’t mean I can’t use my phone at all! A modern smartphone is a complete navigation solution with a compass and GPS. Therefore, we basically just need a standalone display that can have data fed to it from the smartphone. Bluetooth Low Energy (BLE) is a commonly used technology for energy efficient, short-range wireless data transmission. Specifically, we can use the Generic Attribute Profile, or GATT, a simple client-server protocol that will allow us to exchange data between our “viewer” and the smartphone. Now we just need to find an embedded device that can act as the GATT client. Our requirements are:

  • LCD display
  • Supports Bluetooth Low Energy (BLE)
  • A rechargeable battery (duh)
  • That’s all, folks!

Embed This

About 7-10 years ago, when I was still a fresh-faced student, I would have started researching BLE, LCD, and battery charger modules I could integrate with a hobbyist-friendly microcontroller like the AVR. Then, datasheets in hand, I would write some embedded C code to command the microcontroller to orchestrate these modules, bending them to my whim. All of those components would be soldered to a homemade single-sided PCB, lovingly hand-crafted with some toner-transfer paper, a clothes iron, and some ferric chloride.

But times have changed, and that fresh-faced student with a garage workshop at his disposal is now an impatient professional living in a tiny condo. Luckily, the magnanimous folks behind the Arudino feel my pain; They have spent years making embedded systems accessible for impatient people who have no time to research what the UCSR1A register does, and demand results right meow. So I did a quick scouring of the web for a cheap arduino-compatible module that supports an LCD and Bluetooth Low Energy, and AliExpress did not disappoint:

LILYGO ESP32 Module
Work Smarter, Not Harder

Behold! The ESP32-powered LILYGO dev board for the incredible price of $7! And you wouldn’t believe it, but it has a single-cell LiPo battery charger module too! It’s like they knew I was coming. Or it’s pretty common to need an embedded computer with a display, wireless connectivity and battery power. Nah, definitely clairvoyance on their part.

The LILYGO sports the ESP32 microcontroller from Espressif Systems: A beast of a microcontroller with a 32-bit dual-core processor and 520 KiB of RAM, 4MB or FLASH, and of course built-in Bluetooth and WiFi. In fact I’d argue it straddles the line between microcontroller and microprocessor, as it has an memory management unit and someone managed to boot Linux on it.

I mentioned earlier using Arduino, but although I still love having the Arduino libraries to abstract away the nastiness of configuring embedded peripherals, I quickly noticed the Arduino IDE is still stuck in 2007: It has no code completion, barely any syntax highlighting, and no intuitive way to manage a project consisting of more than the one “sketch” file. Luckily, there’s a new contender to fill the gap called PlatformIO: A VSCode plugin that promises to provide a similar professional experience to what one would expect from Atmel Studio or Keil uVision, while also supporting the Arduino libraries and board configurations. It’s also based on CMake, so we get to use an industry-standard build system instead of sketch files. And while we’re modernizing, why not ditch C and use C++? The ESP32 toolchain seems to support most of the C++11 STL and we definitely have resources to spare. Let’s make our life a little easier by having things like std::string and std::vector at our disposal.

So now that our 2020-friendly development environment is set up and we’ve violated all that is holy in the world of low-power embedded development (hey, at least it’s not a Raspberry Pi running Debian Linux), we can get to figuring out how to gather and push the relevant data from a smartphone.