Storing and Retrieving data using EEPROM

// This #include statement was automatically added by the Particle IDE.
#include <photon-thermistor.h>

#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000) // Milliseconds MACROS for 1 day


SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);


char event[100];                                    //standard C string.Then convert this Standard C string into String(event) to publish on google sheets
int led = D7,redled1=D3,greenled1=D2,blueled1=D1,hold;                       // The on-board LED
int ThermistorPin=A0,button= D6;
unsigned long last_time=0,lastSync,last_time1=0;
int refresh,count=60000,write_time=0,i,clear=0,addr=0,update_time=60000,update_time1= 5000,stored_Timestamp,temp_count=0;   //Update reading for  USER GIVEN TIME DURATION
float tempC,upperlimit,lowerlimit,watch1;
int watch,a=0;
String status,mytime;


Thermistor *thermistor;  // pointer variable to the Thermistor library function



void setup() {



pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(button,INPUT_PULLDOWN);
Serial.begin(9600);


 
 thermistor = new Thermistor(A0, 10000, 4095, 10000, 25, 3923, 5, 20);

//Particle.function("upper",upperthreshold);
//Particle.function("lower",lowerthreshold);
//Particle.function("duration",updatetime);


  upperlimit=EEPROM.read(2003);
  lowerlimit=EEPROM.read(2004);
  refresh=EEPROM.read(2005);
 
 


}

void loop() {
     
     Particle.syncTime();
     Time.zone(+5.5);    
     Time.format(watch, TIME_FORMAT_DEFAULT);  
  //  tempC = thermistor->readTempC();
   
     if(digitalRead(button)== HIGH)
     {
         Particle.connect();
         
      
        if(Particle.connected()== true)
         {
         Particle.process();
         //Particle.syncTime();
          if(addr != 0)
        {
          addr = 0;
          
        }
         
         Serial.printlnf("Address value before memory read = %d",addr); 
         Serial.printlnf("Value stored in first location =%d",EEPROM.read(addr));
         Serial.println();
         delay(1000);
         
      
          
          if((addr == 0) && (EEPROM.read(0)!=0))  // check EEPROM ,verify memory for any sensor values present 
         {  
          
          for(addr=0;addr< 19;){
         // Particle.process();
             if(millis()- last_time1 > update_time1)
              {   
               Serial.println("System ON(Memory_values_present) -- WiFi ON");  
               Serial.println();
                
                Serial.printlnf("Temperature Value stored in EEPROM memory = %d",EEPROM.read(addr));
                Serial.println();
                addr=addr+ 1;
          
                Serial.print("Now the address location after reading Temperature value =");
                Serial.print(addr);
                Serial.println();
                Serial.println();
                //stored_Timestamp = EEPROM.get(addr,watch);
                Serial.printlnf("Time stored in EEPROM memory = %d",EEPROM.get(addr,a));
                addr=addr+ 1; 
                Serial.println();
                
                Serial.print("Now the Address location after reading Timestamp =");
                Serial.print(addr);
                Serial.println();
                Serial.println();
          
                last_time1 = millis();
                } 
             } 
             
               for(i=0;i<19;i++)
                { 
                    
                    EEPROM.write(i,0);
                    
                    
                    
                }
                
                addr = 0;
            } 
                
             
             
             
                if(EEPROM.read(0) == 0)
                {
                    if(millis()-last_time1 > update_time1)
                    {
                    Serial.printlnf("Ambient Temperature is = %f",tempC);
                    delay(1000);
                    
                    last_time1 = millis();
                    }
                }
                
 
             
            
 
         }
         }   
     
     
    
 
 
 //-----------------------------------------------------------------------------------------------------------------------//   
    
    if(Particle.connected() == false)
        {
      tempC = thermistor->readTempC();
      watch = Time.now();    
     
      Serial.printlnf("Value of address location before write = %d",addr);
      delay(5000);
      
      if (addr >= 0){
      if(millis()-last_time> update_time)
      {
         Serial.println();
         Serial.println("System OFF -- WiFi OFF -- Cloud not connected");       
         
         Serial.printlnf("Value of address location when wifi off = %d",addr);
         Serial.printlnf("Temperature value = %f",tempC);
         
         
         EEPROM.write(addr,tempC);
         
         Serial.printlnf("Temperature value stored when wifi off = %f",EEPROM.read(addr));
         ++write_time;
         addr = addr + 1;
         
        a= EEPROM.put(addr,watch);  //  Saves the corresponding time for the above written sensor data
         Serial.printlnf("Year=%d Month=%d Day=%d Weekday=%d Hour=%d Minute=%d Seconds=%d",Time.year(watch),Time.month(watch),Time.day(watch),Time.weekday(watch),Time.hour(watch),Time.minute(watch),Time.second(watch));
         
         ++write_time;        
         addr = addr + 1;
          
          
         Serial.printlnf("Timestamp stored when WiFi off = %d",a);
         Serial.printlnf("Year=%d Month=%d Day=%d Weekday=%d Hour=%d Minute=%d Seconds=%d",Time.year(watch),Time.month(watch),Time.day(watch),Time.weekday(watch),Time.hour(watch),Time.minute(watch),Time.second(watch));
         
         Serial.print("Writing time =");
         Serial.print(write_time);
         Serial.println();
         Serial.println("Write complete");
         
          last_time = millis();
          }
        }  
         
       }
         
         
         
         
}

