hm-10 send and receive data and checking received value

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey all i am wondering what is wrong with my code below. I have 2 issues.

 

First issue I am checking if c equals "hi" and if it does then display said hi. I currently see the output of c when it hits the Serial.println(c); but never see the said hi even though Serial.println(c); does say "said hi".

 

Second issue is that when i startup the arduino i am sending the AT command AT using bluetooth.println("AT"); // just a check but all it seems to do is display AT on the phone instead of OK as it should.

 

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(8,9); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("PC Side Connected");
  bluetooth.begin(9600);
  while (!bluetooth) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  bluetooth.println("Bluetooth Side Connected");
  bluetooth.println("AT"); // just a check
}

void loop() { // run over and over
  String c;

  if (bluetooth.available()) {
    //What is sent back from the BT module
    c = bluetooth.readStringUntil('\n');
    delay(10);
    Serial.println(c);

        if (c.equals('hi')) {
          Serial.println("said hi");
        }
  }

  if (Serial.available()) {
    //Send back data to Bluetooth module
    bluetooth.write(Serial.read());
  }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Serial comm seems to be a lost art, you have to pay attention to details.

Such as

bluetooth.println("AT"); // just a check

probably sends AT<cr><lf>, while the modem was expecting only AT<cr>  as the println adds /r/n to the quoted string!

Try

bluetooth.print("AT/r"); // just a check

as .print does not add any control characters to the output.

 

In a similar vain the received string may also contain additional characters, both leading and trailing, making the .equals. not match,

You will need to parse the received input to remove any extra white space and control characters before searching for a match!

 

Jim

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Serial comm seems to be a lost art, you have to pay attention to details.

Such as

bluetooth.println("AT"); // just a check

probably sends AT<cr><lf>, while the modem was expecting only AT<cr>  as the println adds /r/n to the quoted string!

Try

bluetooth.print("AT/r"); // just a check

as .print does not add any control characters to the output.

 

In a similar vain the received string may also contain additional characters, both leading and trailing, making the .equals. not match,

You will need to parse the received input to remove any extra white space and control characters before searching for a match!

 

Jim

 

 

 

 

Hey jim thanks for the info.

 

I did a little test and this is what i came up with:

 

 

Notice the debug says "hi" yet it never does hit the IF "hi" which it should also say "it works!" under the debug! hi one. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What type is comString? Usually you'd compare strings with strcmp() not ==

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
the modem was expecting only AT<cr> 

+1

 

Quote:
Try

bluetooth.print("AT/r"); // just a check

You mean

bluetooth.print("AT\r"); // just a check

ie, a back-slash

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
What type is comString?

elsewhere, (s)he's using stuff like

  String c;

and

  if ( c.equals('hi') ) {