Realtime Bluetooth

Hello!

I am working on a project that requires realtime, wireless communication between a micro-controller (currently an ESP8266 but we plan to scale down later) and a mobile device. I have selected Bluetooth because it is cheap and integrated with pretty much every modern mobile device.

"Realtime". Once an event has been triggered (from a sensor or any other input) we need a "signal" to be sent to the mobile device as fast as possible. The events may be several seconds apart, or may be several times per second.

I have been playing with the HC05 and implemented a mechanism to calculate the time between sending and receiving a single byte of data; the micro-controller logs just before each send, the receiving device logs immediately after each receive and I process both logs after a test-run to calculate the latency of each event.

Generally, the transmission time is between 1 and 2 milliseconds (which is acceptable), but every few seconds I see a lag of anything up to 1000 milliseconds. This lag is what I am trying to eliminate. We need a constant latency of 1 to 2 milliseconds.

I have also been reading about packet sizes, which seems like a promising solution, but I cannot find how to play with packets/sizes on the HC05 (AT commands or Sketch libraries).

I am not tied to Bluetooth and have started researching WiFi direct, but I would prefer to use Bluetooth if we can achieve what we need. So this post is my final stand against Bluetooth before I take a different approach.

I also welcome any information about WiFi direct for this use case. So, can anybody offer any suggestions to help us?

Many thanks

Edit: Adding a code example for completeness

The HC05 is currently setup at 115200 baud, and I am using SoftwareSerial to read/write to it.

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(13, 15); // RX, TX

void setup() {
  bluetooth.begin(115200);
}

void loop() {

  // Wait for connection...

  if (isConnected) {

    byte event = readSensor();

    if (event > 0) {
      // Log send-time
      bluetooth.write(event);
    }
  }
}

betty7:
The HC05 is currently setup at 115200 baud, and I am using SoftwareSerial to read/write to it.

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(13, 15); // RX, TX

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

Without bothering to read the rest of your code, or even trying to understand what the problem really is, the above is a fundamental mistake, and probably the cause of whatever the problem might be. In short, running software serial at that speed is fatal. Indeed, I am surprised you got anything useful at all, but the reason why you might have is not worth investigating.
You have two options:

  1. Use software serial at 38400 maximum
  2. leave bluetooth at 115200 and run it on hardware serial pins 0,1 - deleting all reference to software serial.
    I would suggest the latter, software serial is never a good idea - ever, and here it is a particularly bad one.

I see, ok thank you. Could you shed some more light on why it's such a bad idea, just for educational purposes?

When I get the proof-of-concept back I will make the changes you suggested and report back

This might be educational

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
There are notes on hardware<>software serial, and background in the appendix.
I might also point out that there is some fast and loose comment up there. There is bluetooth and there is bluetoooth and the HC-05 may indeed be fine for your needs, but it is incompatible with iPhone.

I don't understand what you mean by "scaling down" from ESP8266. Since you already have it going, it may be the best solution.

Amazing, thank you, I will do some reading today.

You say the hC05 is not compatible with iPhone? I also didn't know this. Could you suggest an alternative that's compatible with both Android and iOS that would fit our use case?

Regarding scaling down: there are smaller boards than the 8266 available. One of our requirements is to scale the hardware down as much as we can, so another task for us is to research smaller boards for that. I decided to leave the scaling down until we have a fully working proof of concept.

betty7:
Regarding scaling down: there are smaller boards than the 8266 available. One of our requirements is to scale the hardware down as much as we can, so another task for us is to research smaller boards for that. I decided to leave the scaling down until we have a fully working proof of concept.

What sensors are you going to connect because scaling down usually means small battery operated is the end goal. A possibility is one of the nRF51822 modules that do Bluetooth 4.0 (iPhone compatible). They need a bit more work to setup/program but there is an Arduino core for the MCU.

betty7:
We need a constant latency of 1 to 2 milliseconds.

I have very little experience with Bluetooth but I reckon whatever wireless system you choose your project will have to be designed to survive the occasional delay or lost packet.

...R

betty7:
You say the hC05 is not compatible with iPhone?

Correct. IOS only works with BT4 (BLE). Modern Androids are also BT4. It is likely that you can use a BT4 which will work with both, but I don't know anything about your case, and I don't know anything abut BT4 either. I only say likely because you apparently just need a beep.

Regarding scaling down: there are smaller boards than the 8266 available.

This does not seem to justify Bluetooth over ESP8266. The latter come in versions smaller than bluetooth and have their own intelligence. Since you have been "playing with an HC-05" you imply you have been using an Arduino but, if you have been playing with an ESP8266, the Arduino may have been redundant.