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!
1 Answer 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
-
1So 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– Michael Artman2017年07月01日 15:27:41 +00:00Commented 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.goddland_16– goddland_162017年07月02日 04:07:39 +00:00Commented 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.Raja– Raja2018年07月12日 18:45:28 +00:00Commented Jul 12, 2018 at 18:45
Arduino.h
. Arduino uses C++, not C.