Don't know if it can be done the way you want but there are several workarounds.
One way to do it is to make a small sketch that places the needed values in EEPROM. Then load your intended sketch and it can read/modify the values.
Another way to do is to check if eeprom has valid values and if not initialize them and read/modify them. To do this there should be some kind of signature (magic cookie) in EEPROM.
The advantage of the first approach is that your intended sketch may be a bit smaller. I used this "trick" to decrease the size of a sketch that approached the 30KB limit with a few dozen bytes. Furthermore by having a separate application to write the eeprom one is able to add things like serial number (or device ID) in a more controlled way.
#include <EEPROM.h>
struct config_t
{
char vers[8]; // version
byte mac[8];
byte ip[6];
byte gw[6];
byte sn[6];
byte server[6]; // remote server
unsigned int port; // port
unsigned long id;
unsigned int precision;
unsigned int sec_per_sample; // Frequency
} config = {
"0.04",
{ 0xDE, 0xAD, 0x00, 0x00, 0x00, 0x12 },
{ 192, 168, 1, 123},
{ 192, 168, 1, 1 },
{ 255, 255, 255, 0 },
{ 192,168,1,240 },
8888,
19,
11,
1
};
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
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;
int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
void readEEPROM()
{
EEPROM_readAnything(0, config);
}
void writeEEPROM()
{
EEPROM_writeAnything(0, config);
}
void dumpConfig()
{
readEEPROM();
byte* p = (byte*)(void*)&config;
for (int i = 0; i < sizeof(config); i++)
{
Serial.print(*p++, HEX);
Serial.print(" ");
}
Serial.println();
}
///////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
Serial.println("Start writing EEPROM");
delay(1000);
writeEEPROM();
delay(1000);
dumpConfig();
Serial.println("Ready writing EEPROM");
while(true) // blink to indicate ready
{
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
delay(50);
}
}