Bluetooth RN-42 command mode problem [SOLVED]

Hi,
This is starting to drive me totally crazy :0. I bought an Bluetooth Mate Silver (Sparkfun) to use with my Arduino Duemilanove. I use NewSoftSerial beta 11 to communicate with the RN-42 (- beware the class is called SoftwareSerial in the NewSoftSerial beta 11!). I also use the Arduino (usb) serial port to relay input from the Arduino serial monitor to the RN-42. Also, I have a bluetooth device in my PC so I can connect to the RN-42.

The sketch I’m using makes it possible to send data (in both directions): Arduino serial monitor->Arduino Duemilanove->RN-42->TeraTerm (PC bluetooth terminal program). This works.

Writing $$$ in both TeraTerm and Arduino serial monitor I can make the RN-42 go into command mode. BUT when I do it from the Ardunio serial monitor I only get rubbish echoed back and not CMD as expected! I have spent days trying to figure out what’s wrong but I just can’t get it to work. My mission is to make the sketch change baud rate from 115200 to 9600. Any help appreciated extremly much!

This is my sketch

#include <icrmacros.h>
#include <SoftwareSerial.h>

/* Bluetooth Mate Echo
 by: Jim Lindblom - jim at sparkfun.com
 date: 3/15/11
 license: CC-SA v3.0 - Use this code however you'd like, for any
 purpose. If you happen to find it useful, or make it better, let us know!
 
 This code allows you to send any of the RN-42 commands to the
 Bluetooth Mate via the Arduino Serial monitor. Characters sent
 over USB-Serial to the Arduino are relayed to the Mate, and
 vice-versa.
 
 Here are the connections necessary:
 Bluetooth Mate-----------------Arduino
 CTS-I    (not connected)
 VCC------------------------5V or 3.3V (supplied from pin A0)
 GND--------------------------GND
 TX-O-------------------------D2
 RX-I-------------------------D3
 RTS-O    (not connected)
 
 How to use:
 You can use the serial monitor to send any commands listed in
 the RN-42 Advanced User Manual
 (http://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf)
 to the Bluetooth Mate.
 
 Open up the serial monitor to 9600bps, and make sure the 
 pull-down menu next to the baud rate selection is initially set
 to "No line ending". Now enter the configuration command $$ in 
 the serial monitor and click Send. The Bluetooth mate should
 respond with "CMD".
 
 The RN-42 module expects a newline character after every command.
 So, once you're in command mode, change the "No line ending"
 drop down selection to "Newline". To test, send a simple command.
 For instance, try looking for other bluetooth devices by sending
 the I command. Type I and click Send. The Bluetooth Mate should
 respond with "Inquiry, COD", follwed by any bluetooth devices
 it may have found.
 
 To exit command mode, either connect to another device, or send
 ---.
 
 The newline and no line ending selections are very important! If
 you don't get any response, make sure you've set that menu correctly.
 */

// We'll use the newsoftserial library to communicate with the Mate
//#include <NewSoftSerial.h>  


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

//NewSoftSerial bluetooth(bluetoothTx, bluetoothRx);
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(A0, OUTPUT);    // Use analog pin 0 to power on/off the bluetooth on 
  pinMode(A1, OUTPUT);    // Use analog pin 1 to reset the bluetooth on PIO6.
  Serial.begin(9600);     // Begin the serial monitor at 9600bps
  btReboot();             // Reboot/reset the RN-42 bluetooth to defaults.
}

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


// Switch off, on and reset the bluetooth to factory defaults. Start serial.
void btReboot() {
  int dt = 20;
  digitalWrite(A0, LOW);    // Switch off
  delay(dt);
  digitalWrite(A1, HIGH);    // Set PIO6 high to reset
  delay(dt);
  digitalWrite(A0, HIGH);    // Switch on
  delay(dt);

  digitalWrite(A1, LOW);     // Toggle PIO6 3 times to reset
  digitalWrite(A1, HIGH);
  delay(dt);
  digitalWrite(A1, LOW);
  digitalWrite(A1, HIGH);
  delay(dt);
  digitalWrite(A1, LOW);
  digitalWrite(A1, HIGH);

  Serial.println("Starting...");  
  bluetooth.begin(115200);        // The Bluetooth Mate defaults to 115200bps
  /*
  // Can't get this to work!! :(
  bluetooth.print("$$");         // Enter command mode
  delay(250);                      // Short delay, wait for the Mate to send back CMD
  Serial.println((char)bluetooth.read());  
  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
  */
  
}

Cheers

  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably

So, you want to cut the speed by a factor of 12? Have you verified that 115200 is too fast?

  Serial.println((char)bluetooth.read());

This assumes that the bt device sent back exactly one character, within the 250 milliseconds you waited. Is that expected behavior?

Well, it is said in the NewSoftSerial that 115200 might be to fast if you intend, which I do, to do some other processing as well. But 115200 seems to work just fine when typing in the terminal windows in this example.

Well, I expect below to print CMD 250 milliseconds after $$$ command. This was is more or less taken from an existing example at Sparkfun: http://www.sparkfun.com/tutorial/BluetoothMate-Quickstart/BluetoothMate_Echo.pde.

Serial.println((char)bluetooth.read());

However, to start with, since this line is commented, it doesn't explain why I get all kind of strange characters back to the Arduino serial monitor aftert having typed $$$. Or do you have a clue?

However, to start with, since this line is commented, it doesn't explain why I get all kind of strange characters back to the Arduino serial monitor aftert having typed $$$. Or do you have a clue?

After that function ends, having done nothing but set the baud rate to 115200, loop() is called, to read any data coming from the bluetooth device. The data is then send to the serial port.

If what is displayed in the Serial Monitor isn't intelligible, either the Serial Monitor and Serial instance are not exchanging data properly (mismatched baud rate) or the Arduino and bluetooth device are not (again, mismatched baud rate seems most likely).

Hi, FINALLY! I think I found a solution :D. It seems like it is crucial to intruduce some delays when setting up the Bluetooth Mate and/or the serial communication . Basically, the code below represent the rows you need to change baud rate to 9600.

  bluetooth.begin(115200);        // The Bluetooth Mate defaults to 115200bps
  delay(320);                     // IMPORTANT DELAY! (Minimum ~276ms)
  bluetooth.print("$$");         // Enter command mode
  delay(15);                      // IMPORTANT DELAY! (Minimum ~10ms)
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  bluetooth.begin(9600);          // Start bluetooth serial at 9600

The delay times are based on a sketch implementing an iterative process to try different delays. The result was 276ms and 10ms but I guess it is good to have some margins. A general tips for those having problem; try long delays to start with! Perhaps it can help also for others having similar unexplained problems with the Bluetooth Mate and RN-42.

Phu - I almost pulled my hair off for this one...

Thanks for your efforts anyway PaulS!

Hi PaulS

I’m trying to connect a RN-42 and an arduino. Do we have to connect something at pins A0 and A1? If no,then I didn’t get why u used A0 and A1 in the btReboot() function.Please help me out.Thank you…

Ouch old post. It's explained in the code. Check setup() {} function in first post.