The idea is that you put all of your retained variables in a struct, like this:
There’s only one member, deviceName, but you’d add all of your retained values after that if you had more.
The magic value itself is 0xa2c7206a, which I generated randomly.
A setup or some similar time, you check if the magic is valid, and if not, reinitialize all of your values.
if (retainedData.magic != RETAINED_DATA_MAGIC || retainedData.deviceName[0] == 0) {
memset(&retainedData, 0, sizeof(retainedData));
retainedData.magic = RETAINED_DATA_MAGIC;
Particle.subscribe("spark/", deviceNameHandler);
Particle.publish("spark/device/name");
state = NAME_WAIT_STATE;
stateTime = millis();
}