Skip to main content
added 28 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpystrncpy(savedValues.lastThing, readString.c_str(), 32sizeof(savedValues.lastThing));
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpy(savedValues.lastThing, readString.c_str(), 32);
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strncpy(savedValues.lastThing, readString.c_str(), sizeof(savedValues.lastThing));
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}
added 4 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpy(savedValues.lastThing, readString.c_str(), 32);
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpy(savedValues.lastThing, readString.c_str());
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpy(savedValues.lastThing, readString.c_str(), 32);
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

it is better not to use String type at all and it can't be saved to EEPROM. You can save a copy of the internal c-string of the String with strcpy(savedValues.lastThing, readString.c_str());

#include <EEPROM.h>

#define ADDR 512

struct config_t {
  char lastThing[32];
  int lastNum;
} savedValues;

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

  EEPROM.get(ADDR, savedValues);
  Serial.println(savedValues.lastThing);
  Serial.println(savedValues.lastNum);
}

void loop() {
  if (Serial.available()) {
    String readString = Serial.readStringUntil('|');
    int n = Serial.parseInt();

    strcpy(savedValues.lastThing, readString.c_str());
    savedValues.lastNum = n;
    EEPROM.put(ADDR, savedValues);

    //clean rest of the input (crlf)
    byte b;
    while (Serial.readBytes(&b, 1) > 0);
  }
}