Hi everyone,
For my project, I'm trying to automate using AT commands (as in, code for AT mode/communication mode rather than changing hardware)
Everything is working well, until I throw inquiring into the mix!
If I set the device to Inquire mode (AT+INIT, followed by AT+INQM(0,5,9), followed by AT+INQ), and then try using INQC in my code, I get the response "OK," but the device continues inquiring.
Below is a capture of my code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);
#include "Timer.h" //http://github.com/JChristensen/Timer
#define STATE 11 //hc05 state to d11
#define KEY 12 //hc-05 key(soldered) to d12
#define EN 13 //hc-05 EN to pin 13
Timer t;
int dynamicEvent;
boolean SETTINGHC05MODE = false;
int KEY_MODE;
int currentFunctionStep = 0;
String ATResponse;
void setup() {
pinMode(STATE, INPUT);
pinMode(KEY, OUTPUT);
pinMode(EN, OUTPUT);
//Begin Serial
Serial.begin(38400);
while(!Serial){} //wait for serial
Serial.println("Serial ready!");
//Begin BT
BTSerial.begin(38400);
while(!BTSerial){} //Wait for BT serial
Serial.println("Bluetooth ready!");
//Start HC-05 in AT mode:
KEY_MODE = HIGH;
SETTINGHC05MODE= true;
Set_HC05_MODE();
while(SETTINGHC05MODE){t.update();}
//Setup:
delay(1000);
sendAT("AT+INQC");
sendAT("AT+RESET");
sendAT("AT+UART=38400,0,0");
sendAT("AT+STATE?");
sendAT("AT+ROLE=1");
sendAT("AT+NAME=CHILD");
sendAT("AT+PSWD=1234");
sendAT("AT+CMODE=0");
sendAT("AT+RESET");
}
void loop() {
t.update(); }
void sendAT(String Command){
Serial.print("Command:" ); Serial.println(Command);
delay(10);
BTSerial.println(Command);
delay(1000);
if(BTSerial.available()>0){
Serial.print("Response: ");
ATResponse = BTSerial.readStringUntil('OK\r\n');
Serial.println(ATResponse);
Serial.println(" ");
delay(1000);
}
}
void Set_HC05_MODE(){
if(currentFunctionStep==0){ //if we haven't turned EN low yet
Serial.print("Setting HC-05 To ");
if(KEY_MODE==HIGH) { Serial.println("AT mode"); }
else if(KEY_MODE==LOW){ Serial.println("Communication mode"); }
Serial.println("Debugging:");
digitalWrite(EN, LOW); //pull low so we can change modes
Serial.print(" "); Serial.println("EN LOW");
currentFunctionStep++; //don't repeat this
dynamicEvent = t.after(200,Set_HC05_MODE); //let's give it 200ms before we continue
}
else if(currentFunctionStep==1){
digitalWrite(KEY, KEY_MODE); //Change the key pin
Serial.print(" "); Serial.println("KEY HIGH");
currentFunctionStep++;
dynamicEvent = t.after(200,Set_HC05_MODE);
}
else if(currentFunctionStep==2){
digitalWrite(EN, HIGH); //Stop changing modes
Serial.print(" "); Serial.println("EN HIGH");
currentFunctionStep++;
if(KEY_MODE==HIGH){ //aka AT
dynamicEvent = t.after(3000,Set_HC05_MODE); //Let's wait 3 sec to change to AT
}
else if(KEY_MODE==LOW){ //aka comm
dynamicEvent = t.after(5000,Set_HC05_MODE); //Let's wait 5 sec to change to comm... bootup time is longer
}
}
else if(currentFunctionStep==3){ //If we've finished
currentFunctionStep=0; //reset progress
SETTINGHC05MODE=false; //don't follow through again
Serial.print(" "); Serial.println(">>READY");
}
}
Below I will attach two copies of my serial monitor. The first will be running the code without setting the device to inquire mode. The second will be running the code after setting the device in inquire mode (we should cancel inquire using AT+INQC, and then run the normal commands):
(1) Running the code without being in inquiring mode... it works properly!
(2) Running the code while in inquiring mode... it won't leave inquiring mode!
My wiring:
And the "EN" pin on the HC-05 is connected to pin D13 on the Uno.