Last active
April 15, 2023 19:38
-
-
Save HerrRiebmann/a0fe2e360512e332c78b435bfe802fe7 to your computer and use it in GitHub Desktop.
Get and change HC-0X bluetooth module baudrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#define BT_Baud 57600 | |
#define BT_BaudFallback 9600 | |
#define BT_Cmd_Delay 2000 //1000 is okay, until HC-06-20190901 | |
SoftwareSerial BT(8, 7); | |
long baud = 0; | |
uint8_t btVersion = 0xFF; | |
void setup() { | |
Serial.begin(115200); | |
InitializeBluetooth(); | |
} | |
void loop() { | |
bool received; | |
while (BT.available()) { | |
Serial.write(BT.read()); | |
received = true; | |
} | |
if (received) { | |
received = false; | |
Serial.println(); | |
} | |
while (Serial.available()) | |
{ | |
char c = Serial.read(); | |
BT.write(c); | |
} | |
} | |
void InitializeBluetooth() { | |
GetBtVersion(); | |
//Uninitialized | |
if (btVersion == 0xFF) | |
FindBtModule(); | |
//Clear outgoing buffer | |
BT.flush(); | |
//Clear incoming buffer | |
BtDataAvailable(); | |
} | |
bool FindBtModule() { | |
Serial.println(F("BT Init")); | |
delay(BT_Cmd_Delay); | |
//First try with CarriageReturn/Linefeed. Otherwise the buffer might be filled with previous value | |
btVersion = 3; | |
baud = BtFindCurrentBaud(); | |
//Change Version to use delay(1000) without linebreak | |
if (baud == 0L) { | |
btVersion = 0; | |
baud = BtFindCurrentBaud(); | |
} | |
//Nothing found | |
if (baud == 0L) { | |
//Reset Version to uninitialized | |
btVersion = 0xff; | |
baud = BT_BaudFallback; | |
//Try with default value | |
BT.begin(baud); | |
Serial.println(F("BaudNotFound")); | |
return false; | |
} | |
//Get Version to use lineend or delay | |
if (BtGetVersion()) | |
SetBtVersion(); | |
//Clear incoming Buffer | |
BtDataAvailable(); | |
//Baud not 57000 | |
if (baud != BT_Baud) | |
if (!ChangeBtBaud()) { | |
Serial.println(F("BaudNotChanged")); | |
//Stay with found baud or default value | |
if (baud == 0L) | |
baud = BT_BaudFallback; | |
BT.begin(baud); | |
return false; | |
} | |
baud = BT_Baud; | |
BT.begin(baud); | |
return true; | |
} | |
long BtFindCurrentBaud() { | |
static const long rates[] = { 4800, 9600, 19200, 38400, 57600, 115200 }; | |
uint8_t numRates = sizeof(rates) / sizeof(long); //6 | |
//start with highest baud, to not buffer crap into HC-06 | |
//for (int rn = 0; rn < numRates; rn++) { | |
for (int rn = numRates - 1; rn >= 0 ; rn--) { | |
BT.begin(rates[rn]); | |
BtDataAvailable(); | |
delay(200); | |
PrintBtAt(); | |
BT.flush(); | |
if (btVersion >= 3) | |
delay(100); | |
else | |
delay(BT_Cmd_Delay); //No Linefeed, but 1Sec idle | |
if (BtDataAvailable()) | |
return rates[rn]; | |
} | |
return 0L; | |
} | |
// AT+BAUD<X> -> OK<X> | |
// X=4 : 9600bps (Default) | |
// X=6 : 38400bps | |
// X=7 : 57600bps | |
// X=8 : 115200bps | |
bool ChangeBtBaud() { | |
Serial.println(F("BT ChangeBaud")); | |
if (btVersion < 3) | |
BT.print(F("AT+BAUD7")); | |
if (btVersion == 3) | |
BT.println(F("AT+UART=57600,0,0")); | |
if (btVersion > 3) | |
BT.println(F("AT+BAUD7")); | |
delay(BT_Cmd_Delay); | |
return BtDataAvailable(); | |
} | |
void PrintBtAt() { | |
if (btVersion >= 3) | |
BT.println(F("AT")); | |
else | |
BT.print(F("AT")); | |
} | |
bool BtDataAvailable() { | |
if (BT.available()) { | |
//Read what´s on the buffer | |
while (BT.available()) | |
BT.read(); | |
return true; | |
} | |
return false; | |
} | |
bool BtGetVersion() { | |
if (btVersion != 0xFF) | |
return BtGetVersion(btVersion >= 3); | |
if (BtGetVersion(true)) | |
return true; | |
delay(BT_Cmd_Delay); | |
return BtGetVersion(false); | |
} | |
bool BtGetVersion(bool linebreak) { | |
BT.print(F("AT+VERSION")); | |
if (linebreak) { | |
BT.println(); | |
delay(200); | |
} | |
else | |
delay(BT_Cmd_Delay); | |
char input[40]; | |
uint8_t j = 0; | |
while (BT.available()) { | |
if (j < sizeof(input)) | |
input[j++] = BT.read(); | |
else | |
BT.read(); | |
//0 HC-06-20190901 | |
//1 ZS-040 (DX_smartv2.0) | |
//2 FC-114 (hc01.comV2) | |
//3 1744 (VERSION:3.0-20170609) | |
//4 ZS-40 (Firmware V3.0.6,Bluetooth V4.0 LE) | |
//4 BT05 (+VERSION=Firmware V3.0.6,Bluetooth V4.0 LE) | |
if (input[0] == 'O' && input[1] == 'K') { | |
btVersion = 1; | |
return true; | |
} | |
if (input[0] == 'D' && input[1] == 'X') { | |
btVersion = 1; | |
return true; | |
} | |
if (input[8] == 'V' && input[9] == '2') { | |
btVersion = 2; | |
return true; | |
} | |
if (input[0] == 'H' && input[1] == 'C') { | |
btVersion = 0; | |
return true; | |
} | |
if (input[8] == '3' && input[10] == '0') { | |
btVersion = 3; | |
return true; | |
} | |
if (input[10] == '3' && input[27] == '4') { | |
btVersion = 4; | |
return true; | |
} | |
if (input[19] == '3' && input[36] == '4') { | |
btVersion = 4; | |
return true; | |
} | |
} | |
if (j > sizeof(input) - 1) | |
j = sizeof(input) - 1; | |
input[j] = '\0'; | |
Serial.println(F("VersionNotFound")); | |
return false; | |
} | |
void GetBtVersion() { | |
//ToDo: get version from EEPROM | |
} | |
void SetBtVersion() { | |
//ToDo: store version to EEPROM | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment