0

I am having trouble reading data from the bluetooth serial interface on the Arduino.

Here my code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 7); // RX, TX

void setup() {
  mySerial.begin(38400);   
  Serial.begin(38400);
}
String Data = "";
void loop(){
  //mySerial.println("Bluetooth Out");
  
  while(mySerial.available()){
    char character = mySerial.read();
    Data.concat(character);
    if (character == '\n'){
        Serial.print("Received: ");
        Serial.println(Data);
        Data = "";
    }
  }
}

This should take whatever I enter into the bluetooth serial and print it on the regular serial port. Here is what I've tried:

If I uncomment mySerial.println("Bluetooth Out"); then I see the message bring printed out to the bluetooth terminal.

I tried similar to above to print to normal serial out and I see it being printed.

I've tried multiple ways (from some tutorials online) to decode the string and the data coming from mySerial, but nothing is happening.

I am using arduino Serial Monitor to check the ports, however I also tried to use the bluetooth terminal on my laptop and same behavior.

So I guess, what is the proper way for me to read data from the bluetooth serial port?

nick_v1
  • 1,654
  • 1
  • 18
  • 29
  • 1
    Are you sure that the data you are sending ends in a null character? Try making that something different like a '!' and then put one of those at the end of your message – Delta_G Jul 11 '20 at 00:08
  • Thanks, I am not sure what happened, but it started working. I suspect it was something with me deploying updated code and not fully re-establishing a new bluetooth connection. I've made a few modifications and can see BlueTooth serial input now. However it is still not consistent, sometimes I see gibberish. ⸮⸮⸮⸮ ⸮⸮⸮⸮+Ŭ⸮B⸮⸮⸮⸮,⸮0 01:17:49.874 -> 01:17:49.874 -> 01:17:49.874 -> 01:17:49.874 -> Received: 0 01:17:49.874 -> – nick_v1 Jul 11 '20 at 06:19
  • Ok, I am now able to get it to work consistently. Here is what I have to do, I push new code to Arduino, open the serial monitor, change the baud to 9600, type some command into the bluetooth terminal and see jibberish show up in the arduino serial monitor. Then I change the baud to 38400 and then I see the logging I expect. I am not sure why this works like this or what I did to deserve this punishment, but at least it works now. – nick_v1 Jul 11 '20 at 06:46
  • @Delta_G Why would nul character matter? He is using the String class. Or do you mean a new line character? – gre_gor Jul 11 '20 at 11:50
  • Because of that if statement. He only prints if he gets a \n. So if there’s no \n then there’s no print. Sorry yes I meant new line. – Delta_G Jul 11 '20 at 13:40

0 Answers0