Threephase

Creative and professional things by Chris Peplin

Configuring Bluetooth modules with AT-commander

09 July 2013

As a part of the OpenXC platform, I have released the AT-commander C library to help programatically configure Bluetooth devices like the Roving Networks RN-42 (i.e. SparkFun's BlueSMiRF).

The RN-42 based BlueSmirf from SparkFun is a great development board for prototyping Bluetooth applications, and it’s fairly easy to configure. It uses an “AT” command set (here’s the entire command set for the RN-42), which are simple, short ASCII commands you can send over the Bluetooth or the UART side of the module. The basic procedure is to put the device into “command mode” with $$$, after which point you can use the various AT commands.

SparkFun BlueSMiRF

(Image is copyright SparkFun under CC BY-NC-SA 3.0)

Other devices use similarly styled AT commands, like Xbee radios (here’s the AT command set for the Xbee radios from SparkFun). The concept is similar, but the individual commands are different as is the special characters to get into command mode (on the Xbee it’s +++).

Xbee Radio

(Image is copyright SparkFun under CC BY-NC-SA 3.0)

As I mentioned, using AT commands is very straightforward…but it’s a huge pain if you have to do it very often. It’s also a major hurdle for someone not familiar with command line terminals, UART, or Bluetooth SPP tools. My experience building a vehicle interface for the OpenXC platform led me to create a C library - the AT-commander - that can take the annoyance out of configuring AT-compatible devices.

Introducing AT-commander

The AT-commander is a C library to control a device via UART that responds to an AT command set (like an RN-42). It’s especially designed for use on an embedded platform like an Arduino, chipKIT, Mbed or Blueboard, but is designed with a flexible API so it can be used on any platform.

Let’s just right to the code. Here’s an example of how to configure the baud rate of an attached AT-compatible device:

AtCommanderConfig config;
config.platform = AT_PLATFORM_RN42;
config.write_function = write_byte
config.read_function = read_byte
config.delay_function = delay;

// Set the baud to 115200, if it's not already correct
bool baud_set = at_commander_set_baud(&config, 115200);

You must pass a few simple functions to the library that can read and write bytes to your UART interface, and everything else is handled by the library. All of the at_commander_* functions automatically detect the current baud rate in order to enable command mode, taking away one of the biggest headaches with configuring an AT device.

Code

The library is hosted at GitHub, where there are additional docs and example programs for Arduino and any LPC17xx based development board. All of the code is released under the BSD license, courtesy Ford Motor Company.

The library currently supports entering and exiting command mode, setting the UART baud rate, changing the device name and soft rebooting. To see it in action, check out the OpenXC vehicle interface firmware’s Bluetooth initialization code.