Detecting OK from Serial Port -> HC-05

I am trying to send an AT Command each time it ends. So that the command will always be working. This is the code i have written so far. It doesn’t work. Please help me have a look at it. Will really appreciate it

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX

int count=0;
char val;

void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println(“Enter AT commands:”);
BTSerial.begin(38400); // HC-05 default speed in AT command more
Serial.println(“AT”);
BTSerial.println(“AT”);
delay(1000);
Serial.println(“AT+ORGL”);
BTSerial.println(“AT+ORGL”);
delay(1000);
Serial.println(“AT+INIT”);
BTSerial.println(“AT+INIT”);
delay(1000);
Serial.println(“AT+ROLE=1”);
BTSerial.println(“AT+ROLE=1”);
delay(1000);
Serial.println(“AT+INQM=1,10000,48”);
BTSerial.println(“AT+INQM=1,10000,48”);
delay(1000);
BTSerial.println(“AT+INQ”);
delay(1000);
}

void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
{
Serial.write(BTSerial.read());
val = Serial.read();
}

// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
BTSerial.write(Serial.read());
val = Serial.read();
}

if (val == ‘OK’)
{
BTSerial.println(“AT+INQ”);
}

}

  • Serial.read and BTSerial.read will read and remove one character from the input buffer, so you cannot read() multiple times and expect the same character.

  • val should be a char array so that you can store incoming characters

  • The HC-05 will reply “OK\r\n”

  • String comparison is done with function strcmp

Try something like this:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX

int count=0;
char val;

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  Serial.println("AT");
  BTSerial.println("AT");
}

void loop()
{
  if ( BTSerial.available() > 0 ) 
  {
    static char input[64];
    static uint8_t i;
    char c = BTSerial.read();

    if ( c != '\r' && c != '\n' && i < sizeof( input ) - 1 )
      input[i++] = c;
   
    else
    {
      input[i] = '\0';
      i = 0;

      if ( !strcmp( input, "OK" ) )
      {
        Serial.println("Received OK");
      }
    }
  }
}

Also if you changed the bauds setting of the HC-05, then use the actual value instead of 38400.

Please use code tags.

Read this before posting a programming question

How to use this forum

    if (val == 'OK')
char val;
...
    if (val == 'OK')

A single character is not going to hold "OK".

Have a look at the examples in Serial Input Basics. I have been experimenting with an ESP8266 and I use the 2nd example to receive the responses to the AT commands.

Then, when you have received a complete response you can use the function strcmp() to check what you have received. For example

int rchk = strcmp("OK", receivedChars);

You may need to allow for the possibility that the responses are terminated by CRLF rather than LF on its own. The simple solution is to test for CR characters and not save them.

The ESP8266 does not always respond with OK and I have not yet had time to figure out a system that can deal with different responses to different commands.

...R