Simple Bluetooth Connection

Hello Community,

i have an Arduino BT-V06 and i want to establish a simple bluetooth connection to a specific device.

This is my code:

const int buttonPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;

int buttonState = 0;
char inByte = 0;
char incomingString[200];
int counter = 0;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
  Serial.println("SET CONTROL ESCAPE 43 00 1");
  Serial.println("SET BT PAGEMODE 4 FFFF 1");
}

char getByte() {
  while (Serial.available() == 0) {
  }
  return Serial.read();
}

void connectBlue() {
  digitalWrite(ledPin2, HIGH);
  switchMode();
  Serial.println("CALL 00:a0:96:26:37:10 1 RFCOMM");
  switchMode();
  digitalWrite(ledPin2, LOW);
}

void switchMode() {
  delay(1100);
  Serial.print("+++");
  delay(1100);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) {        
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
      
    if( Serial.available() ) {
      inByte = getByte();
      if (inByte == '?') {
          connectBlue();
          Serial.println('R');
      }
    else if (inByte == 'N') {
      digitalWrite(ledPin3, HIGH);
      delay(2000);
      digitalWrite(ledPin3, LOW);
    }
    else if (inByte == 'O') {
      digitalWrite(ledPin3, HIGH);
      delay(2000);
      digitalWrite(ledPin3, LOW);
    }
    else { 
        incomingString[counter] = inByte;
      }    
    }
  } 
    else if (buttonState == LOW) {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
    }
}

Explanation:

I have a Switch-Button with which i can switch between two status. In Status 1 i can open the Serial monitor and if i send a ? the Board should try to connect to the given address. After connecting i want to send R. The device will return a N and if i receive the N a LED will blink.

This does not work! The slave device has a LED that symbolizes if the connection is alive or not. The LED is always off. Can anyone help me please?

Crhiss

It looks like you are trying to use Serial to talk to the Serial Monitor AND the bluetooth device. No can do. Only one device can be on the other end of a serial connection - either the PC or the bluetooth device.

But only at the beginning to initiate the communication. After i type '?' the connection between PC and Arduino is closed and the board is calling the sensor. Or is the connection not automatically closed?

After i type '?' the connection between PC and Arduino is closed and the board is calling the sensor.

I don't see anywhere in your code where you are closing the serial connection to the PC.

I don't see where you have a servo or other mechanism that unplugs wires and plugs new ones in.

I don't see where you got the idea that typing something in the Serial Monitor would cause the Arduino to disconnect from the serial port.

What this is leading up to is that you can't use the hardware serial port to communicate with two serial devices, unless you have more than one hardware serial port, like the Mega does.

If you don't, you'll need to use NewSoftSerial to create a software serial port to talk to one of the devices, and connect that device to the correct set of pins.

Ok i thought the connection would close, because after i typed '?' once, the board did not react on a second entry.

So now i changed the code, so that i don't need a connection to the pc before connecting to the device.

const int buttonPin = 2;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;

int buttonState = 0;
char inByte = 0;
char incomingString[200];
int counter = 0;
int _setup = 0;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
  Serial.println("SET CONTROL ESCAPE 43 00 1");
  Serial.println("SET BT PAGEMODE 4 FFFF 1");
}

char getByte() {
  while (Serial.available() == 0) {
  }
  return Serial.read();
}

void connectBlue() {
  digitalWrite(ledPin2, HIGH);
  switchMode();
  Serial.println("CALL 00:a0:96:26:37:10 1 RFCOMM");
  switchMode();
  digitalWrite(ledPin2, LOW);
}

void switchMode() {
  delay(1100);
  Serial.print("+++");
  delay(1100);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) {        
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    
    if (_setup == 0) {
      connectBlue();
      _setup = 1;
    }
    else {}
      
    if( Serial.available() ) {
      inByte = getByte();
      if (inByte == '?') {
          //connectBlue();
          Serial.println('R');
      }
      else if (inByte == 'N') {
        digitalWrite(ledPin3, HIGH);
        delay(2000);
        digitalWrite(ledPin3, LOW);
      }
      else if (inByte == 'O') {
        digitalWrite(ledPin3, HIGH);
        delay(2000);
        digitalWrite(ledPin3, LOW);
      }
      else { 
        incomingString[counter] = inByte;
      }    
    }
  } 
    else if (buttonState == LOW) {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
    }
}

I can't get this to work either.

Another question, how can i use NewSoftSerial with the Bluetooth Module? I thought the Bluetooth Module is only connected with pins 0 and 1 for serial communication. How can i make other pins eligible for bluetooth communication and how can i connect a bluetooth device to a specific pair of pins?

Greetings Crhiss

You need to break this problem down into manageable pieces.

Can you get the Arduino to connect to the bluetooth device? Reading serial data and blinking LEDs is extraneous until you can talk to the bluetooth device.

If your bluetooth device connects to the Arduino using the TX/RX pins, then the serial connection to the PC is interfering with the bluetooth connection, and you will never get the bluetooth connection to work.