Bluetooth connection, nothing received

Hi,

I’m trying to get my bluetooth connection working. I made a class called bluetooth for setting up the bluetooth connection and sending a String. The connection uses a Software Serial. Here’s the code:

Arduino Sketch:

#include <SoftwareSerial.h>
#include "bluetooth.h"

SoftwareSerial blue_ss(2,3);    //Rx_Pin = 2 Tx_Pin = 3
BLUETOOTH bt(&blue_ss);

void setup() {
  Serial.begin(9600);          //Serial Port via USB connection
}

void loop() {
  Serial.println("test");      //send via Hardware Serial Port (USB connection)
  bt.senden("test");           //Send via Software Serial Port (Bluetooth)
  delay(500);
}

bluetooth.h:

#ifndef BLUETOOTH_H
#define BLUETOOTH_H

#include <Arduino.h>
#include <SoftwareSerial.h>

class BLUETOOTH
{   
  private:
  SoftwareSerial *bt_serial; 

  public:
  BLUETOOTH(SoftwareSerial *ss);
  ~BLUETOOTH();
  void senden(String);
};
#endif

bluetooth.cpp:

#include "bluetooth.h"

BLUETOOTH::BLUETOOTH(SoftwareSerial *_ss)  //Constructor, setting up the connection
{
    bt_serial = _ss;
    
    this->bt_serial->begin(115200);        // The Bluetooth Mate defaults to 115200bps
    this->bt_serial->print("$");           // Print three times individually
    this->bt_serial->print("$");
    this->bt_serial->print("$");           // Enter command mode
    delay(100);                            // Short delay, wait for the Mate to send back CMD
    this->bt_serial->println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
    this->bt_serial->begin(9600);          // Start bluetooth serial at 9600

}

BLUETOOTH::~BLUETOOTH(){}                //Destructor

void BLUETOOTH::senden(String s)          //send String
  {
       this->bt_serial->print(s);
  }

Additional to the bluetooth connection i made a serial connection over USB for testing.

I can compile the whole code without errors. My Problem now is, that I don’t receive anything over the bluetooth connection or the serial connection via usb. I think that maybe the loop in the sketch isn’t executed correctly. I tested the bluetooth connection in one sketch without header and source files and it worked. So maybe I made a mistake with the files I include?!

Hope that someone can help me.

Thank you,
pat_

Your class needs a begin() method just like SoftwareSerial has. You need to call that method in setup(), just like you would call begin() for the SoftwareSerial instance. You do NOT call the begin() method for the SoftwareSerial instance in your constructor. The hardware is not ready yet.

Thank you Paul. That was the mistake. Now it works :wink:

So here is the code now (if someone will have the same problem):

Arduino sketch:

#include <SoftwareSerial.h>
#include "bluetooth.h"

SoftwareSerial blue_ss(3,2);    //Rx_Pin = 3 Tx_Pin = 2
BLUETOOTH bt(&blue_ss);

void setup() {
  Serial.begin(9600);          //Serial Port via USB connection
  bt.start();
}

void loop() {
  Serial.println("testUSB");      //send via Hardware Serial Port (USB connection)
  bt.senden("testBT");           //Send via Software Serial Port (Bluetooth)
  delay(500);
}

bluetooth.h:

#ifndef BLUETOOTH_H
#define BLUETOOTH_H

#include <Arduino.h>
#include <SoftwareSerial.h>

class BLUETOOTH
{   
  private:
  SoftwareSerial *bt_serial; 

  public:
  BLUETOOTH(SoftwareSerial *ss);
  ~BLUETOOTH();
  void start();
  void senden(String);
};
#endif

bluetooth.cpp:

#include "bluetooth.h"

BLUETOOTH::BLUETOOTH(SoftwareSerial *_ss)  //Constructor
{
    bt_serial = _ss;
}

BLUETOOTH::~BLUETOOTH(){}  //Destructor

void BLUETOOTH::start()
{
    this->bt_serial->begin(115200);        // The Bluetooth Mate defaults to 115200bps
    this->bt_serial->print("$");           // Print three times individually
    this->bt_serial->print("$");
    this->bt_serial->print("$");           // Enter command mode
    delay(100);                            // Short delay, wait for the Mate to send back CMD
    this->bt_serial->println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
    this->bt_serial->begin(9600);          // Start bluetooth serial at 9600
  }

void BLUETOOTH::senden(String s)          //send String
  {
       this->bt_serial->print(s);
  }

So one question for understanding. Why did my old code not work? The stuff i do in the start() method now was in the constructor before. I thougt that the constructor will be executed once when i create the bluetooth object in the main sketch.

I was doing a lot of java in the past and there I usually put all the init stuff in the constructor. So this is different in arduino?