Saving String to EEPROM

I’m trying to save a String value that is being passed from a function to the EEPROM.

The code below works if I define myObj as below but will not work if I pass a String Value I get error

invalid user-defined conversion from ‘String’ to ‘char’ [-fpermissive]

This works:

MyObject myObj = {
    "someValue"
  };

This does not:

SYSTEM_THREAD(ENABLED);

int pin = D4;
int led1 = D7;
int state;

int setPhoneEEPROMLocation = 1;

char email[20];

String setPhoneString;

struct MyObject {
  char phoneNumber[20];
};

void setup() {
  getStoredInfo();

  pinMode(pin, INPUT_PULLUP);
  pinMode(led1, OUTPUT);

  if (digitalRead(pin) == LOW) {

    digitalWrite(led1, HIGH);
    Particle.publish("DoorStatus", "OPEN", 60, PRIVATE);
  } else {
    Particle.publish("DoorStatus", "CLOSED", 60, PRIVATE);

  }

  Particle.variable("Status", state);
  Particle.variable("Phone", setPhoneString);
  Particle.function("setPhone", setPhone);

}

void loop() {

  for (uint32_t ms = millis(); millis() - ms < 200; Particle.process());

  if (digitalRead(pin) == LOW) {

    if (state != 0) {

      digitalWrite(led1, HIGH);
      Particle.publish("DoorStatus", "OPEN", 60, PRIVATE);
      state = 0;

    }

  } else if (digitalRead(pin) == HIGH) {

    if (state != 1) {

      digitalWrite(led1, LOW);
      Particle.publish("DoorStatus", "CLOSED", 60, PRIVATE);
      state = 1;

    }

  }

}

int setPhone(String phoneNumber) {

  setPhoneString = phoneNumber;
  Particle.publish("set", setPhoneString);

  MyObject myObj = {
    setPhoneString
  };
  EEPROM.put(setPhoneEEPROMLocation, myObj);

}

void getStoredInfo() {
  MyObject myObj;
  EEPROM.get(setPhoneEEPROMLocation, myObj);

  setPhoneString = (myObj.phoneNumber);

}

Not sure if this is the right approach but for anyone interested below is the working code

SYSTEM_THREAD(ENABLED);

int pin = D4;
int led1 = D7;
int state;

int setPhoneEEPROMLocation = 1;

char email[20];
char phoneNumber[50];

String setPhoneString;

void setup() {
  getStoredInfo();

  pinMode(pin, INPUT_PULLUP);
  pinMode(led1, OUTPUT);

  if (digitalRead(pin) == LOW) {

    digitalWrite(led1, HIGH);
    Particle.publish("DoorStatus", "OPEN", 60, PRIVATE);
  } else {
    Particle.publish("DoorStatus", "CLOSED", 60, PRIVATE);

  }

  Particle.variable("Status", state);
  Particle.variable("Phone", setPhoneString);
  Particle.function("setPhone", setPhone);

}

void loop() {

  for (uint32_t ms = millis(); millis() - ms < 200; Particle.process());

  if (digitalRead(pin) == LOW) {

    if (state != 0) {

      digitalWrite(led1, HIGH);
      Particle.publish("DoorStatus", "OPEN", 60, PRIVATE);
      state = 0;

    }

  } else if (digitalRead(pin) == HIGH) {

    if (state != 1) {

      digitalWrite(led1, LOW);
      Particle.publish("DoorStatus", "CLOSED", 60, PRIVATE);
      state = 1;

    }

  }

}

int setPhone(const char * data) {

  setPhoneString = data;

  strncpy(phoneNumber, data, sizeof(phoneNumber) - 1);
  phoneNumber[sizeof(phoneNumber) - 1] = '\0';

  EEPROM.put(setPhoneEEPROMLocation, phoneNumber);

}

void getStoredInfo() {
  EEPROM.get(setPhoneEEPROMLocation, phoneNumber);

  setPhoneString = phoneNumber;

}

Thanks for this. I think i found one of my problems.

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