I'm hoping there's a simple answer for this. In the ArduinoBT init sketch the setup() code inits the BT module with Serial.println statements, like:
Serial.println("SET BT PAGEMODE 3 2000 1");
I (think) I understand what's going on here--the sketch is sending commands to the BT module. However, the loop() function uses println to write messages to the serial monitor, like this:
Serial.println("ciao");
Sure enough, when I open the serial monitor I see "ciao" over and over.
My confusion is where the output of Serial.println is going. It has to be one of the following:
Serial.println is sending the output to the monitor and the BT module and the BT module is ignoring commands it doesn't recognize.
Something has switched the output to go from the BT module in the setup() to the monitor in loop().
Which is it? If #2, what's causing the switch? Thanks for any help!
Serial.println() goes to the hardware serial port. In the case of a Bluetooth Arduino the hardware serial port goes to the Bluetooth module. After a reset the Bluetooth module comes up in Command mode. There are several events that will switch the module to Data mode (passing data to the client). The main method is the completion of a connection (outgoing CALL event or incoming RING event).
To understand things better you can always find and read the datasheet for the module you are using.
Thanks for the reply John. Let me make sure I understand this.
When the ArduinoBT sketch starts the BT module is in command mode (because there are no connections) and the Serial.println statements in setup() get sent to the BT as iWrap commands. When the loop() function is first run, the "ciao" statements also get sent to the BT module as iWrap commands (because there are still no connections) and they are ignored because they aren't valid commands.
When I open the serial monitor it connects to the BT module which causes its state to change to data mode. In data mode the BT module forwards all the data from the Serial.println statements ("ciao" in this case) to the connected serial monitor.
Is that correct? I assume this means that if I want to write some debug statements to the monitor while simultaneously sending data to the other connected devices that I'll have to use MUX mode to handle multiple connections. As one example, I'd like to execute a LIST command and then write the results to the monitor; I'll have to juggle the monitor output with the LIST command.
If you need to put the Bluetooth module back into command mode you can either use the escape sequence ( delay(1000); send("+++"); delay(1000) is the default) or use one of the status lines (DTR? DSR?). It's in the iWRAP specification. The latter is probably the best bet if you are talking to multiple Bluetooth clients.
To test out the "Command mode till connection" hypothesis you could send a sequence number instead of "ciao". If the first number to arrive is not the first one you send, the others must have gone during Command Mode.
Just as a follow-up I took John's suggestion and printed a counter to Serial.println() in the loop() function. The lines printed are missing the first few counter values so it looks like they are going to the BT module until I open the serial monitor. Just in case someone cares here's the code:
int LED = 13; // select the pin for the LED
int RESET = 7;
void setup() {
pinMode(LED,OUTPUT); // declare the LED's pin as output
pinMode(RESET,OUTPUT); // declare the LED's pin as output
Serial.begin(115200); // connect to the serial port
digitalWrite(RESET, HIGH);
delay(10);
digitalWrite(RESET, LOW);
delay(2000);
}
int count = 0;
void loop () {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
Serial.print("ciao ");
Serial.println(count++);
delay(1000);
}