HC-05 Bluetooth Serial Handshake too slow. (Please, need urgent help)

(Arduino Mega 2560 Board)

I have a program that needs to quickly send and receive small quantities of data over a Serial Port, when the Arduino is ready to receive it.

I am using an HC-05 Bluetooth module, when I run the following Arduino and Processing programs over the USB port of my laptop, every thing goes super fast as intend it too. However when I use the BC-05 to transmit data, there's delay of several hundred milliseconds. (Just read the code you'll get what's going on.)

I spent days trying to fix this, thinking the problem was with the data and the way I was writing/reading it. But after dozens of alterations, it turns out my code was always fine (I think). HC-05 or Bluetooth in general has a truck load of latency between transmissions.

So I wrote a test code to check where the issue is arising. The code is basically a handshake, which happens pretty quickly over the USB A (at the laptop) to USB B (at the Arduino) Cable (Literally ). But over the HC-05, there is an eye watering amount of delay between 2 complete handshakes.

Read files "Arduino code" and "Processing code"**

Have a look at the Console window of Processing to get a better idea of what I'm talking about.

Read files "Processing output over USB" and "Processing output over HC-05"**

Both Console Programs were allowed to run for ~5 seconds.

  1. When using USB cable:

There's so many handshakes happening so quickly that the original first few outputs are not available in the window!!!!!!(WHICH IS EXACTLY WHAT I NEED)

Notice how there's only about 4 ms delay between handshakes? And there's so many of them that the first few aren't even available in the window anymore (the outputs started printing at about 2000)! (Again exactly what I need)

And now the bone of contention, the issue that has been driving me absolute NUTS since damn 4 days/b]
2. Processing console output when using HC-05
(Again made to run for ~5 seconds)
Nearly 100-150 ms delay !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
And like a handful of handshakes (no pun in-10-dead) in whole 5 seconds! and over usb theres like hundreds per second!
Question 1:
why? why? why WHY? WHY? WHY? WHY? WHY? WHY? WHYYYYYYYY IS this happening???!!!!
Question 2:
Now, I see there's a similar but BT 4.0 HM-10 module. Will that be any better?
Question 3:
My actual goal is to just send data from PC to Arduino over the Internet.
I need bluetooth because I am thinking of doing it this way: from PC ---> Internet ---> Android Smartphone ---> Bluetooth module ---> Arduino.
Will the ESP8266 NodeMCU WIFI module cut out the middleman (Android + Bluetooth mod), AND be able to offer me similar data transfer as that over USB (not including the delay over the Internet of course, that can't be avoided.)
Thanks a ton for making it this far. Please help me I'm on the verge clawing my eyes out trying to troubleshoot this since forever.

Arduino Code.txt (911 Bytes)

Processing Code.txt (438 Bytes)

Processing output over USB.txt (13.6 KB)

Procssing console output over Hc-05.txt (811 Bytes)

Read the code?

No, you post your code using code tags. </>

.

I tried doing that. It goes over 9000 chars that way. The files are just down there.
Anyhoo here you go.

void setup() 
{
  Serial.begin(115200); 

/*I know this is a high baud rate but the problem isn't here, all rates work fine over the cable, none over the bluetooth module*/


  while(!Serial );
  delay(1000); // just an initial breathing room for execution post pressing reset button on duino

  Serial.write("X"); // 'X' is the char the Processing program buffers until, and then reverts back with 'Z'
   
  delay(10); /* I know you people hate delays but after 100s of hours of trial and error, I can confidently 
  say that  initial setup delays cause more good than harm, also 1. it's just a one time delay and 2. it never is a problem over the cable, so shouldn't be over the module*/
}

void SerialEvent()
{
  Serial.write("X");
}

void loop() 
{
  while( !( (char)(Serial.read()) == 'Z') );  // waits to receive 'Z'
  SerialEvent();
}

Processing:

void setup() 
{
import processing.serial.*;

Serial myPort;
void setup()
{
  size(200, 200);
  myPort = new Serial(this, "COM3", 115200);
}

void serialEvent (Serial myPort)
{
  myPort.clear();
  myPort.write('Z');

  println("Handshake happened  " + millis()); // confirmation + time in milliseconds
}

void draw()
{
  background(0,0,0);

  myPort.bufferUntil('X');
}

Serial for communication with PC + Processing.
I don't see anything for serial to the HC-05.

Serial when using processing through USB cable.
Serial1 when using processing through the module.
Also I just change COM3 -> COM5 on processing because the latter is the HC-05 port
The rest of the code stays the exact same.

That's my question. When all else stays same why is HC-05 so much worse than USB. I am literally just sending a single char.

karan328:
Question 3:

My actual goal is to just send data from PC to Arduino over the Internet.

I need bluetooth because I am thinking of doing it this way: from PC ---> Internet ---> Android Smartphone ---> Bluetooth module ---> Arduino.

Will the ESP8266 NodeMCU WIFI module cut out the middleman (Android + Bluetooth mod), AND be able to offer me similar data transfer as that over USB

Not only will a NodeMCU cut out the middleman, thereby relieving you of the interesting exercise of getting the phone to receive via WiFi and simultaeously transmit via Bluetooth, but it will also likely make your Arduino redundant as well. I wouldn't guarantee anything about data rate, as I don't know where the real problem is and, since you intend to use the Internet anyway, I fail to see why you are worried about all this speed stuff.
It could be that you have run up against a hitherto unknown brick wall that is all to do with Bluetooth protocols, and therefore unfixable.

At a guess, and assuming it is possible, your best bet is to have Processing talk direct to NodeMCU via WiFi, thereby getting rid of Internet, phone, and Bluetooth. A NodeMCU is cheaper and faster than an Arduino.

Question 2:

Now, I see there's a similar but BT 4.0 HM-10 module. Will that be any better?

You will probably, at best, just move the problem rather than solve it, and may even make matters worse, as I understand that HM-10 is not well suited for this sort of Bluetooth operation.