Esp32 SD Card Sample Rate Problem. Please Help :(

Hello Everyone,

I have been stuck on this problem for almost a week now and cannot figure it out. I have an esp32 (transmitter) sending data as fast as possible to my sim7000G module (receiver) that has a microSD on board. Sd Card used is a Micro SD HC SDHC Flash Memory Card.

I want to write all of this data (5x depth sensors, XYZ acceleration data, temperature and millis time) from the transmitter on the SD card. I should be receiving about 72 packets a second and can keep track because of the millis value that is sent with them. When I open the text file after some time it seems as if the Data writing gets less and less samples each second that goes by. The first reading is perfect its 71 samples. The next is worse around 28. The next second is even worse at 15. It gets worse and worse every second that it is running. It should be 71 to 72 samples for each second andI am not sure why it is doing this. Here is the text file readings:

0.0000 0.0000 0.0000 0.0000 0.1438 2.4190 8.9764 0.00 0.00 13
0.0000 0.0000 0.0000 0.0000 0.1634 2.4059 8.9829 0.00 0.00 27
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 8.9960 0.00 0.00 40
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 9.0025 0.00 0.00 54
0.0000 0.0000 0.0000 0.0000 0.1504 2.4190 8.9894 0.00 0.00 67
0.0000 0.0000 0.0000 0.0000 0.1308 2.4059 8.9633 0.00 0.00 80
0.0000 0.0000 0.0000 0.0000 0.1504 2.3928 8.9633 0.00 0.00 93
0.0000 0.0000 0.0000 0.0000 0.1438 2.4190 8.9764 0.00 0.00 107
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 8.9633 0.00 0.00 120
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 8.9764 0.00 0.00 133
0.0000 0.0000 0.0000 0.0000 0.1438 2.4124 9.0025 0.00 0.00 147
0.0000 0.0000 0.0000 0.0000 0.1569 2.3863 9.0025 0.00 0.00 160
0.0000 0.0000 0.0000 0.0000 0.1308 2.4059 8.9894 0.00 0.00 173
0.0000 0.0000 0.0000 0.0000 0.1438 2.4059 8.9960 0.00 0.00 187
0.0000 0.0000 0.0000 0.0000 0.1373 2.4059 8.9829 0.00 0.00 200
0.0000 0.0000 0.0000 0.0000 0.1308 2.4059 8.9894 0.00 0.00 213
0.0000 0.0000 0.0000 0.0000 0.1765 2.3797 8.9894 0.00 0.00 227
0.0000 0.0000 0.0000 0.0000 0.1634 2.3732 9.0025 0.00 0.00 240
0.0000 0.0000 0.0000 0.0000 0.1634 2.4059 8.9829 0.00 0.00 253
0.0000 0.0000 0.0000 0.0000 0.1373 2.4190 8.9829 0.00 0.00 267
0.0000 0.0000 0.0000 0.0000 0.1308 2.4190 9.0025 0.00 0.00 280
0.0000 0.0000 0.0000 0.0000 0.1373 2.3994 8.9633 0.00 0.00 293
0.0000 0.0000 0.0000 0.0000 0.1504 2.3863 8.9371 0.00 0.00 307
0.0000 0.0000 0.0000 0.0000 0.1308 2.3928 8.9829 0.00 0.00 320
0.0000 0.0000 0.0000 0.0000 0.1438 2.3928 8.9633 0.00 0.00 334
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9829 0.00 0.00 347
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 8.9698 0.00 0.00 360
0.0000 0.0000 0.0000 0.0000 0.1373 2.3928 9.0025 0.00 0.00 374
0.0000 0.0000 0.0000 0.0000 0.1634 2.3994 9.0090 0.00 0.00 387
0.0000 0.0000 0.0000 0.0000 0.1569 2.3928 8.9960 0.00 0.00 400
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 8.9306 0.00 0.00 413
0.0000 0.0000 0.0000 0.0000 0.1242 2.3928 8.9829 0.00 0.00 427
0.0000 0.0000 0.0000 0.0000 0.1308 2.3928 8.9764 0.00 0.00 440
0.0000 0.0000 0.0000 0.0000 0.1569 2.3797 8.9764 0.00 0.00 453
0.0000 0.0000 0.0000 0.0000 0.1569 2.4124 8.9960 0.00 0.00 467
0.0000 0.0000 0.0000 0.0000 0.1569 2.3928 8.9567 0.00 0.00 480
0.0000 0.0000 0.0000 0.0000 0.1569 2.4124 8.9960 0.00 0.00 493
0.0000 0.0000 0.0000 0.0000 0.1504 2.3994 8.9894 0.00 0.00 507
0.0000 0.0000 0.0000 0.0000 0.1504 2.3994 8.9894 0.00 0.00 520
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9698 0.00 0.00 533
0.0000 0.0000 0.0000 0.0000 0.1438 2.3863 8.9960 0.00 0.00 547
0.0000 0.0000 0.0000 0.0000 0.1242 2.3994 8.9502 0.00 0.00 560
0.0000 0.0000 0.0000 0.0000 0.1438 2.4059 8.9960 0.00 0.00 573
0.0000 0.0000 0.0000 0.0000 0.1373 2.4059 8.9829 0.00 0.00 587
0.0000 0.0000 0.0000 0.0000 0.1634 2.4190 8.9960 0.00 0.00 600
0.0000 0.0000 0.0000 0.0000 0.1438 2.4059 8.9764 0.00 0.00 613
0.0000 0.0000 0.0000 0.0000 0.1504 2.4124 9.0090 0.00 0.00 627
0.0000 0.0000 0.0000 0.0000 0.1504 2.3994 8.9829 0.00 0.00 640
0.0000 0.0000 0.0000 0.0000 0.1373 2.4059 8.9764 0.00 0.00 653
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9960 0.00 0.00 667
0.0000 0.0000 0.0000 0.0000 0.1438 2.3797 8.9698 0.00 0.00 680
0.0000 0.0000 0.0000 0.0000 0.1373 2.4124 8.9764 0.00 0.00 720
0.0000 0.0000 0.0000 0.0000 0.1569 2.3732 8.9894 0.00 0.00 733
0.0000 0.0000 0.0000 0.0000 0.1504 2.3994 8.9829 0.00 0.00 760
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9764 0.00 0.00 773
0.0000 0.0000 0.0000 0.0000 0.1308 2.3928 8.9633 0.00 0.00 813
0.0000 0.0000 0.0000 0.0000 0.1634 2.4124 9.0025 0.00 0.00 853
0.0000 0.0000 0.0000 0.0000 0.1634 2.4059 8.9829 0.00 0.00 867
0.0000 0.0000 0.0000 0.0000 0.1308 2.4124 8.9764 0.00 0.00 893
0.0000 0.0000 0.0000 0.0000 0.1308 2.4059 8.9829 0.00 0.00 907
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 9.0221 0.00 0.00 933
0.0000 0.0000 0.0000 0.0000 0.1438 2.4059 8.9829 0.00 0.00 946
0.0000 0.0000 0.0000 0.0000 0.1308 2.4190 8.9894 0.00 0.00 973
0.0000 0.0000 0.0000 0.0000 0.1242 2.4255 8.9698 0.00 0.00 986
0.0000 0.0000 0.0000 0.0000 0.1569 2.3994 8.9829 0.00 0.00 26
0.0000 0.0000 0.0000 0.0000 0.1438 2.4190 8.9764 0.00 0.00 80
0.0000 0.0000 0.0000 0.0000 0.1438 2.3928 8.9764 0.00 0.00 93
0.0000 0.0000 0.0000 0.0000 0.1177 2.4124 8.9829 0.00 0.00 120
0.0000 0.0000 0.0000 0.0000 0.1242 2.4059 8.9960 0.00 0.00 133
0.0000 0.0000 0.0000 0.0000 0.1504 2.3928 8.9764 0.00 0.00 186
0.0000 0.0000 0.0000 0.0000 0.1634 2.4059 8.9829 0.00 0.00 239
0.0000 0.0000 0.0000 0.0000 0.1373 2.3994 8.9960 0.00 0.00 266
0.0000 0.0000 0.0000 0.0000 0.1569 2.3994 8.9960 0.00 0.00 279
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 8.9764 0.00 0.00 306
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 8.9829 0.00 0.00 319
0.0000 0.0000 0.0000 0.0000 0.1438 2.3928 9.0221 0.00 0.00 333
0.0000 0.0000 0.0000 0.0000 0.1373 2.3928 8.9764 0.00 0.00 359
0.0000 0.0000 0.0000 0.0000 0.1700 2.3994 9.0025 0.00 0.00 373
0.0000 0.0000 0.0000 0.0000 0.1569 2.3732 8.9894 0.00 0.00 426
0.0000 0.0000 0.0000 0.0000 0.1896 2.4124 8.9894 0.00 0.00 492
0.0000 0.0000 0.0000 0.0000 0.1896 2.3863 9.0025 0.00 0.00 519
0.0000 0.0000 0.0000 0.0000 0.0981 2.4124 9.0090 0.00 0.00 545
0.0000 0.0000 0.0000 0.0000 0.1373 2.4386 8.9960 0.00 0.00 599
0.0000 0.0000 0.0000 0.0000 0.1373 2.3797 8.9567 0.00 0.00 652
0.0000 0.0000 0.0000 0.0000 0.1504 2.3797 8.8914 0.00 0.00 665
0.0000 0.0000 0.0000 0.0000 0.1896 2.3601 8.9241 0.00 0.00 705
0.0000 0.0000 0.0000 0.0000 0.1700 2.3536 8.9567 0.00 0.00 745
0.0000 0.0000 0.0000 0.0000 0.1046 2.4124 8.9175 0.00 0.00 785
0.0000 0.0000 0.0000 0.0000 0.1896 2.3601 8.9764 0.00 0.00 838
0.0000 0.0000 0.0000 0.0000 0.1569 2.3863 9.0221 0.00 0.00 945
0.0000 0.0000 0.0000 0.0000 0.1308 2.3994 8.9764 0.00 0.00 971
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 8.9764 0.00 0.00 13
0.0000 0.0000 0.0000 0.0000 0.1504 2.4255 8.9502 0.00 0.00 67
0.0000 0.0000 0.0000 0.0000 0.1177 2.4386 8.9960 0.00 0.00 80
0.0000 0.0000 0.0000 0.0000 0.1700 2.3797 8.9829 0.00 0.00 146
0.0000 0.0000 0.0000 0.0000 0.1504 2.4255 8.9241 0.00 0.00 200
0.0000 0.0000 0.0000 0.0000 0.1308 2.4255 8.9764 0.00 0.00 306
0.0000 0.0000 0.0000 0.0000 0.1111 2.4124 8.9960 0.00 0.00 426
0.0000 0.0000 0.0000 0.0000 0.1504 2.3667 9.0090 0.00 0.00 452
0.0000 0.0000 0.0000 0.0000 0.1438 2.4190 8.9502 0.00 0.00 505
0.0000 0.0000 0.0000 0.0000 0.1438 2.3732 8.9764 0.00 0.00 559
0.0000 0.0000 0.0000 0.0000 0.1373 2.3994 8.9633 0.00 0.00 639
0.0000 0.0000 0.0000 0.0000 0.1373 2.3928 8.9894 0.00 0.00 692
0.0000 0.0000 0.0000 0.0000 0.1111 2.4320 8.9633 0.00 0.00 811
0.0000 0.0000 0.0000 0.0000 0.1308 2.3994 8.9633 0.00 0.00 931
0.0000 0.0000 0.0000 0.0000 0.1569 2.4059 8.9698 0.00 0.00 958
0.0000 0.0000 0.0000 0.0000 0.1504 2.4059 8.9960 0.00 0.00 53
0.0000 0.0000 0.0000 0.0000 0.1046 2.4190 8.9633 0.00 0.00 147
0.0000 0.0000 0.0000 0.0000 0.1308 2.4124 9.0090 0.00 0.00 319
0.0000 0.0000 0.0000 0.0000 0.1504 2.4124 8.9633 0.00 0.00 439
0.0000 0.0000 0.0000 0.0000 0.1111 2.4124 8.9829 0.00 0.00 452
0.0000 0.0000 0.0000 0.0000 0.1373 2.3928 8.9698 0.00 0.00 572
0.0000 0.0000 0.0000 0.0000 0.1634 2.4059 8.9567 0.00 0.00 852
0.0000 0.0000 0.0000 0.0000 0.1242 2.4255 8.9306 0.00 0.00 971
0.0000 0.0000 0.0000 0.0000 0.1373 2.4451 8.9567 0.00 0.00 998
0.0000 0.0000 0.0000 0.0000 0.1569 2.3994 8.9960 0.00 0.00 107
0.0000 0.0000 0.0000 0.0000 0.1308 2.4320 8.9437 0.00 0.00 386
0.0000 0.0000 0.0000 0.0000 0.1111 2.4255 8.9437 0.00 0.00 506
0.0000 0.0000 0.0000 0.0000 0.1373 2.3863 9.0090 0.00 0.00 532
0.0000 0.0000 0.0000 0.0000 0.1504 2.3928 9.0090 0.00 0.00 652
0.0000 0.0000 0.0000 0.0000 0.1308 2.4320 8.9698 0.00 0.00 798
0.0000 0.0000 0.0000 0.0000 0.1308 2.3994 8.9764 0.00 0.00 945
0.0000 0.0000 0.0000 0.0000 0.1373 2.4059 8.9894 0.00 0.00 53
0.0000 0.0000 0.0000 0.0000 0.1569 2.3994 9.0156 0.00 0.00 66
0.0000 0.0000 0.0000 0.0000 0.1765 2.3928 9.0090 0.00 0.00 186
0.0000 0.0000 0.0000 0.0000 0.1242 2.4059 8.9829 0.00 0.00 333
0.0000 0.0000 0.0000 0.0000 0.1438 2.4255 8.9764 0.00 0.00 466
0.0000 0.0000 0.0000 0.0000 0.1634 2.3863 8.9894 0.00 0.00 599
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9764 0.00 0.00 612
0.0000 0.0000 0.0000 0.0000 0.1438 2.3994 8.9829 0.00 0.00 732
0.0000 0.0000 0.0000 0.0000 0.1438 2.4124 8.9633 0.00 0.00 878
0.0000 0.0000 0.0000 0.0000 0.1438 2.3863 8.9764 0.00 0.00 998

Here is the code I am using to write the incoming data to the SD card:

#include <esp_now.h>
#include <WiFiClientSecure.h>
#include <WiFi.h>
#include <SD.h>
#include <SPI.h>
#include <BluetoothSerial.h>

#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define FILE_NAME "/data.txt"

struct struct_message {
  float depth1;
  float depth2;
  float depth3;
  float depth4;
  float accel_x;
  float accel_y;
  float accel_z;
  char TTime[15];
  float Temp;
};

File DataFile;
uint8_t broadcastAddress[] = { 0xCC, 0xDB, 0xA7, 0x5A, 0x32, 0xC0 };
esp_now_peer_info_t peerInfo;
bool LoggingEnable = true;

struct_message myData;
BluetoothSerial BTS;

void OnDataRecv(const uint8_t* mac, const uint8_t* incomingData, int len) {
  if (LoggingEnable) {
    memcpy(&myData, incomingData, sizeof(myData));
    DataFile = SD.open(FILE_NAME, FILE_APPEND);
    if (DataFile) {
      DataFile.print(myData.depth1);
      DataFile.print(" ");
      DataFile.print(myData.depth2);
      DataFile.print(" ");
      DataFile.print(myData.depth3);
      DataFile.print(" ");
      DataFile.print(myData.depth4);
      DataFile.print(" ");
      DataFile.print(myData.accel_x);
      DataFile.print(" ");
      DataFile.print(myData.accel_y);
      DataFile.print(" ");
      DataFile.print(myData.accel_z);
      DataFile.print(" ");
      DataFile.println(myData.TTime);
      DataFile.close();
    } else {
      Serial.println("Failed to open DataFile for writing");
    }
  }
}

void setup() {
  Serial.begin(115200);
  delay(10);

  WiFi.mode(WIFI_STA);
  SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
  if (!SD.begin(SD_CS)) {
    Serial.println("SDCard Failed.");
  } else {
   Serial.println("SDCard Initialized Success.");
    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    Serial.print("SDCard Size: ");
    Serial.print(cardSize);
    Serial.println(" MB");
  }

  if (esp_now_init() != ESP_OK) {
   Serial.println("Error initializing ESP-NOW");
    return;
  } else {
   Serial.println("ESP-NOW initialized Sucessfully");
  }
  esp_now_register_recv_cb(OnDataRecv);

  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
  BTS.begin("FloodSensor");
  Serial.println("ready to connect bluetooth");
}

void loop() {
  if (BTS.available()) {
    char command = BTS.read();
    while (BTS.available()) {
      BTS.read();
    }
    if (command == '1') {
      LoggingEnable = false;
      BTS.println("Sending Data:");
      DataFile = SD.open(FILE_NAME, FILE_READ);
      if (DataFile) {
        while (DataFile.available()) {
          String line = DataFile.readStringUntil('\n');
          BTS.println(line);
        }
        DataFile.close();
      } else {
        BTS.println("Failed to open DataFile for reading");
      }
      LoggingEnable = true;
    }
  }
}

This causes the String to be created new each time you get a new message. This takes lots of time. A much quicker way would be to just write the structure to the SD card.
In addition, you seem to be closing the file for each record and this will take more and more time as you add to the file size.

Great thanks for the quick feedback. How can I just write the structure to the SD card? Could you give me a little more information on how to do that?

Also, I understand the closing of the file for each record, couldn't I keep it open and close it after awhile? I will be running this for 24 hours straight so I don't want to leave it open the whole time in case power goes out I could lose everything I believe if the file never closed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.