Last active
August 29, 2015 14:00
-
-
Save mathieulesniak/11267515 to your computer and use it in GitHub Desktop.
Set a bluetooth connection with Arduino
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> | |
// Pins | |
#define BT_PIN_TX 7 | |
#define BT_PIN_RX 6 | |
#define BT_PIN_KEY 5 | |
#define BT_CMD_RETRIES 3 | |
boolean btErrorFlag = true; | |
SoftwareSerial blueToothSerial(BT_PIN_RX, BT_PIN_TX); | |
void setupBlueToothConnection() | |
{ | |
btErrorFlag = false; //set bluetooth error flag to false | |
enterATMode(); // enter HC-05 AT mode | |
delay(250); | |
sendATCommand("RESET"); // send to HC-05 RESET | |
delay(1000); | |
sendATCommand("ORGL"); //send ORGL, reset to original properties | |
sendATCommand("ROLE=1"); //send ROLE=1, set role to master | |
sendATCommand("CMODE=0"); //send CMODE=0, set connection mode to specific address | |
sendATCommand("BIND=0D,18,01"); //send BIND=??, bind HC-05 to OBD bluetooth address | |
sendATCommand("INIT"); //send INIT, cant connect without this cmd | |
delay(200); | |
sendATCommand("PAIR=0D,18,01,20"); //send PAIR, pair with OBD address | |
delay(200); | |
sendATCommand("LINK=0D,18,01"); //send LINK, link with OBD address | |
delay(200); | |
enterComMode(); //enter HC-05 comunication mode | |
delay(250); | |
} | |
//----------------------------------------------------------// | |
//--------Enter HC-05 bluetooth moduel command mode---------// | |
//-------------set HC-05 mode pin to LOW--------------------// | |
void enterComMode() | |
{ | |
blueToothSerial.flush(); | |
delay(500); | |
digitalWrite(BT_PIN_KEY, LOW); | |
delay(500); | |
blueToothSerial.begin(38400); //default communication baud rate of HC-05 is 38400 | |
} | |
//----------------------------------------------------------// | |
//----------Enter HC-05 bluetooth moduel AT mode------------// | |
//-------------set HC-05 mode pin to HIGH--------------------// | |
void enterATMode() | |
{ | |
blueToothSerial.flush(); | |
delay(500); | |
digitalWrite(BT_PIN_KEY, HIGH); | |
delay(500); | |
blueToothSerial.begin(38400); // HC-05 AT mode baud rate is 38400 | |
} | |
//----------------------------------------------------------// | |
void sendATCommand(char *command) | |
{ | |
char recvChar; | |
char str[2]; | |
int i,retries; | |
boolean OK_flag; | |
if (!btErrorFlag) { //if no bluetooth connection error | |
retries = 0; | |
OK_flag = false; | |
while (retries < BT_CMD_RETRIES && !OK_flag) { //while not OK and bluetooth cmd retries not reached | |
blueToothSerial.print("AT+"); //sent AT cmd to HC-05 | |
blueToothSerial.print(command); | |
blueToothSerial.print("\r\n"); | |
while (blueToothSerial.available() <= 0); //wait while no data | |
i = 0; | |
while (blueToothSerial.available() > 0){ // while data is available | |
recvChar = blueToothSerial.read(); //read data from HC-05 | |
if (i < 2) { | |
str[i] = recvChar; //put received char to str | |
i++; | |
} | |
} | |
retries++; //increase retries | |
if ((str[0]=='O') && (str[1]=='K')) { | |
OK_flag = true; //if response is OK then OK-flag set to true | |
} | |
if (!OK_flag) { | |
delay(500); | |
} | |
} | |
if (retries >= BT_CMD_RETRIES) { //if bluetooth retries reached | |
btErrorFlag = true; //set bluetooth error flag to true | |
} | |
} | |
} | |
void setup() { | |
// Bluetooth pins | |
pinMode(BT_PIN_RX, INPUT); | |
pinMode(BT_PIN_TX, OUTPUT); | |
pinMode(BT_PIN_KEY, OUTPUT); | |
setupBlueToothConnection(); | |
} | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment