so I'm trying to build an app that sends the phone's sensor data (accelerometer, GPS, etc) to an ESP32 through serial communication
I'm not a app developer and I have little to no knowledge when it comes to the actual phone drivers and whatnot, so most of the code is written with the help of AI, unfortunately.
I need the data to sent as soon as it is acquired, but the problem that im facing is that the phone's USB driver only sends data every 33ms which is fast but I need to widen this bottleneck.
So my problem is that due to USB data buffering, the device is only sending data at a rate of 30hz
Using some logging I found that the function that is supposed to send the data is running at a much faster rate, but on the receiving end I get a burst of data at consistently 33ms
I tried padding the data that I'm sending every time I run the send function (from 14 bytes to 64) but the receiving side would still only register at 33ms
AI recommended to make the esp request the data and only then the function is activated, still didnt work.
help.
here is the android code (this function runs every 1-7ms)
private void sendThreeFloatsSerial(float f1, float f2, float f3) {
if (!isSerialConnected || serialPort == null) {
if (serialsentText != null) {
serialsentText.setText("Serial not connected");
}
return;
}
final int PACKET_SIZE = 14;
final int PADDING_SIZE = 50;
final int TOTAL_SIZE = PACKET_SIZE + PADDING_SIZE;
try {
serialBuffer.clear();
serialBuffer.order(ByteOrder.LITTLE_ENDIAN);
serialBuffer.put((byte) 0xAA);
serialBuffer.putFloat(f1);
serialBuffer.putFloat(f2);
serialBuffer.putFloat(f3);
serialBuffer.put((byte) 0x55);
for(int i = 0; i < PADDING_SIZE; i++) {
serialBuffer.put((byte) 0x00);
}
long now = System.currentTimeMillis();
Log.d("SENSOR", "Event at: " + now + " ms | delay = " + (now - lastSensorTime) + " ms");
lastSensorTime = now;
// ----------------------------------------
byte[] data = new byte[TOTAL_SIZE];
serialBuffer.rewind(); // Reset position to 0 to read
serialBuffer.get(data); // Copy all 64 bytes from buffer to array
serialPort.write(data, SERIAL_TIMEOUT);
serialDataSentCount++; // Increment the counter
} catch (IOException e) {
Log.e(TAG, "Error sending padded serial data", e);
}
}
android studio log:
2025-11-15 03:41:59.000 32578-3268 SENSOR com.example.qc32 D Event at: 1763167319000 ms | delay = 3 ms
2025-11-15 03:41:59.003 32578-3268 SENSOR com.example.qc32 D Event at: 1763167319003 ms | delay = 3 ms
2025-11-15 03:41:59.006 32578-3268 SENSOR com.example.qc32 D Event at: 1763167319006 ms | delay = 3 ms
2025-11-15 03:41:59.009 32578-3268 SENSOR com.example.qc32 D Event at: 1763167319009 ms | delay = 3 ms
esp32 log (intervals of 33ms) :
03:25:35.655 -> Received Floats: f1 = -48.4219, f2 = -2.7359, f3 = -1.0233
03:25:35.655 -> Received Floats: f1 = -48.4219, f2 = -2.7359, f3 = -1.0233
03:25:35.655 -> Received Floats: f1 = -48.5170, f2 = -2.6567, f3 = -1.0182
03:25:35.688 -> Received Floats: f1 = -48.5170, f2 = -2.6567, f3 = -1.0182
03:25:35.688 -> Received Floats: f1 = -48.5170, f2 = -2.6567, f3 = -1.0182
03:25:35.688 -> Received Floats: f1 = -48.5809, f2 = -2.6660, f3 = -1.0828
03:25:35.688 -> Received Floats: f1 = -48.5080, f2 = -2.6637, f3 = -1.0182
03:25:35.688 -> Received Floats: f1 = -48.5185, f2 = -2.6486, f3 = -1.0108
03:25:35.688 -> Received Floats: f1 = -48.5185, f2 = -2.6486, f3 = -1.0108
03:25:35.688 -> Received Floats: f1 = -48.5185, f2 = -2.6486, f3 = -1.0108
03:25:35.721 -> Received Floats: f1 = -48.4946, f2 = -2.7134, f3 = -1.0608
03:25:35.721 -> Received Floats: f1 = -48.5174, f2 = -2.6933, f3 = -1.0584
03:25:35.721 -> Received Floats: f1 = -48.7024, f2 = -2.6933, f3 = -1.0584