Communication with bluetooth without serial

i want to communication between bluetooth in fully automatic not manual.

i think bluetooth use SoftwareSerial so i think i can do that only char array

in easy way, i was try like this.

  #include <SoftwareSerial.h>

  #define BT_RX 10
  #define BT_TX 11
  #define STATE 12

  SoftwareSerial HM10(BT_RX,BT_TX);
  
  void setup() {
  Serial.begin(9600);
  HM10.begin(9600);
  pinMode(STATE, INPUT);
}

  void loop() {
  char c[4] = {0x41, 0x54, "\n", "\r"};
  // OR
  char* c = "AT\n\r";
  int i = 0;
  while(i < 4)
  {
  HM10.write(c[i]);
  i++;
  }

  if (HM10.available()) {
    Serial.write(HM10.read());
  }
  
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
}

and when i try this, i predict that Serial will return "OK" but it won't.
Serial Monitor condition is "no line ending" because i send NL and CR.

what is wrong in my code?

What happens with this simpler version

void loop() {

  if (HM10.available()) {
    Serial.write(HM10.read());
  }
  
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
}

if you set the line ending to BOTH and send AT from the Serial Monitor?

...R

Also I would have thought blasting FOUR characters at the HM10 each and every time through loop., but at most reading only one coming back from the module, would be a recipe for disaster.

Try sending AT less often. e.g. every 1 second.
Then listen for a response.

Robin2:
What happens with this simpler version

void loop() {

if (HM10.available()) {
    Serial.write(HM10.read());
  }
 
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
}



if you set the line ending to BOTH and send AT from the Serial Monitor?

...R

you mean "Both NL & CR"? then it work currectly. but i want to send it in my code, without manual input.

pcbbc:
Also I would have thought blasting FOUR characters at the HM10 each and every time through loop., but at most reading only one coming back from the module, would be a recipe for disaster.

Try sending AT less often. e.g. every 1 second.
Then listen for a response.

you mean like this?

 #include <SoftwareSerial.h>
  #include "src\imLibrary\Relay.h"

  #define BT_RX 10
  #define BT_TX 11
  #define STATE 12

  SoftwareSerial HM10(BT_RX,BT_TX);  // RX핀(7번)은 HM10의 TX에 연결
                                   // TX핀(8번)은 HM10의 RX에 연결

  bool once = false;
  long mtime = 0;
  
  void setup() {
  Serial.begin(9600);
  HM10.begin(9600);
  pinMode(STATE, INPUT);
  }

  void loop() {
  if(millis() - mtime > 1000)
  {
    char* c = "AT\n\r";
    int i = 0;
    while(i < 4)
    {
      HM10.write(c[i]);
      i++;
    }
    
    mtime = millis();
  }

  if (HM10.available()) {
    Serial.write(HM10.read());
  }
  
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
  }

but it won't reply "OK". it just nothing reply anything.

sinsin63:
you mean "Both NL & CR"? then it work currectly. but i want to send it in my code, without manual input.

I know that. But it is best to develop a program in small steps testing at each stage.

Now try this, which emulates what you were doing when sending the data from the Serial Monitor

void loop() {
  static unsigned long lastMessageTime = 0;

  if (millis() - lastMessageTime >= 3000) { // send once every 3 seconds
    lastMessageTime = millis();
    HM10.println("AT");
  }
  
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
}

...R

Robin2:
I know that. But it is best to develop a program in small steps testing at each stage.

Now try this, which emulates what you were doing when sending the data from the Serial Monitor

void loop() {

static unsigned long lastMessageTime = 0;

if (millis() - lastMessageTime >= 3000) { // send once every 3 seconds
    lastMessageTime = millis();
    HM10.println("AT");
  }
 
  if (Serial.available()) {
    char c = Serial.read();
    HM10.write(c);
  }
}




...R

ok, small step at each time. thank you for your advise!

i found it. i'm searching HM10 datasheet, and they said line feed first like this.

    HM10.write("at\r\n");

and it returns right.

thank you!

sinsin63:
i found it. i'm searching HM10 datasheet, and they said line feed first like this.

    HM10.write("at\r\n");

and it returns right.

Do you mean that HM10.write("at\r\n"); works and HM10.println("at"); does not?

...R

HM10.println("at"); didn't work. but HM10.println("at\r"); is works!

Thanks for the update.

But what you say seems to be at odds with the line of code in your Reply #6 HM10.write("at\r\n"); - very curious.

...R

i think HM10.println include '\n' but HM10.write didn't

sinsin63:
i think HM10.println include '\n' but HM10.write didn't

I know that. But what's strange about your Replies #6 and #8 is the varying order of the \r\n

...R