Using mega serial port 1 and Bluetooth

Here is the code

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

I can send from serial to the mobile, but anything I send from mobile does not show in the Terminal.

I used Termite and I get funny character when I press send on the mobile , so there is something going that way. The data send from mobile has NO EFFECT; I mean I still get the SAME funny character. Changed baud rate I get different funny characters.

I can provide more details, but all I need is how to debug this problem. I am using HC-06

The problem is unlikely to be the MEGA or that code. After you've exhausted the possibility of the wrong baud rate, there's really not much else which can go wrong.

I haven't use the HC-06 myself. Maybe there's more options to set?

I took the HC-06 to the UNO; and modified the code

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  //Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial.write(inByte);
  }
}

It worked both ways with no problem so at least the HC-06 is working !!!

In your code in the opening post, use print statements and display the (hexa)decimal values of the received data. That might give you an idea what the funny characters actually are.

Replace

Serial.write(inByte);

by e.g.

if(inByte)<0x10)
{
  Serial.print(0);
}
Serial.print(inByte, HEX);
Serial.print(" ");

arishy:
I took the HC-06 to the UNO; and modified the code

If you have the HC-06 connected to pins 0 and 1 on the Uno then that is the same as directly connecting them to your PC - the Uno is redundant. For a comparable test you need to use SoftwareSerial on the Uno for the HC-06

Have you a GND connection to the HC-06 ?

I presume you have taken precautions not to feed the 5v Tx of an Arduino to a 3.3v HC-06

...R

With reference to the UNO test, are you saying that the two way communication is not a proof that the HC-06 is OK ???

The reason for the test is to prove to me that the module is ok and the baud rates are ok.

I also detached the UNO from usb and provided power to the module , the two way communication is working without connecting to the computer.

As for the 3.3v Rx the module is taking care of it.

As for the Mega since I have LEDS to indicate the proper communication I am powering the breadboard WITH EXTERNAL 5v AND it is grounded TO THE MEGA

arishy:
With reference to the UNO test, are you saying that the two way communication is not a proof that the HC-06 is OK ???

No. I was saying that the Uno is playing no part.

If all you wanted to do was connect the HC-O6 to the Rx and Tx pins you could have just as easily done that on the Mega. I had assumed you were saying that something works with an Uno but not with a Mega.

Post a link to the datasheet for your HC-06 module.

...R

Let me ask you again, do you suspect the module or the mega From what I see you want to know if it is true that the Rx does not need the 3.3 voltage divider !!!!!

I have HC-06 that works but needs help with the mega, because I want to install 3 BT modules on the mega using the extra 3 RX/TX

arishy:
Let me ask you again, do you suspect the module or the mega

I don't have enough information.

As already requested, please post a link to the datasheet for the HC-06 module that you are using. The module I have does not have in-built voltage conversion.

...R

If you are looking for a datasheet with schematics etc...Forget it , it is a Chinese product.
BUT here are two sources that very clearly NOT using voltage divider.

Now remember my problem was with "receiving" and not transmitting from the micro controller; and the voltage divider is needed from MCU 5 volt TX that is connected to RX of the bluetooth.

Product-User-Guide-JY-MCU-Bluetooth-UART-R1-0.pdf (469 KB)

That guy on Youtube doesn't know what he is talking about and the reason why Robin2 is confused is because no HC-06 breakout board had on-board conversion for its Rx signal. No matter how many sources there not using the voltage divider, and there are quite a few of them, if you can actually read what is on the back of the board, and can understand the difference between 3.3v and 5v, you might work out that not using a divider is a bad idea. Having said that, I have never heard of anybody actually frying an HC-0x by feeding it a 5v signal.

The Arduino code in your link seems straightforward and advises you in red about having bluetooth disconnected while uploading, but confuses things about the baud rate by specifying 38400 for a monitor you are not using. So it all seems pretty clunky and I doubt it was ever put into practice.

Your code from Uno should work unchanged on Mega. To use other serial ports, you simply change numbers and wire to pins 14>19 accordingly although, if you have that much communication to worry about, you might have been better off using an NRF24 network, which is made for that sort of thing, or even have one bluetooth polling several. In both cases, a Uno would suffice.

Appreciate your input, and please let us focus on one specific point.
I tested the communication bet mega and the BT USING TX0 and RX0.
Now changed the code to RX1,TX1 It is OK to transmit I see the data on my mobile.
THEN I SEND data from my mobile to mega I get nothing. Whatever I type in the send ( in my mobile) is not received by the mega.

One thing I can do is setup HC-05 as a server on say RX2,TX2 and see if the two BT's server and client can communicate

As for the data that I intend to deal with is mere one character to operate relays etc....
Practically NOTHING

Probably wiring.
You might find the following background notes useful, as it leaves the serial monitor out of the game. Use it on Uno first.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Thank for sharing...Just what I want.
By the way the document IS using voltage dividers that Robin2 suggested.

I'm not clear if Reply #13 means that the problem has been solved?

...R