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!

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jun 30, 2017 at 23:31
2
  • I dont think Arduino.h has a string typedef. perhaps you can use a character array instead. Commented Jul 1, 2017 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. Commented Sep 19, 2018 at 10:27

1 Answer 1

2

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
answered Jul 1, 2017 at 2:17
3
  • 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? Commented Jul 1, 2017 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. Commented Jul 2, 2017 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. Commented Jul 12, 2018 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.