Delay When Connecting Two Bluetooth Devices

Hi, I’m working on a project that transmits data using Bluetooth to my computer from 2 separate devices which have 5 force sensors each. The data from the force sensors are printed in an array. Each device has its own microcontroller. When both are connected with Bluetooth, there’s a delay in the time the data is received on my computer with only one of the devices. The delay for one of the devices is about 1-2 seconds while the other one transmits the data instantly. However, I need both of them to be instant. Is there any way to fix this?

Once the code is uploaded to the two microcontrollers, a Python program is used to read the data from the two ports and print both of them in the same line.

Arduino code used for both microcontrollers:

#include <BluetoothSerial.h>

#define NUM_SENSORS 5
int FORCE_SENSOR_PINS[NUM_SENSORS] = {36, 39, 34, 35, 32}; 
BluetoothSerial SerialBT;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_BTR");
}

void loop() {
  int analogReadings[NUM_SENSORS];

  for (int i = 0; i < NUM_SENSORS; i++) {
    analogReadings[i] = analogRead(FORCE_SENSOR_PINS[i]);
    delay(10); 
  }

  for (int i = 0; i < NUM_SENSORS; i++) {
    Serial.print(analogReadings[i]);
    SerialBT.print(analogReadings[i]);
    
    if (i < NUM_SENSORS - 1) {
      Serial.print(" ");
      SerialBT.print(" ");
    }
  }
  Serial.println();
  SerialBT.println();

}

Python code to print both readings in the same line:

import serial

ser1 = serial.Serial('COM9', 9600, timeout=1)
ser2 = serial.Serial('COM6', 9600, timeout=1)

file_path = r'C:\Users\user-\OneDrive\Desktop\output.txt'

with open(file_path, 'w') as file:
    pass  
while True:
    with open(file_path, 'a') as file:
        line = ser1.readline().decode('utf-8').strip() + " " + ser2.readline().decode('utf-8').strip()
        if line:  
            print(line)
            file.write(line + '\n')
            file.flush()

Hello @smac3, Welcome to the forum.

What I’m going to say is correct for serial, not sure how much it applies when there is bluetooth in the middle. Think about the timing of the sending of the characters and the receiving. You must not send faster than the receiver can keep up with. If you do, the characters are buffered somewhere and it appears that the values are delayed. Eventually the buffers overfill and some data is discarded.

At the PC you have serial at 9600 baud. Is that the PC’s built in bluetooth port(s)? or a bluetooth device (like HC05) connected to a (usb/real) serial port? 9600 baud is close to 1 character/mS. What rate are the ESPs transmitting? I think the loop is running each 50 mS. How many characters are you sending? 5 (guess) chars /value * 5 = 25. Looks like 50% of max, alright if everything works correctly. Check the calcs with actual data.

Maybe the PC and Python is not keeping up. Your Python program is writing to a file, and it’s a network file. Lots of opportunity there for things not to go as fast as you want. Suggest you test without writing to file, put the values on screen. Processing Java is quite happy to write values on screen 20 times/sec = 50 mS, I expect Python will do the same.

Try a bigger delay in the ESP see if that fixes the delay. Try faster baud rate e.g. 19200. Test the performance of the file system with a simple program that writes to file every 50 mS. Maybe you don’t need all the data logged, display each value on screen, but only log 1 in N.

Hope this helps.

Richard.