HC-0X Bluetooth Version auto detection

Hi,

I´ve created a custom board, which was enhanced by a bluetooth HC-05 module.
Due to different suppliers, I received many varying versions of it.

No big deal, if they´d act all the same, but they don´t. Some expect a carriage return & linefeed, others doesn´t. Some need a " = " between the AT-command and the value, some not.

My goal was to solder a board, attach the bluetooth-module and it should configure itself to 57600 baud automatically on the first start.
Then I can store the version into the EEPROM and act correspond to it.

#include <SoftwareSerial.h>
uint8_t wiVersion = 0;  //0 not Set
                        //2 FC-114 (hc01.comV2)
                        //3 1744 (VERSION:3.0-20170609)
                        //4 ZS-40 (Firmware V3.0.6,Bluetooth V4.0 LE) -> CR + LF AT+ROLE0 (Slave) Commands without " = "Commands without " = "
#define WIBAUD 57600
SoftwareSerial WI(8, 7);

bool WiInitialization() {
  unsigned long baud = WiFindCurrentBaud();
  //Change Version to use Linefeed & Carriage Return
  if (baud == 0) {
    if (wiVersion == 0)
      wiVersion = 3;
    else
      wiVersion = 0;
    baud = WiFindCurrentBaud();
  }
  if (baud == 0) {
    //Error
    return false;
  }
  if (baud != WIBAUD)
    if (ChangeWiBaud())
      WI.begin(WIBAUD);
    else {
      if (baud != 0)
        WI.begin(baud);
      return false;
    }
  return true;
}

unsigned long WiFindCurrentBaud() {
  static const unsigned long rates[] = {4800, 9600, 19200, 38400, 57600, 115200};
  uint8_t numRates = sizeof(rates) / sizeof(unsigned long); //6
  for (int rn = 0; rn < numRates; rn++) {
    WI.begin(rates[rn]);
    WI.setTimeout(100);
    WI.flush();
    if (wiVersion >= 3) {
      WI.println("AT");
      delay(100);
    }
    else {
      WI.print("AT");
      delay(1000);
    }
    if (WI.available()) {
      //Read what´s still on the buffer
      while (WI.available())
        WI.read();
      return rates[rn];
    }
  }
  return 0;
}

bool WiGetVersion() {
  for (uint8_t i = 0; i < 2; i++) {
    switch (i) {
      case 0:
        WI.println("AT+VERSION");
        break;
      case 1:
        WI.print("AT+VERSION");
        break;
    }
    delay(1000);
    char input[30];
    uint8_t j = 0;
    while (WI.available()) {
      if (j < sizeof(input))
        input[j++] = WI.read();
      else
        WI.read();
      //1
      //2 FC-114 (hc01.comV2)
      //3 1744 (VERSION:3.0-20170609)
      //4 ZS-40 (Firmware V3.0.6,Bluetooth V4.0 LE)
      if (input[0] == 'O' && input[1] == 'K') {
        wiVersion = 1;
        return true;
      }
      if (input[8] == 'V' && input[9] == '2') {
        wiVersion = 2;
        return true;
      }
      if (input[8] == '3' && input[10] == '0') {
        wiVersion = 3;
        return true;
      }
      if (input[10] == '3' && input[27] == '4') {
        wiVersion = 4;
        return true;
      }
    }
  }
  return false;
}

// AT+BAUD<X> -> OK<X>
// X=4 : 9600bps (Default)
// X=6 : 38400bps
// X=7 : 57600bps
// X=8 : 115200bps
bool ChangeWiBaud() {
  if (wiVersion != 3)
    WI.print("AT+BAUD7");
  else
    WI.println("AT+UART=57600,0,0");
  delay(1000);
  if (WI.available()) {
    //Read what´s still on the buffer
    while (WI.available())
      WI.read();
    return true;
  }
  else
    return false;
}

Not the cleanest code in the world, but it does what I need.

Maybe someone got further responses from "AT+VERSION", which I can enhance :slight_smile:

It would appear that you want to ensure that Bluetooth will communicate at 57600, and therefore you need to either configure it to run at that rate, or ensure that it is already configured to run at that rate - probably just the former. This implies that you need to put HC-05 into AT mode. To do that you need to need to wire it and power it in the proper manner, and then send the commands at 38400. No other rate will do. There is no suggestion you are doing this, and a quite strong suggestion that you are not.

The above is little more than a guess. The code is incomprehensible junk from beginning to end. About the only thing clear is that you specifically refer to the HC-05, but I wouldn't be surprised if that is wrong too. The BLE modules, probably HM-10s, may be installed on a ZS-040 board, but they are not HC-05s, they are actually BLE modules, as stated, and are in AT mode by default, i.e. not the same as HC-05. It has just occurred to me that you might be thinking that HC-05s are in AT mode by default, which is not the case.

At a guess, you may find you can solve your problem by ditching the HC-05s and using HC-06s instead. They have the advantage of being in AT mode by default, are configured at 9600, no other rate will do, and AT+BAUD(7) should work every time.

Uhm... I guess you don´t use a HC-05 very often?
It starts in AT-mode all the time, until a device connects via bluetooth.
As long as you do the check on startup, it will work. Except you can connect a device in less then a second after powering up the arduino...

It checks the current configured baud, expecting to have a device which does not require a line ending.
If that fails, it tries again, with a line ending.
After that, the current baud is checked against the desired baud. If it does not fit, it will be changed.

That works and I tested it several dozen times. With a variety of different HC-05´s.

Due to your amount of posts, I´d expect you´d have test things before trying to destroy something.
But you never stop learning :wink:

TriB:
Uhm... I guess you don´t use a HC-05 very often?
It starts in AT-mode all the time, until a device connects via bluetooth.
..................
I tested it several dozen times. With a variety of different HC-05´s

Well it looks like I must defer to your wide experience, great depth of knowledge, and rigorous test procedures. I guess I didn't know all that because I just read the data sheets and I make sure I know what I have in my hand, but you never stop learning, and the nature of your post does not surprise me one little bit.

TriB:
Uhm... I guess you don´t use a HC-05 very often?
It starts in AT-mode all the time, until a device connects via bluetooth.

I guess you don't use an HC-05 very often...

HC-05 Datasheet: http://www.electronicaestudio.com/docs/istd016A.pdf (Page 5)

Or:

Alternate Datasheet (Also Page 5)