Using the BlueSMiRF

Contributors: jimblom
Favorited Favorite 12

Example Code: Using Command Mode

With a little ingenuity, we can use the Arduino as a medium between us and the Bluetooth Mate/BlueSMiRF to send and receive commands. Here’s a small sketch which relays data between the Arduino Serial Monitor and a Bluetooth modem.

  Example Bluetooth Serial Passthrough Sketch
 by: Jim Lindblom
 SparkFun Electronics
 date: February 26, 2013
 license: Public domain

 This example sketch converts an RN-42 bluetooth module to
 communicate at 9600 bps (from 115200), and passes any serial
 data between Serial Monitor and bluetooth module.
#include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600

void loop()
  if(bluetooth.available())  // If the bluetooth sent any characters
    // Send any characters the bluetooth prints to the serial monitor
  if(Serial.available())  // If stuff was typed in the serial monitor
    // Send any characters the Serial monitor prints to the bluetooth
  // and loop forever and ever!

This sketch makes use of the SoftwareSerial library, which should be included with most of the recent versions of Arduino.

At the beginning of the sketch, the Arduino enters the command mode string and temporarily changes the Bluetooth modem's baud rate to 9600 bps (using the U,9600,N command). Remember this is temporary, so when power is cycled, the modem will default back to 115200 bps.

The loop of the sketch simply checks to see if either the Bluetooth modem or the Serial Monitor have sent any data to the Arduino. If so, it'll relay the data sent from one device to the other.

Using the Passthrough Sketch

With the code uploaded, and everything hooked up accordingly, open up the Serial Monitor. Make sure the baud rate is set to 9600. Throughout this process you'll have to fudge around with the dropdown menu to the left of the baud rate selection. It should initially be set to “No line ending”.

First, let's enter command mode by typing $$$, and click “Send”. You should see the Bluetooth modem respond with CMD, and you'll notice the red Stat LED blinking much faster, this all indicates that the device is in command mode.

Once you're in command mode, you'll need to change the line ending dropdown to “Newline”. The basis of all this is that the RN-42 module expects a newline character after every command except for the command mode string. Annoying, but we'll deal.

Using GET Commands

The GET commands are a good place to start using command mode, they'll display settings, status, or other information that might be helpful. Try sending the "Display Basic Settings" command by typing “D”, and pressing “Send”. This will trigger a response from the Bluetooth modem that details, among other things, the baud rate settings, the name, and the address (BTA) of the device. The address is something you should take note of, it can either be identified from this command, or by taking a gander at the module's label, next to the “MAC NO”. Each Bluetooth moule has a unique address which can't be changed. Try sending the other get commands, and see what information you can retrieve from the modem.

Displaying Settings from Command Mode

Using SET Commands

After sending the “D” command, you may have noticed your Bluetooth modem has it's own name, in addition to the address. Unlike the address, this name can be changed to whatever you'd like. By default it'll be RN42-XXXX, where XXXX is the last four digits of the address. Let's give a SET command a whirl. The SN,<name> command is used to set the name, where \<name> is any collection of up to 20 characters. Think up a unique name, and assign it to your device. After sending the SN command, the modem should respond with an “AOK”. Now if you send the D command, you should see your new name listed next to the “BTName” setting.

Using the SET name command

Be careful with the SET commands, only change something if you're sure it won't negatively affect the modem, or your ability to communicate with it. If you change something you don't think you should have, send the SF, 1 command to reset everything back to its factory default value. Another handy command, if you're lazy like me, is ST,255, which turns the config timer off. This enables the remote configuration forever. Remember that any setting you modify will be saved to the Bluetooth modem's memory, and will be retained upon loss of power.

ACTION Commands

Finally, it's time for some action. Among other things the Bluetooth modem's ACTION commands can be used to find other Bluetooth devices, connect to them, and disconnect from them.

Begin by sending the inquiry scan command -- I,<value> -- to search for other Bluetooth modules in range. The \<value> parameter defines the number of seconds the modem will take to look for other modules. It'll default to 10 if not defined. If you just type “I” and click send, the device should respond with a “Inquiry, COD=0”, and then after ten seconds it'll respond with any Bluetooth modules it found. It will print their information as "BT address, BT name, COD" (COD is class of device).

Connecting via command mode

If the modem finds any modules, you can try sending the connect command -- C,<address> -- to connect to one of them. The modem in the example above found two devices in range, by sending the C,000666421B01 command, we can attempt to connect to one of them.

After sending the connect command, the device will respond with “TRYING”, which will be followed by either “CONNECT failed” (the meaning of which should be pretty apparent) or the connection will be successful! After a successful connection we immediately enter data mode, and the modem becomes a pipeline. Any characters sent from one Bluetooth device will be sent to the other, and vice-versa. To disconnect, you'll need to re-enter command mode (don't forget to set to “No new line”), and send the “K,” command (with Newline selected, bleh).

There are a lot of other commands to explore! Thumb through the User's Manual and familiarize yourself with all of the power at your Bluetooth modems's fingertips!