Hello everyone. I have a arduino program that sends at commands over serial to the hc06 Bluetooth module. It works great but after I send and receive data two times its like the serial connection freezes and noting happens. The program is suppost to set a relay to high when it detects my phones mac address in range so I have it continually scanning the area but it freezes after two scans
#include <SoftwareSerial.h>
int unlockTimes =0;
int comFail =0;
int session =0;
#define DEBUG true
#define bt_power 7
#define bt_key_power 8
#define indication_led 13
SoftwareSerial esp8266(10, 11); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
// set the pins to OUTPUT
pinMode(bt_power, OUTPUT);
pinMode(bt_key_power, OUTPUT);
pinMode(indication_led, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
// set the pins to LOW
digitalWrite(bt_power, LOW);
digitalWrite(bt_key_power, LOW);
digitalWrite(indication_led, LOW);
/************************************************
Setting the pins to low is important because
in order for us to get into AT mode the key pin
has to be set to Ground FIRST. Many tutorials out
there fail to mention this important fact and
therefore many people have problems with getting
into the AT mode of the HC-05
************************************************/
// make sure the key has been LOW for a bit
delay(100);
// set the key pin to High
digitalWrite(bt_key_power, HIGH);
// small delay
delay(100);
// now power on the BT
digitalWrite(bt_power, HIGH);
// start our serial so we can send and recieve
// information from the BT module
Serial.begin(9600);
esp8266.begin(38400); // your esp's baud rate might be different
Serial.print("Bluetooth scanner booting, Please wait....\r\n");
sendData("AT+CMODE=0\r\n",2000,DEBUG); // reset module
Serial.print("Setting Role: ");
sendData("AT+ROLE=1\r\n",1000,DEBUG); // configure as access point
Serial.print("Initiating device: ");
sendData("AT+INIT\r\n",1000,DEBUG); // get ip address
// sendData("AT+BIND=C14,20,E1F04E\r\n",1000,DEBUG); // configure for multiple connections
// sendData("AT+PAIR=C14,20,E1F04E,30\r\n",1000,DEBUG); // turn on server on port 80
Serial.print("Bluetooth module initiated successfully. Starting program!\r\n");
}
void loop()
{
String c = "";
c = sendData("AT+RNAME?C14,20,E1F04E\r\n",5000,DEBUG); // get ip address
//Serial.print(c);
if(c == "+RNAME:SCH-I535\r\nOK\r\n")
{
if(unlockTimes == 0)
{
unlock();
}
Serial.print("Security Status: Unlocked\r\n");
Serial.println("Rebooting. . .");
/*delay(100); // Give the computer time to receive the "Rebooting. . ." message, or it won't show up
void (*reboot)(void) = 0; // Creating a function pointer to address 0 then calling it reboots the board.
reboot();
unlockTimes = 1;
*/
}
else
{
if(comFail == 0)
{
lock();
}
Serial.print("Security Status: Locked\r\n");
//Serial.print(c);
unlockTimes = 0;
comFail = 1;
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+5000) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
String unlock()
{
digitalWrite(13, HIGH);
delay(750);
digitalWrite(13, LOW);
}
String lock()
{
digitalWrite(2, HIGH);
delay(750);
digitalWrite(2, LOW);
}
Any help would be greatly appreciated!