-5

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
5
  • 1
    I do not understand what you mean by an "Android USB" app, but what? Commented Nov 15 at 3:46
  • I really doubt buffering would cause a consistent 33 ms delay. Commented Nov 15 at 13:35
  • Well sorry for not being clear enough, I meant an android app that is supposed to send data over a physical USB connection, this is basically the whole purpose of the app beside some minor things Commented Nov 16 at 0:13
  • As for the delay it is very much real, the packets are only sent every 33ms for “efficiency “ Commented Nov 16 at 0:14
  • Unclear how you are communicating with the USB port on Android, i.e. library or custom code. If custom code, show code, if library which one. At a guess you may be using github.com/mik3y/usb-serial-for-android but that also presumes that you know Android and has read/understood the things in github.com/mik3y/usb-serial-for-android/wiki/FAQ While using an AI to 'vibe code' your way to something you may think work as your question indicates once past that the developer needs to understand the code/platform they are working on. Commented Nov 16 at 2:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.