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