2

I am creating a server node that I want to be able to save the wifi SSID and the password into persistent memory so that when it starts ups it can connect to the network. I have looked for several hours and found just a few bits of information. One bit I found here http://playground.arduino.cc/Code/EEPROMWriteAnything

#include <EEPROM.h>
#include <Arduino.h>  // for type definitions

template <class T> int EEPROM_writeAnything(int ee, const T& value) {
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value) {
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}

This code will not store String values to the EEPROM. So as I thought I would ask for advice from the Stack Exchange geniuses.

Thanks!

  • I dont think Arduino.h has a string typedef. perhaps you can use a character array instead. – j0h Jul 1 '17 at 1:56
  • String is a type that exists in C++, so it doesn't need to exist in Arduino.h. Arduino uses C++, not C. – vurp0 Sep 19 '18 at 10:27
1

EEPROM library has two functions put and get , which can help you store character array of any size, not exceeding the size of internal EEPROM size of Arduino. Please see the examples provided in the link above.

Here is a sample code to get you going.

int address = 10;
char arrayToStore[20];                    // Must be greater than the length of string.
String testString = "hello there";

testString.toCharArray(arrayToStore, testString.length()+1);  // Convert string to array.

EEPROM.put(address, arrayToStore);                 // To store data
EEPROM.get(address, arrayToStore);                 // To read data
  • 1
    So this kept bugging me, if I want to get a value from the address how come I have to put a value that I want to store there as well? What if I want to just read the values there? – Michael Artman Jul 1 '17 at 15:27
  • I didn't get you, buddy. This is just an example sketch to show you how to store and read data in EEPROM locations. You have to make your own sketch for your requirement based upon this. – goddland_16 Jul 2 '17 at 4:07
  • This is how I store my configuration data: I have a function to store the configuration, which will execute only occasionally whenever there is a config change. This mode is controlled by a jumper connection on my PCB. When the program restarts in regular execution mode, it just keeps reading the config data. The behavior is controlled by a check flag in the startup code. – Raja Jul 12 '18 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.