How to get Arduino to send AT codes automatically

Hello!
I am currently trying to get my arduino-uno to automatically send AT codes to my HM-19 (very similar to HM-10). The thing is with my code it does not show me if my AT Codes are sending. All I am getting is the Serial print. Here is my code.


#include <SoftwareSerial.h>

SoftwareSerial blueToothSerial(10, 11); // RX, TX

char c=' ';
char reply[30];
int pos = 0;
//int cmp[] = "OK+CONN";

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  delay(1000);
  Serial.println("Searching for device");
  blueToothSerial.begin(9600);
  Serial.println(" ");

  delay(1000);
  Serial.println("Lemme set up real quick");
  //this step tells me when the device is attempting to connect;
  blueToothSerial.write("AT+IMME1\r\n");
  Serial.println("Auto connection is off");
  //allows me to know if the command AT+IMME is sent;
  delay(1000);
  blueToothSerial.write("AT+ROLE1\r\n");
  Serial.println("Master Role set");
  //allows me to know if the command AT+ROLE1 is sent, configues device to be master upon startup;
  delay(1000);
  blueToothSerial.write("AT+DISC?");
  Serial.println("Discovery on");
  //lets me know if the device is starting discovery
  delay(1000);
  if (strcmp(reply,"OK+DIS0:E4E112960A56:-050:CarAlarmS")==0);
  {
    Serial.println("Connection found");
    blueToothSerial.write("AT+CONN0");
  }
  
  Serial.println("I think everything is done don't quote me on it though");
  if (blueToothSerial.available())
     {
      Serial.write(blueToothSerial.read());
     }
}
void loop() { // run over and over
   {
    while (blueToothSerial.available())
      {
        c = blueToothSerial.read();
        reply[pos] = c;
        pos++;
      }
      reply[pos] = '\0';
      Serial.print("Reply = "); Serial.println(reply);
    if(strcmp(reply,"OK+CONN")==0)
    {
      Serial.println("1");
      digitalWrite(7,HIGH);
      delay(1000);
      blueToothSerial.print("0");
      Serial.println("0");
      digitalWrite(7,LOW);
      delay(1000);
    }
    else
     {
      while(blueToothSerial.available())
       {
        c = blueToothSerial.read();
        reply[pos] = c;
        pos++;
       }
       reply[pos] = '\0';
       Serial.print("Reply = "); Serial.println(reply);
      Serial.println("TRY AGAIN");
     }
  }
}

I am still pretty confused. I am on the internet searching for all different ways to do this but still cannot arrive at an answer.
Thank you very much!

Why not write a simple function that accepts a string, and echoes the string to both Serial and BT?

I'm sorry, how can I do that?
I am extremely new to programming arduinos, and need a lot of help.
Thank you

void echoAT (const char* ATstring)
{
  blueToothSerial.write(ATstring);
  Serial.write (ATstring);
}
...
...
...
echoAT ("AT+IMME1\r\n");

(Uncompiled, untested)

problem with if() statement - the ; will terminate the if() - remove it

 if (strcmp(reply,"OK+DIS0:E4E112960A56:-050:CarAlarmS")==0);
  {

when you transmit an AT command you should wait for a response and check it it is correct, e.g. a function like

// send a single command to the modem, wait for a response or timeout or ERROR
// return 1 for OK, -1 for timeout, -2 for ERROR
int Command(const char *command, const char *response, int timeout) 
{

That string is longer than 30 character so if your did read the reply and it was equal it means you wrote off the end of your array.

And that ';' at the end of the 'if' statement is the entire contents of the 'if'. Remove it if you want the 'if' to control the next block of statements.

For a test, it might be appropriate to use ".readString()"

#include <SoftwareSerial.h>

SoftwareSerial blueToothSerial(10, 11); // RX, TX

String Reply;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  delay(200);

  pinMode(7, OUTPUT);

  Serial.println("Searching for device");
  blueToothSerial.begin(9600);

  Serial.println("Lemme set up real quick");
  //this step tells me when the device is attempting to connect;
  blueToothSerial.write("AT+IMME1\r\n");
  Reply = blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Auto connection is off");

  //allows me to know if the command AT+IMME is sent;
  delay(1000);
  blueToothSerial.write("AT+ROLE1\r\n");
  Reply = blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Master Role set");

  //allows me to know if the command AT+ROLE1 is sent, configues device to be master upon startup;
  delay(1000);
  blueToothSerial.write("AT+DISC?");
  Reply = blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Discovery on");

  //lets me know if the device is starting discovery
  if (Reply.startsWith("OK+DIS0:E4E112960A56:-050:CarAlarmS"))
  {
    Serial.println("Connection found");
    blueToothSerial.write("AT+CONN0");
    Reply = blueToothSerial.readString();
    Serial.print(Reply);
  }

  Serial.println("I think everything is done don't quote me on it though");
}
void loop()   // run over and over
{
  if (blueToothSerial.available())
  {
    Reply = blueToothSerial.readString();
    Serial.print(Reply);
  }

  if (Reply.startsWith("OK+CONN"))
  {
    Serial.println("1");
    digitalWrite(7, HIGH);
    delay(1000);
    blueToothSerial.print("0");
    Serial.println("0");
    digitalWrite(7, LOW);
    delay(1000);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.