Hello

Above there is my code to store and retrieve the Timestamp and Temperature data.

My problem is the device is storing the last updated value of timestamp but not the old values like when WiFi connection goes off the device stores both sensor and timestamp data if only one reading is taken it shows exact stored values, but if I store more than both data’s more than once only the sensor value is been read correctly but not the timestamp.

Could you please help me where would I have gone wrong.Or is there any problem with the storing data’s in EEPROM?.Waiting for your reply

You have asked that same question in a Private Message, so I repeat the answer here too and point out that double posting - private or public - is NOT appreciated as it consumes multiple people’s time repeatedly.
BTW, that same code also comes up in several other threads of yours and still does not incorporate the tips you got there (e.g. use a struct and EEPROM.put()/EEPROM.get() for your data).


I think it is up to you to do some more debugging first.
Check what you write, check what you get, think why this doesn’t match up, provide your data and the conclusions you drew from that.

You are using incremental addresses (addr = addr + 1) and Time.now() does give you a UNIX epoch time stamp, you need to read up on what that is and why this will not work as you expect.

Hint: You are only writing/reading one byte to/from EEPROM.

1 Like

I got your question Sir.

I want to store the Timestamp and its corresponding sensor data recorded.I am able to store both of them and retrieve both of them if only I store once.

Suppose if I repeat the cycle to store both the Timestamp and Sensor data when WiFi off continuously like 3 sensor values and its corresponding 3 Timestamp.

But while retrieving I am able to get only the last stored value i.e, the value stored before the WiFi connection establishment.Here in this case I am able to read the third value of Timestamp and one more thing is Sensor values are retrieved perfectly but not Timestamp.

I have debugged lot Sir.But could not make out.

Now I have still fine tuned my code so that one can easily see the difference:
The code goes here---------

// This #include statement was automatically added by the Particle IDE.
#include <photon-thermistor.h>

#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000) // Milliseconds MACROS for 1 day


SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);


char event[100];                                    //standard C string.Then convert this Standard C string into String(event) to publish on google sheets
int led = D7,redled1=D3,greenled1=D2,blueled1=D1,hold;                       // The on-board LED
int ThermistorPin=A0,button= D6;
unsigned long last_time=0,lastSync,last_time1=0;
int refresh,count=60000,write_time=0,i,clear=0,addr=0,update_time=20000,update_time1= 1000,stored_Timestamp,temp_count=0;   //Update reading for  USER GIVEN TIME DURATION
float tempC,upperlimit,lowerlimit,watch1;
int watch,a;
String status,mytime;


Thermistor *thermistor;  // pointer variable to the Thermistor library function



