Go Down

Topic: HC-05 AT+INQC not ending Inquiring mode (Read 1 time) previous topic - next topic

scrimarc

Jul 23, 2018, 08:23 pm Last Edit: Jul 24, 2018, 04:12 pm by scrimarc
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:
Code: [Select]
#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.

Nick_Pyner

I'm afraid I can't comment, as I don't know much about the finer points of AT configs - or even if it is a finer point - but I think there has been a similar thread in the last week.

It is interesting that you have found a method for configuring on the fly despite the button. I now realise that the Key pin is always on hand and the button is merely an alternative. I guess that makes sense, as I think the buttons are pretty well de rigueur these days.

You appear to have left out a ground wire from Uno to breadboard. I think you had better fix it before the anti-Frizing squad finds you.

scrimarc

Thank you for attempting to help and for the correction to fritzing! Fixed

scrimarc

For anybody looking here in the future, I've figured out the fix!

Change the sendAT function to this:
Code: [Select]
void sendAT(String Command){

  Serial.print("Command:" ); Serial.println(Command);
  BTSerial.println(Command);
  delay(1000);
    if(BTSerial.available()>0){
      Serial.print("Response: ");
      ATResponse = BTSerial.readStringUntil('OK\r\n '); //space is necessary to get AT+INQC and AT+STATE? to work
      Serial.println(ATResponse);
      Serial.println(" ");
      //delay(1000);
    }
}


As it turns out, AT+INQC often response with multiple OK's. By adding the space after OK\r\n, we look for the very last OK in that series.
Previously, the OK's read after some of the other commands were actually leftover OK's from AT+INQC.


Hope this helps anybody in the future!

mylifejustmine

Good Day Sir,

I was impressed and tried your code but I couldnt get a response from the sendAT method. Can you give me some insight/s about the problem?

Go Up