Simultaneous serial communication (HC-06 and USB)

Hello,

I've set up a project and I see that simultaneous serial communication is possible using the bluetooth HC-06 module and the USB serial. The only problem is that while the HC-06 is connected, the usb (via serial, e.g. serial monitor) can only receive data and it can't send.

I guess this is normal, but is there a way to enable sending from both usb and bluetooth?

What Arduino are you using?

if it is an Uno or Nano use SoftwareSerial to create a separate serial port for the HC06

If it is a Mega. Leonardo or Micro use Serial1 for the HC06

...R Serial Input Basics

if it is an Uno or Nano use SoftwareSerial to create a separate serial port for the HC06

SoftwareSerial is the worst choice. Better choices are:

1) The fastest, most reliable way to connect a device is to use a HardwareSerial port.

On any Arduino board, you can connect a device to the Serial pins (0 & 1). You can still print debug statements, and they will show up on the Serial Monitor window. The characters received from the device will not interfere with debug prints, and you will not see those characters on the Serial Monitor window. Debug print will also be sent to the device, but if it has a command format, it would ignore the debug prints as "invalid commands". For example, if you have to send "AT " at the beginning of commands for the device, don't do any debug prints that start with that sequence. The device will ignore all your debug prints.

However, when you need to upload a new sketch to the Arduino, you must disconnect the device from the Arduino RX pin 0. Otherwise, the device characters will interfere with the uploading data.

For Mega, Micro, Due and Teensy boards, you can connect a device to the Serial1, Serial2 or Serial3 pins.

For Micro and Leo (and other 32U4-based Arduinos), you can connect the GPS device to the Serial1 pins (0 & 1).

2) If you can't connect the device to a HardwareSerial port, you should download and install the AltSoftSerial or NeoICSerial library. These libraries only work on two specific pins (8 & 9 on an UNO). This library is very efficient and reliable. It uses one of the hardware TIMERs, so it may conflict with libraries that use TIMERs or PWM output (e.g., servo).

3) If you can't use the pins required by AltSoftSerial, and your device runs at 9600, 19200 or 38400 baud, you should download and install the NeoSWSerial library. This library is almost as efficient. It will help you avoid common timing problems caused by SoftwareSerial. It does not need an extra TIMER, so it can be used with most other libraries. It does use Pin Change Interrupts, but there is an option in the header file that allows you to coordinate other PCI usage with NeoSWSerial.

NeoSWSerial can be used with AltSoftSerial at the same time, allowing your sketch to have two extra serial ports.

4) SoftwareSerial is NOT RECOMMENDED, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch, or with other libraries. It cannot transmit and receive at the same time, and your sketch can only receive from one SoftwareSerial instance at time.

AltSoftSerial and NeoSWSerial are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

You can run Bluetooth and USB together but, as mentioned above, you must disconnect Bluetooth when uploading. The concurrent running means you can usefully see what is going on with the bluetooth communications, but I'm not sure it has any more value than that.

-dev: 1) The fastest, most reliable way to connect a device is to use a HardwareSerial port.

On any Arduino board, you can connect a device to the Serial pins (0 & 1). You can still print debug statements, and they will show up on the Serial Monitor window. The characters received from the device will not interfere with debug prints, and you will not see those characters on the Serial Monitor window. Debug print will also be sent to the device, but if it has a command format, it would ignore the debug prints as "invalid commands". For example, if you have to send "AT " at the beginning of commands for the device, don't do any debug prints that start with that sequence. The device will ignore all your debug prints.

I'm already using HardwareSerial. But I don't understand what you are saying about debug statements. Since Arduino is connected via usb serial, and an hc-06 on it, shouldn't it receive/transmit data form/to both?

E.g. the code from my Arduino will do serial.print() to anything that it receives. If I write "test1" it will reply with "test1". Now, if I write "test1" from my phone via Bluetooth (connected to the hc-06), I can see the reply "test1" in my Android app, but if I write "test1" in serial monitor from PC, I don't get any reply (but I can see the replies, in serial monitor on PC, from when I did "test1" with HC-06 from phone).

My thoughts are, the RX pin from Arduino is kept busy by the HC-06 and it can't receive any data from USB serial while the HC-06 is connected to it. But the TX is available for both HC-06 and USB serial.

Maybe the software serial solutions like AltSoftSerial can solve this drawback. But the thing is that I need USB and HC-06 to be on the same serial.

raulx222: My thoughts are, the RX pin from Arduino is kept busy by the HC-06 and it can't receive any data from USB serial while the HC-06 is connected to it. But the TX is available for both HC-06 and USB serial.

That is almost certainly the problem. Which is why I suggested various alternatives in Reply #1. Don't connect two devices directly to the same serial port.

...R

I need USB and HC-06 to be on the same serial.

Nonsense. You cannot have 2 "talkers" on one serial RX pin. You can listen to 2 talkers and forward the characters received on one port to the other port:

void loop()
{
  if (Serial.available())
    bt.write( Serial.read() );

  if (bt.available())
    Serial.write( bt.read() );
}

Option 1 is only useful when the USB is not transmitting anything to the Arduino. Typically, Arduino sketches only print debug statements to the Serial Monitor window. Most sketches do not expect characters "from" the Serial Monitor window (i.e., by typing something in the field and pressing "Send). That common usage would have allowed the HC-06 to transmit to pin 0, and the Arduino sketch could transmit to both the Serial Monitor window and the HC-06 at the same time (if it has a special command format).

Since the USB (a PC?) is transmitting to the Arduino (something you didn't share), you cannot use option 1 for the HC-06. The HC-06 must be on AltSoftSerial. It cannot be on a NeoSWSerial set of pins, because transmitting on NeoSWSerial prevents reception on everything else. AltSoftSerial and Serial will not interfere with each other.