Communication over USB works fine, Bluetooth is a different story...

Hey guys,

I’ve been a coder for 10 years. My code is simple and should be spot on.

Issue:

I am communicating with the Arduino via Bluetooth and USB. USB works fine. It’s only when I try and receive information from the Arduino via Bluetooth that things get buggy/odd.

I wrote an application in Visual Studio that connects with the Arduino and then sends it commands in the form of strings and the Arduino receives the strings and replies with strings.

My Visual Studio project isn’t the issue.

Arduino Code:

char character;
 String request = String("");    
 String response = String("");
 void setup() {
   Serial.begin(9600);
 }
 void loop() {
    while(Serial.available() > 0) {
      character = Serial.read();
      request.concat(character);  
     }
     if (request != "") {
     if (request == "GET_ID") {
     Serial.print("137");
     delay(50);
     request = "";
     }      
     if (request == "GET_AT") {
     Serial.print("<at>76.0 F<at/>");
     request = "";
     }      
     if (request == "GET_RT") {
     Serial.print("<at>68.0 F<at/>");
     request = "";
     }   
     }
   }

The first time I send a command this is what happens:
I should receive “137”.
The Arduino sends “13”.

The second time I click send:
The Arduino sends “71”

The third time I click send:

The Arduino sends “37”

So after three clicks I have “137137”
Like I said before, USB works fine. With USB I only have to click it once and I’ll have “137”

So USB works fine and my Visual Studio app is sending commands fine.

Thanks in advance for any help you might be able to give me.
-Jeb

Jebtrillion: I’ve been a coder for 10 years. My code is simple and should be spot on.

In another 10 years you will have learned not to make rash statements :)

I don't see any sign of Bluetooth stuff in the code you posted.

Also, please post your code in code tags as explained in the How to use the forum sticky. Code should look like this.

...R

Thanks for your reply.

I don’t believe that was a rash statement at all. I said; “It should be spot on”. Meaning, there is a chance greater than zero that it is not spot on. I only meant that I have a decent amount of experience with code and that this issue has stumped me. I usually figure out bugs before I need to seek help.

Bluetooth is serial communication, so as long as you utilize the TX and RX ports you’re golden.

So here is the “Bluetooth” code:

Serial.begin(9600);

and

Serial.print("137");

Jebtrillion:
should be spot on.

but appears to be quite some way short of that. I could not get it to work at first, and needed constant resetting.

It now works fine. I send strings over the phone and the responses comes up on the phone and the monitor. I don’t know what I did to fix it. As far as “Bluetooth stuff” is concerned, you clearly have all you need.

char character;
 String request = String("");    
 String response = String("");
 void setup() {
   Serial.begin(9600);
 }
 void loop() {
    while(Serial.available() > 0) {
      character = Serial.read();
      request.concat(character);  
     }
     if (request != "") {
     if (request == "id") {
     Serial.println("137");
     delay(50);
     request = "";
     }      
     if (request == "at") {
     Serial.println("<at>76.0 F<at/>");
     request = "";
     }      
     if (request == "rt") {
     Serial.println("<at>68.0 F<at/>");
     request = "";
     }   
     }
}

Yeah it's really odd and doesn't make a lick of sense. I have that reset issue as well. I hope someone can help me. Since you had that issue as well at least I know its not my BT unit.

Jebtrillion: I don't believe that was a rash statement at all. I said; "It should be spot on". Meaning, there is a chance greater than zero that it is not spot on. I only meant that I have a decent amount of experience with code and that this issue has stumped me. I usually figure out bugs before I need to seek help.

It was intended as a joke - lighten up.

I guess from your response, and more particularly from @Nick_Pyner's post that you have the bluetooth module connected to Pins 0 and 1 in parallel with their use by the USB connection to the PC Serial Monitor. I did not understand that from your original post.

Have you tried connecting the Blutetooth device to a separate Serial Connection - using SoftwareSerial perhaps?

...R

I’ll give SS a shot right now and update you in about an hour. I just read some documentation on it and I get the feeling its going to work. Thanks for the help. Also, text makes it difficult to judge intention. Sorry for being a bit defensive.

-Jeb