[Solved] Photon - help with EEPROM values after re-flashing. Want to count resets after reflashing

Hi, any help would be appreciated, thanks in advance.

Use case: Track the number of resets / reboots after a device is reflashed / deployed and be able to reset the value remotely. I am also looking to do the same with wifi connection drops.

I can solve the reset part using a cloud function but am not having success with the first requirement.

My challenge is with resetting the EEPROM value after reflashing the device as the EEPROM value appears to be retained even after a reflash. I can see that this might be useful under some use cases so my questions are:

  1. is this behaviour correct, ie EEPROM values are not cleared on a reflash?
  2. if the behaviour is correct is there a way to programmatically tell when setup is run for the first time after a reflash or is there a way to clear / set EEPROM addresses to zero when reflashing?

Code used to test:

// Simple EEPROM test script
// Photon, Build 0.4.7
// Test 1: reflash multiples times and EEPROM does not reset - ?
// Test 2: reboot / repower multiple times and 'reboots' increments - CORRECT

#include "application.h"

int addr = 1;
int reboots = 0;
uint8_t val = 0;

void setup() {

  Particle.variable("reboots", &reboots, INT);

  val = EEPROM.read(addr);
  if (val > 250) {
    val = 0;
  } else {
    val++;
  }
  
  Serial.print("val = ");
  Serial.println(val);
  
  EEPROM.update(addr, val);
  reboots = val;

}


void loop() {
    
}

Thanks in advance

OK, just reflashed a completely different app but using similar code and even this does not reset the flash values at address 1. Is this a bug?

EEPROM isn’t reflashed when you reflash an app - the memory is totally separate, just as with the Arduino (if that reference helps.)

This is so that you can update your application yet still maintain the data.

One way to get this

If you define a version number in your code and check if the stored number and the current match you can tell if you are revisiting the same code or not.
Once you got that, store your current version number for next time.

1 Like

Or you can create a particle.function to set it back to zero whenever you want…

1 Like

@mdma - OK, thanks for the clarification - assumption is the mother of all … :smile:.

@ScruffR, @Hootie81, thanks for the suggestions.

1 Like