# Blue Tooth Module Code
# from earl@microcontrollerelectronics.com
#
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
#define INTERVAL 10000
long previousMillis = 0;
char recvChar;
SoftwareSerial blueToothSerial(RxD,TxD);
void setup() {
Serial.begin(9600);
while (!Serial);
delay(2000);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}
void BT_cmd(String cmd) {
char recvChar;
if (cmd != "") {
blueToothSerial.print(cmd + "\r\n");
delay(200);
}
while(blueToothSerial.available()) {
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
}
void loop() {
if(millis() - previousMillis > INTERVAL) {
previousMillis = millis();
BT_cmd("AT+STATE");
BT_cmd("AT+LSP");
}
if(blueToothSerial.available()){
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
void setupBlueToothConnection() {
/*
AT Check if the command terminal work normally
AT+RESET Software reboot
AT+VERSION Get firmware, bluetooth, HCI and LMP version
AT+HELP List all the commands
AT+NAME Get/Set local device name
AT+PIN Get/Set pin code for pairing
AT+BAUD Get/Set baud rate
AT+CLEAR Remove the remembered remote address
AT+LADDR Get local bluetooth address
AT+RNAME Get remote device name
AT+DEFAULT Restore factory default
AT+CMODE Get/Set connection mode
AT+BIND Get/Set bind bluetooth address
AT+COD Get/Set local class of device
AT+IAC Get/Set inquiry access code
AT+ROLE Get/Set master or slave mode
AT+STATE Get current state
AT+SENM Get/Set security and encryption mode
AT+IPSCAN Get/Set page and inquiry scan parameters
AT+SNIFF Get/Set sniff power table parameters
AT+LOWPOWER Start/Stop low power mode
AT+UARTMODE Get/Set uart stop bits and parity
AT+ENABLEIND Enable/Disable Indication print
AT+LSP List Paired Device List
AT+RESETPDL Reset Paired Device List
AT+REMOVEPDL Remove one entry from Paired Device List
AT+SUPERVISION Get/Set supervision timeout
AT+AUTOINQ Start/Stop auto inquiry
AT+INQ Start inquiry
AT+INQC Cancel ongoing inquiry
(M)AT+AUTOCONN Start/Stop auto connection
(M)AT+INQM Get/Set inquiry parameters
(M)AT+CONNECT Connect to a remote device by BD address
*/
blueToothSerial.begin(9600);
delay(1000);
BT_cmd("AT+ENABLEIND1"); //Enable Indications
BT_cmd("AT+ROLE0"); //set the bluetooth to work in slave mode
BT_cmd("AT+NAMEArduinoBTSlave"); //set the bluetooth name
BT_cmd("AT+PIN232323"); //set PIN
BT_cmd("AT+AUTOINQ1"); //Automatic Search
Serial.println("\r\nThe slave bluetooth is inquirable!");
}
Recent Comments