data-storage-dat
data storage
| Method | Size Limit | Persistent after reset/power off? | Use Case |
|---|---|---|---|
| Global variables (RAM) | Up to available RAM (~500KB) | ❌ No | Temporary data while program is running |
| RTC RAM | ~8 KB | ✅ Across reset, ❌ across power off | Store small state info between deep sleep cycles |
| NVS (Non-Volatile Storage / Preferences) | Key–value pairs, ~4 KB per partition | ✅ Yes | Config values, Wi-Fi credentials, small settings |
| SPIFFS / LittleFS | Depends on flash partition (100KB–1MB typical) | ✅ Yes | Store files, logs, calibration data |
| SD Card | GBs | ✅ Yes | Large data logging, images, audio |
| External EEPROM | Varies (e.g. 4KB–1MB chips) | ✅ Yes | Additional storage if onboard flash is insufficient |
BLE Data Update Workflow (ESP32)
-
ESP32 runs a BLE server
- Creates a characteristic for data transfer.
-
Phone/PC app connects via BLE
- Splits the file into small BLE packets (~20 bytes each).
- Sends them to ESP32 characteristic.
- Splits the file into small BLE packets (~20 bytes each).
-
ESP32 receives packets
- Reassembles them into a buffer.
- Writes the buffer into SPIFFS/LittleFS (or NVS for small key-values).
- Reassembles them into a buffer.
-
Program reloads data
- Next time your code runs (or immediately after update),
it reads the updated file/config from storage.
- Next time your code runs (or immediately after update),
BLE server code example (Arduino):
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <SPIFFS.h>
#define SERVICE_UUID "1234"
#define CHARACTERISTIC_UUID "5678"
File dataFile;
String buffer = "";
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
buffer += rxValue.c_str();
}
}
};
void setup() {
Serial.begin(115200);
SPIFFS.begin(true);
BLEDevice::init("ESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();
Serial.println("Waiting for BLE data...");
}
void loop() {
// Example: once data transfer finished, write to SPIFFS
if (buffer.endsWith("EOF")) { // simple marker
dataFile = SPIFFS.open("/data.txt", FILE_WRITE);
dataFile.print(buffer);
dataFile.close();
buffer = "";
Serial.println("File updated!");
}
}