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()