Skip to content

Instantly share code, notes, and snippets.

@ironlungx
Last active February 17, 2025 21:28
Show Gist options
  • Save ironlungx/076252e1b9623c0dbe960b5133e45459 to your computer and use it in GitHub Desktop.
Save ironlungx/076252e1b9623c0dbe960b5133e45459 to your computer and use it in GitHub Desktop.
Serialize & Deserialize an object (for settings)
#include <Arduino.h>
#include <LittleFS.h>
typedef struct {
String ip;
int port;
String ssid;
String passwd;
bool autoConnect;
} Settings;
template <typename T> void save(const char *filename, const T *data) {
File file = LittleFS.open(filename, "w");
if (!file) {
throw std::runtime_error("Failed to open file for writing");
}
file.write(reinterpret_cast<const uint8_t *>(data), sizeof(T));
file.close();
}
template <typename T> void load(const char *filename, T *data) {
File file = LittleFS.open(filename, "r");
if (!file) {
throw std::runtime_error("Failed to open file for reading");
}
if (file.read(reinterpret_cast<uint8_t *>(data), sizeof(T)) != sizeof(T)) {
file.close();
throw std::runtime_error("Failed to read sufficient data");
}
file.close();
}
void setup() {
Serial.begin(115200);
LittleFS.begin();
// Initialize settings with example values
Settings settings = {
.ip = "localhost",
.port = 42,
.ssid = "Wifi password",
.passwd = "Strong & complicated password",
.autoConnect = true};
// Save data to flash FS
save("/settings", &settings);
// Load data from flash FS
Settings loadedSettings;
load("/settings", &loadedSettings);
}

Settings Manager for ESP32 with LittleFS

This library provides simple functions to serialize and deserialize settings or any other struct data to and from the ESP32's LittleFS (Little File System). It allows easy storage and retrieval of configuration parameters.

Features

  • Serialize any object of type T and save it as a file in LittleFS.
  • Deserialize the stored file back into an object.
  • Supports arbitrary struct types.
  • Error handling for file operations.

Dependencies

Ensure you have the following libraries installed in your Arduino IDE:

  • FS.h (included with ESP32 support)
  • LittleFS.h (for ESP32 file system support) or any other file system

Usage

1. Include the necessary headers

#include <Arduino.h>
#include <LittleFS.h>

2. Define a settings structure

typedef struct {
  String ip;
  int port;
  String ssid;
  String passwd;
  bool autoConnect;
} Settings;

3. Implement serialization and deserialization functions

template <typename T> void save(const char *filename, const T *data) {
  File file = LittleFS.open(filename, "w");
  if (!file) {
    throw std::runtime_error("Failed to open file for writing");
  }
  file.write(reinterpret_cast<const uint8_t *>(data), sizeof(T));
  file.close();
}

template <typename T> void load(const char *filename, T *data) {
  File file = LittleFS.open(filename, "r");
  if (!file) {
    throw std::runtime_error("Failed to open file for reading");
  }
  if (file.read(reinterpret_cast<uint8_t *>(data), sizeof(T)) != sizeof(T)) {
    file.close();
    throw std::runtime_error("Failed to read sufficient data");
  }
  file.close();
}

4. Use the functions in your ESP32 program

void setup() {
  Serial.begin(115200);
  LittleFS.begin();

  // Initialize settings with example values
  Settings settings = {
      .ip = "localhost",
      .port = 42,
      .ssid = "Wifi password",
      .passwd = "Strong & complicated password",
      .autoConnect = true};

  // Save data to flash FS
  save("/settings", &settings);

  // Load data from flash FS
  Settings loadedSettings;
  load("/settings", &loadedSettings);
}

void loop() {}

Error Handling

  • If file operations fail, a std::runtime_error is thrown.
  • Ensure LittleFS.begin() is called before using serialization functions.

Notes

  • Ensure the struct does not contain dynamic memory allocations (e.g., pointers). Using String is safe because it is managed internally by the Arduino framework.
  • The stored file size will be equal to sizeof(T), so be mindful of memory usage when storing large structs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment