Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get and change HC-0X bluetooth module baudrate
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)
//Firmware V3.0.6,Bluetooth V4.0 LE - CR + LF AT+ROLE0 (Slave) 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;
}
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.