void setup() {



pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(button,INPUT_PULLDOWN);
Serial.begin(9600);


 
 thermistor = new Thermistor(A0, 10000, 4095, 10000, 25, 3923, 5, 20);

//Particle.function("upper",upperthreshold);
//Particle.function("lower",lowerthreshold);
//Particle.function("duration",updatetime);


  upperlimit=EEPROM.read(2003);
  lowerlimit=EEPROM.read(2004);
  refresh=EEPROM.read(2005);
 
 


}

void loop() {
     
     Particle.syncTime();
     Time.zone(+5.5);    
     Time.format(watch, TIME_FORMAT_DEFAULT);  
  //  tempC = thermistor->readTempC();
   
     if(digitalRead(button)== HIGH)
     {
         Particle.connect();
         
      
        if(Particle.connected()== true)
         {
         Particle.process();
         //Particle.syncTime();
               
          if(addr != 0)
        {
          addr = 0;
          
        }
         
         Serial.printlnf("Address value before memory read = %d",addr); 
         Serial.printlnf("Value stored in first location =%d",EEPROM.read(addr));
         Serial.println();
         delay(1000);
         
      
          
          if((addr == 0) && (EEPROM.read(0)!=0))  // check EEPROM ,verify memory for any sensor values present 
         {  
               Serial.println("System ON(Memory_values_present) -- WiFi ON");  
               Serial.println();
          for(addr=0;addr< 19;){
         // Particle.process();
           
             if(millis()- last_time1 > update_time1)
              {   
               
                
                Serial.printf("Temperature stored= %d",EEPROM.read(addr));
                addr=addr+ 1;
                Serial.print("\t");
                Serial.printf("Time stored = %d",a=EEPROM.get(addr,watch));
                addr=addr+ 1; 
                Serial.println();
          
                last_time1 = millis();
                } 
             } 
             
               
               for(i=0;i<19;i++)
                { 
                    
                    EEPROM.write(i,0);
                    
                    
                    
                }
                
                addr = 0;
            } 
                
             Serial.printlnf("value stored =%d",a);
             
             
                if(EEPROM.read(0) == 0)
                {
                    if(millis()-last_time1 > update_time1)
                    {
                    Serial.printlnf("Ambient Temperature is = %f",tempC);
                    delay(1000);
                    
                    last_time1 = millis();
                    }
                }
                
 
             
            
 
         }
         }   
     
     
    
 
 
 //-----------------------------------------------------------------------------------------------------------------------//   
    
    if(Particle.connected() == false)
        {
      tempC = thermistor->readTempC();
      watch = Time.now();    
     
      Serial.printlnf("Value of address location before write = %d",addr);
      delay(5000);
      
      if (addr >= 0){
      if(millis()-last_time> update_time)
      {
         Serial.println();
         Serial.println("System OFF -- WiFi OFF -- Cloud not connected");       
         
         Serial.printlnf("Value of address location when wifi off = %d",addr);
         Serial.printlnf("Temperature value = %f",tempC);
         
         
         EEPROM.write(addr,tempC);
         
         Serial.printlnf("Temperature value stored when wifi off = %f",EEPROM.read(addr));
         ++write_time;
         addr = addr + 1;
         
         EEPROM.put(addr,watch);  //  Saves the corresponding time for the above written sensor data
         Serial.printlnf("Year=%d Month=%d Day=%d Weekday=%d Hour=%d Minute=%d Seconds=%d",Time.year(watch),Time.month(watch),Time.day(watch),Time.weekday(watch),Time.hour(watch),Time.minute(watch),Time.second(watch));
         
         ++write_time;        
         addr = addr + 1;
          
          
         Serial.printlnf("Timestamp stored when WiFi off = %d",watch);
         Serial.printlnf("Year=%d Month=%d Day=%d Weekday=%d Hour=%d Minute=%d Seconds=%d",Time.year(watch),Time.month(watch),Time.day(watch),Time.weekday(watch),Time.hour(watch),Time.minute(watch),Time.second(watch));
         
         Serial.print("Writing time =");
         Serial.print(write_time);
         Serial.println();
         Serial.println("Write complete");
         
          last_time = millis();
          }
        }  
         
       }
         
         
         
         
}