To store data when not in the range of WiFi

My issue is

" I need to store the sensor data when the Particle Photon is out of the WiFi range i.e when the Photon rapidly blinks GREEN.This state in Photon is achieved whenever we switch off our WiFi connection or when we move the Photon device out of WiFi range.I am using SEMI_AUTOMATIC mode.

My problem is to STORE SENSOR DATA DURING THE ABOVE MENTIONED CONDITION i.e WHEN WiFi is SWITCHED OFF.Is that possible with the already available function in the Firmware functions.I am finding difficult in selecting the particular Particle Firmware functions.

But I can store sensor data when WiFi is OFF not when the device is out of WiFi raange.

Please help me to solve this.

Which part are you having difficulties with? Storing the data, or dealing with the blinking green?

Yes, you can store sensor data when you have no WiFi connection or even when you do have a WiFi connection and then just only push that data out at certain times.

There are quite a few other threads on the forum where others have been walked through how to do this and what your different methods are for storing the data.

How much data do you need to store and how frequently?

How often do you plan to be without an internet connection?

2 Likes

I cannot store data when the Photon blinks green i.e when I disconnect my WiFi connectivity

Look into system threading :slight_smile:

I need to store temperature value for every 1 min, 5 min ,10 min or 15 min any duration among these.This duration will be configurable like I am using Particle.function to provide duration using CLI.

Did you get what problem I am facing.Like I can store sensor data when WiFi is switched off on the device but not when I switch off my WiFi router.So when I switch off my WiFi router the photon blinks green rapidly, during this time I need to store data into EEPROM.

Storing into EEPROM is already coded but the "void loop’ is not running when WiFi router is switched OFF

Ok sir,

If I cannot solve I may ask your help

If you don’t manage to solve it after researching the docs, and countless forum topics dealing with this “no internet” state, feel free to ask.

Ok sir,

Thank you

Yes, we get your problem. System Threading will fix this for you, it will allow the connection process to work without affecting your main loop code like it is doing now.

If you are saving data to EEPROM already then adding this line at the top of your code should fix things.

SYSTEM_THREAD(ENABLED);

Sir ,

I am using SEMI_AUTOMATIC mode should I change this to MANUAL mode or will it work with SEMI_AUTOMATIC mode.Because I am at the last stage of my project Sir, and I need it to be finished early.

As we discussed in another thread already. When you get a suggestion don’t divert the discussion away from that suggestion. Just go with it.

Unsolicited changes don’t help building a consistent image of your situation.

2 Likes

Sorry Sir.

Add the suggested code and see if it fixes your problem.

Yes Sir,

I am trying that .It worked with my other example code.Now I am checking with my main code

Hello Sir,

I tested my code with SYSTEM_THREAD(ENABLED) but when WiFi router is switched off , the expected duration to write to EEPROM was 1 minute as this duration was set by me from Particle.function so that writing occurs for every 1minute.

But in my case this is not happening, instead it is taking too long and also its not writing to EEPROM.

Later I changed the duration of writing to 10 seconds then the device is writing to EEPROM for every 1 minute 3 seconds.How come this?

Why is that change in duration?Please help me

If only we had some code to look at…

Also, for debugging purposed, try hard-coding the times first to make sure it’s working, before you start meddling with function calls.

What do you think?

What might be happening in this case?
What might consume the extra time you are seeing used?
If you are using any features that require WiFi/cloud connection, have you guarded them against lost connection?

We don't know your code but you do. And putting 1 and 1 togehter would be the first step in solving that riddle.
We are not here to spoon feed anybody.

Sir,

This is my code.
Currently, the issue is it writes to EEPROM memory during WiFi router is OFF and when the update_time=10 seconds every write completes after 1 minute if 10seconds is my duration of update.

Then when I changed the update_time for 1 minute it takes more time as I calculated it must write for every 6 minutes 30 seconds but it is not.

So is it my update_time affecting my code control?

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

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE (SEMI_AUTOMATIC);


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


char event[100];                    
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=1000,count=60000,write_time=0,i,clear=0,addr=0,update_time,update_time1=10000,oldmemorytime;   //Update reading for  USER GIVEN TIME DURATION
float tempC,upperlimit=5,lowerlimit=2,watch1;
time_t watch;
String status,mytime,newmemorytime;


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




//***************************** setup*****************************************//
void setup() 
{
    
  Serial.begin(9600);    
  pinMode(led, OUTPUT);    // make inbuilt LED to ON
  pinMode(ThermistorPin,INPUT);
  pinMode(button,INPUT_PULLDOWN);
  pinMode(redled1,OUTPUT); // High condition indicator
  pinMode(greenled1,OUTPUT);// Normal condition indicator
  pinMode(blueled1,OUTPUT);  //Low condition indicator
  
  thermistor = new Thermistor(A0, 10000, 4095, 10000, 25, 3923, 5, 20);

//--------------------Particle.functions -----------------------//
  Particle.function("upper",upperthreshold);
  Particle.function("lower",lowerthreshold);
  Particle.function("duration",updatetime);
//---------------------------------------------------------------//  
  
  digitalWrite(redled1,LOW);
  digitalWrite(greenled1,LOW);
  digitalWrite(blueled1,LOW);
  
  upperlimit=EEPROM.read(2003);
  lowerlimit=EEPROM.read(2004);
  refresh=EEPROM.read(2005);
  
 
}


//****************************** loop *****************************************// 


void loop() 
{

  // Particle.connect();
  
  // WiFi.on();  
  
   //*************************Synchronizing the Time with Particle cloud ****************************
            
            if (millis() - lastSync > ONE_DAY_MILLIS)
            {
            // Request time synchronization from the Particle Cloud
              Particle.syncTime();
              
              Serial.println(Time.timeStr());
              lastSync = millis();
            }  
           
           if(Particle.syncTimeDone()==true)
            {
              Serial.println("Time synchronized");
            }    

           if(Particle.syncTimeDone()==false)  
            {
              Serial.println("Time not synchronized");
            }
    
   
   Time.zone(+5.5);    
   Time.format(watch, TIME_FORMAT_DEFAULT);  
   
   watch=Time.now(); 
   mytime=Time.timeStr();
   
   update_time = count*refresh;
   
   Serial.print("Dummy Update time=");
   Serial.print(update_time);
   Serial.println(" ms");

   
   Serial.println();
   Serial.print("Time in Seconds(Normal Value) =");
   Serial.print(watch);
  
   Serial.println();
   Serial.println("=========================================");
   Serial.print("Formatted Real Time =");
   Serial.print(mytime);
   Serial.println();
   Serial.println("=========================================");
   Serial.println();
 
   
     tempC = thermistor->readTempC();
     
    
     Serial.print("Temperature Value without duration=");
     Serial.print(tempC);
     Serial.println("'C");
     Serial.println();
   
     Serial.print("Upper limit =");
     Serial.print(upperlimit);
     Serial.println();
     //delay(1000);
   
     Serial.print("Lower limit =");
     Serial.print(lowerlimit);
     Serial.println();
     //delay(1000);
   
     Serial.print("Duration =");
     Serial.print(refresh);
     Serial.print(" minute/s");
     Serial.println();
   
   if(tempC > upperlimit) 
    {
      digitalWrite(redled1,HIGH);  // Warning High condition
      digitalWrite(greenled1,LOW);
      digitalWrite(blueled1,LOW);
      status = "warning high condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
    }
  
    else if(tempC < lowerlimit)
    {
      digitalWrite(blueled1,HIGH); // Warning Low condition
      digitalWrite(redled1,LOW);
      digitalWrite(greenled1,LOW);
      status ="warning low condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
      
    }    
   
    else
     {
      digitalWrite(greenled1,HIGH); //Good condition
      digitalWrite(redled1,LOW);
      digitalWrite(blueled1,LOW);
      status ="good condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
     }
 delay(5000);


 //****************************WHEN WiFi NOT CONNECTED**************************************//

 //****************************Writing to EEPROM ********************************************//
 
      
   if(WiFi.ready() == false)
   {  
    if(millis()-last_time1 > update_time1)
    {  
         Serial.println();
         Serial.println("System OFF -- WiFi OFF -- Cloud not connected");       
         Serial.println("WiFi and Cloud disconnected");  
  
  
         
         Serial.println(update_time1);   // check the duration when WiFi
         Serial.println();
    
         Serial.println("Ready to Write");
         
         EEPROM.write(addr,tempC);
         ++write_time;
         addr = addr + 1;
         
         watch1=(watch/(255*255*255)); //Divide the bigger seconds value into small value that fits into EEPROM(b/w 0 to 255)
         EEPROM.write(addr,watch1);  //  Saves the corresponding time for the above written sensor data
         ++write_time;        
         addr = addr + 1;
         Serial.print("Writing time =");
         Serial.print(write_time);
         Serial.println();
         Serial.println("Write complete");
         
         clear= ((EEPROM.length()- 48)-(EEPROM.length()- 48 - write_time));   // EEPROM length now is 2000
        
        
        
        
         if (addr >= EEPROM.length()- 2042)   //  writing 3 sensor and timestamp values
         {
          Serial.println("Memory Full");
          write_time=0;
          addr= 0;
          
          digitalWrite(led,HIGH);   //To indicate EEPROM memory cleared
          delay(1000);
          digitalWrite(led,LOW);
          delay(1000);
          
          } 
         
            
    
          Serial.println("------------------------------------------------------------------");
          
          last_time = millis();
    } 
    // millis()-last_time>update_time
          
         
 }  // if ends her
  
   
    
 

 //*****************************************************************************************************//
 //************************************* WHEN WiFi CONNECTED *******************************************//
 /* Two conditions need to be checked:
 1.First read the EEPROM memory for if any sensor values stored for every 5 minutes.
 2.Second if no values present in EEPROM memory read the present sensor value for every 5 minutes.
 3.Thirdly sleep the device during each time lapse of sensor data reading.
 
 */
  
  
  

if(digitalRead(button)==HIGH)   // Button to turn ON WiFi
 {
  Particle.connect();
  WiFi.on();  
  if(WiFi.ready() == true)  
   {
  
    if((WiFi.connecting()==false) && (Particle.connected()==true))
     {
       
       Serial.println("WiFi and Cloud connected"); 
       Serial.println();
      
 //******************************* To read the sensor data from EEPROM memory **********************   
   
    Serial.print("Address value before memory check =");
    Serial.print(addr);
    Serial.println();
    
    
    
     if(EEPROM.read(addr)!=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<clear;)   // read 5 values from memory
            {
                tempC = EEPROM.read(addr);
                addr=addr+ 1;
                
                Serial.print("Address updates  =");
                Serial.print(addr);
                Serial.println();
                
                oldmemorytime= EEPROM.read(addr);  // Stored Time in EEPROM
                newmemorytime= (oldmemorytime*16581375); // 255*255*255
                addr=addr+ 1; 
                
                Serial.print("Address updates  =");
                Serial.print(addr);
                Serial.println();
                Serial.println();
                
                mytime = Time.timeStr();   // This may be Stored time or current time NEED TO CONFIRM
                
                 
                Serial.print("Time stored in EEPROM in seconds ="); 
                Serial.print(newmemorytime);
                Serial.println();
                Serial.println();
            
        
             //PUBLISHING TO GOOGLE SHEETS//
                
                sprintf(event,"%s          %f       %f     %f    %d",newmemorytime.c_str(),tempC,upperlimit,lowerlimit,refresh); // The actual correction by ScruffR
                Particle.publish("Clock_Sensor_Status", String(event));
             
             //===========================//  
                Serial.println("***************** MEMORY VALUES *****************************");
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.print("address=");
                Serial.print(addr);
                Serial.print("  ");
                Serial.println();
                
                Serial.print("Time stored in EEPROM=");
                Serial.print(newmemorytime);
                Serial.print(" ");
                Serial.println();
                
                Serial.print("Memory value=");
                Serial.print(tempC,2);
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.println("***************** MEMORY VALUES *****************************");
             }// for loop
           
  
  //********************* To Clear the EEPROM Memory *************************************     
       
        for (i= 0 ; i< clear; i++) 
          {
            EEPROM.write(i, 0);
          } 
            Serial.println();
            Serial.println("Memory Cleared during WiFi ON,Cloud connected");//To indicate memory clear is completed
            Serial.println();
            digitalWrite(led, HIGH); //To indicate EEPROM memory clear successful 
   
    }  //EEPROM.read(addr)!=0


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//    
//***************************** To read sensor data from Surrounding *************************     
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//          
        
         Serial.print("Reading memory after clear =");
         Serial.println(EEPROM.read(addr));
         Serial.println();
 
         Serial.print("Address before =");
         Serial.print( addr);
         Serial.println();
         Serial.print("Writing time before =");
         Serial.print(write_time);
         Serial.println();
         Serial.println();
         addr=0;
         write_time=0;
         Serial.print("Address after =");
         Serial.print( addr);
         Serial.println();
         Serial.print("Writing time after =");
         Serial.print(write_time);
         Serial.println();
         Serial.println();
         

         
       if(EEPROM.read(addr)==0)
       {
          if(millis()-last_time> update_time)     // read sensor value for USER GIVEN DURATION
          { 
            digitalWrite(led,LOW); 
            
            Serial.println("===============  PRESENT TEMPERATURE STATUS  ========================");
            Serial.println();
            Serial.println("System ON(ambient_condition) -- WiFi ON,Cloud connected");   
            Serial.println();
            Serial.print("Temperature: "); 
            Serial.print(tempC);
            Serial.println("'C");
            
           
            mytime=Time.timeStr();  
            sprintf(event,"%s          %f      %s     %f     %f    %d",mytime.c_str(),tempC,status.c_str(),upperlimit,lowerlimit,refresh); // The actual correction by ScruffR
            Particle.publish("Clock_Sensor_Status", String(event));
  
         
            Serial.println("------------------------------------------------------------------");
         
            last_time = millis(); 
 
    }   //if((WiFi.connecting()==false) && (Particle.connected()==true))
 
        
   }  // last_time = millis(); 

  } // if loop ends here
  
 }
}


if(digitalRead(button)== LOW)
{
    WiFi.off();
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led,LOW);
    delay(1000);
}

}    // main loop ends here


//*************************Particle.function Definitions *******************************//
 /*Here we have 3 Particle.functions exposed namely,
 1.upperthreshold with key upper
 2.lowerthreshold with key lower
 3.updatetime with key duration  
 */
 
// FUNCTION 1 
int upperthreshold(String command)
{
    
    EEPROM.write(2003,command.toInt());  // Crucial conversion
    System.reset();
    return 1 ;
}  

//FUNCTION 2
int lowerthreshold(String command)
{
    
    EEPROM.write(2004,command.toInt());  //Crucial conversion
    System.reset();
    return 2;

}

//FUNCTION 3
int updatetime(String command)
   {
       EEPROM.write(2005,command.toInt());
       System.reset();
       return 3 ;
       
   }

//**************************************Program ends here *************************************************//

Sir,

This is the code I have written to store data when WiFi OFF
Currently, the issue is it writes to EEPROM memory during WiFi router is OFF and when the update_time=10 seconds every write completes after 1 minute if 10seconds is my duration of update.

Then when I changed the update_time for 1 minute it takes more time as I calculated it must write for every 6 minutes 30 seconds but it is not.

So is it my update_time affecting my code control?


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

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE (SEMI_AUTOMATIC);


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


char event[100];                    
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=1000,count=60000,write_time=0,i,clear=0,addr=0,update_time,update_time1=10000,oldmemorytime;   //Update reading for  USER GIVEN TIME DURATION
float tempC,upperlimit=5,lowerlimit=2,watch1;
time_t watch;
String status,mytime,newmemorytime;


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




//***************************** setup*****************************************//
void setup() 
{
    
  Serial.begin(9600);    
  pinMode(led, OUTPUT);    // make inbuilt LED to ON
  pinMode(ThermistorPin,INPUT);
  pinMode(button,INPUT_PULLDOWN);
  pinMode(redled1,OUTPUT); // High condition indicator
  pinMode(greenled1,OUTPUT);// Normal condition indicator
  pinMode(blueled1,OUTPUT);  //Low condition indicator
  
  thermistor = new Thermistor(A0, 10000, 4095, 10000, 25, 3923, 5, 20);

//--------------------Particle.functions -----------------------//
  Particle.function("upper",upperthreshold);
  Particle.function("lower",lowerthreshold);
  Particle.function("duration",updatetime);
//---------------------------------------------------------------//  
  
  digitalWrite(redled1,LOW);
  digitalWrite(greenled1,LOW);
  digitalWrite(blueled1,LOW);
  
  upperlimit=EEPROM.read(2003);
  lowerlimit=EEPROM.read(2004);
  refresh=EEPROM.read(2005);
  
 
}


//****************************** loop *****************************************// 


void loop() 
{

  // Particle.connect();
  
  // WiFi.on();  
  
   //*************************Synchronizing the Time with Particle cloud ****************************
            
            if (millis() - lastSync > ONE_DAY_MILLIS)
            {
            // Request time synchronization from the Particle Cloud
              Particle.syncTime();
              
              Serial.println(Time.timeStr());
              lastSync = millis();
            }  
           
           if(Particle.syncTimeDone()==true)
            {
              Serial.println("Time synchronized");
            }    

           if(Particle.syncTimeDone()==false)  
            {
              Serial.println("Time not synchronized");
            }
    
   
   Time.zone(+5.5);    
   Time.format(watch, TIME_FORMAT_DEFAULT);  
   
   watch=Time.now(); 
   mytime=Time.timeStr();
   
   update_time = count*refresh;
   
   Serial.print("Dummy Update time=");
   Serial.print(update_time);
   Serial.println(" ms");

   
   Serial.println();
   Serial.print("Time in Seconds(Normal Value) =");
   Serial.print(watch);
  
   Serial.println();
   Serial.println("=========================================");
   Serial.print("Formatted Real Time =");
   Serial.print(mytime);
   Serial.println();
   Serial.println("=========================================");
   Serial.println();
 
   
     tempC = thermistor->readTempC();
     
    
     Serial.print("Temperature Value without duration=");
     Serial.print(tempC);
     Serial.println("'C");
     Serial.println();
   
     Serial.print("Upper limit =");
     Serial.print(upperlimit);
     Serial.println();
     //delay(1000);
   
     Serial.print("Lower limit =");
     Serial.print(lowerlimit);
     Serial.println();
     //delay(1000);
   
     Serial.print("Duration =");
     Serial.print(refresh);
     Serial.print(" minute/s");
     Serial.println();
   
   if(tempC > upperlimit) 
    {
      digitalWrite(redled1,HIGH);  // Warning High condition
      digitalWrite(greenled1,LOW);
      digitalWrite(blueled1,LOW);
      status = "warning high condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
    }
  
    else if(tempC < lowerlimit)
    {
      digitalWrite(blueled1,HIGH); // Warning Low condition
      digitalWrite(redled1,LOW);
      digitalWrite(greenled1,LOW);
      status ="warning low condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
      
    }    
   
    else
     {
      digitalWrite(greenled1,HIGH); //Good condition
      digitalWrite(redled1,LOW);
      digitalWrite(blueled1,LOW);
      status ="good condition";
      Serial.print("Status =");
      Serial.print(status);
      Serial.println();
     }
 delay(5000);


 //****************************WHEN WiFi NOT CONNECTED**************************************//

 //****************************Writing to EEPROM ********************************************//
 
      
   if(WiFi.ready() == false)
   {  
    if(millis()-last_time1 > update_time1)
    {  
         Serial.println();
         Serial.println("System OFF -- WiFi OFF -- Cloud not connected");       
         Serial.println("WiFi and Cloud disconnected");  
  
  
         
         Serial.println(update_time1);   // check the duration when WiFi
         Serial.println();
    
         Serial.println("Ready to Write");
         
         EEPROM.write(addr,tempC);
         ++write_time;
         addr = addr + 1;
         
         watch1=(watch/(255*255*255)); //Divide the bigger seconds value into small value that fits into EEPROM(b/w 0 to 255)
         EEPROM.write(addr,watch1);  //  Saves the corresponding time for the above written sensor data
         ++write_time;        
         addr = addr + 1;
         Serial.print("Writing time =");
         Serial.print(write_time);
         Serial.println();
         Serial.println("Write complete");
         
         clear= ((EEPROM.length()- 48)-(EEPROM.length()- 48 - write_time));   // EEPROM length now is 2000
        
        
        
        
         if (addr >= EEPROM.length()- 2042)   //  writing 3 sensor and timestamp values
         {
          Serial.println("Memory Full");
          write_time=0;
          addr= 0;
          
          digitalWrite(led,HIGH);   //To indicate EEPROM memory cleared
          delay(1000);
          digitalWrite(led,LOW);
          delay(1000);
          
          } 
         
            
    
          Serial.println("------------------------------------------------------------------");
          
          last_time = millis();
    } 
    // millis()-last_time>update_time
          
         
 }  // if ends her
  
   
    
 

 //*****************************************************************************************************//
 //************************************* WHEN WiFi CONNECTED *******************************************//
 /* Two conditions need to be checked:
 1.First read the EEPROM memory for if any sensor values stored for every 5 minutes.
 2.Second if no values present in EEPROM memory read the present sensor value for every 5 minutes.
 3.Thirdly sleep the device during each time lapse of sensor data reading.
 
 */
  
  
  

if(digitalRead(button)==HIGH)   // Button to turn ON WiFi
 {
  Particle.connect();
  WiFi.on();  
  if(WiFi.ready() == true)  
   {
  
    if((WiFi.connecting()==false) && (Particle.connected()==true))
     {
       
       Serial.println("WiFi and Cloud connected"); 
       Serial.println();
      
 //******************************* To read the sensor data from EEPROM memory **********************   
   
    Serial.print("Address value before memory check =");
    Serial.print(addr);
    Serial.println();
    
    
    
     if(EEPROM.read(addr)!=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<clear;)   // read 5 values from memory
            {
                tempC = EEPROM.read(addr);
                addr=addr+ 1;
                
                Serial.print("Address updates  =");
                Serial.print(addr);
                Serial.println();
                
                oldmemorytime= EEPROM.read(addr);  // Stored Time in EEPROM
                newmemorytime= (oldmemorytime*16581375); // 255*255*255
                addr=addr+ 1; 
                
                Serial.print("Address updates  =");
                Serial.print(addr);
                Serial.println();
                Serial.println();
                
                mytime = Time.timeStr();   // This may be Stored time or current time NEED TO CONFIRM
                
                 
                Serial.print("Time stored in EEPROM in seconds ="); 
                Serial.print(newmemorytime);
                Serial.println();
                Serial.println();
            
        
             //PUBLISHING TO GOOGLE SHEETS//
                
                sprintf(event,"%s          %f       %f     %f    %d",newmemorytime.c_str(),tempC,upperlimit,lowerlimit,refresh); // The actual correction by ScruffR
                Particle.publish("Clock_Sensor_Status", String(event));
             
             //===========================//  
                Serial.println("***************** MEMORY VALUES *****************************");
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.print("address=");
                Serial.print(addr);
                Serial.print("  ");
                Serial.println();
                
                Serial.print("Time stored in EEPROM=");
                Serial.print(newmemorytime);
                Serial.print(" ");
                Serial.println();
                
                Serial.print("Memory value=");
                Serial.print(tempC,2);
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.println();
                Serial.println("***************** MEMORY VALUES *****************************");
             }// for loop
           
  
  //********************* To Clear the EEPROM Memory *************************************     
       
        for (i= 0 ; i< clear; i++) 
          {
            EEPROM.write(i, 0);
          } 
            Serial.println();
            Serial.println("Memory Cleared during WiFi ON,Cloud connected");//To indicate memory clear is completed
            Serial.println();
            digitalWrite(led, HIGH); //To indicate EEPROM memory clear successful 
   
    }  //EEPROM.read(addr)!=0


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//    
//***************************** To read sensor data from Surrounding *************************     
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//          
        
         Serial.print("Reading memory after clear =");
         Serial.println(EEPROM.read(addr));
         Serial.println();
 
         Serial.print("Address before =");
         Serial.print( addr);
         Serial.println();
         Serial.print("Writing time before =");
         Serial.print(write_time);
         Serial.println();
         Serial.println();
         addr=0;
         write_time=0;
         Serial.print("Address after =");
         Serial.print( addr);
         Serial.println();
         Serial.print("Writing time after =");
         Serial.print(write_time);
         Serial.println();
         Serial.println();
         

         
       if(EEPROM.read(addr)==0)
       {
          if(millis()-last_time> update_time)     // read sensor value for USER GIVEN DURATION
          { 
            digitalWrite(led,LOW); 
            
            Serial.println("===============  PRESENT TEMPERATURE STATUS  ========================");
            Serial.println();
            Serial.println("System ON(ambient_condition) -- WiFi ON,Cloud connected");   
            Serial.println();
            Serial.print("Temperature: "); 
            Serial.print(tempC);
            Serial.println("'C");
            
           
            mytime=Time.timeStr();  
            sprintf(event,"%s          %f      %s     %f     %f    %d",mytime.c_str(),tempC,status.c_str(),upperlimit,lowerlimit,refresh); // The actual correction by ScruffR
            Particle.publish("Clock_Sensor_Status", String(event));
  
         
            Serial.println("------------------------------------------------------------------");
         
            last_time = millis(); 
 
    }   //if((WiFi.connecting()==false) && (Particle.connected()==true))
 
        
   }  // last_time = millis(); 

  } // if loop ends here
  
 }
}


if(digitalRead(button)== LOW)
{
    WiFi.off();
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led,LOW);
    delay(1000);
}

}    // main loop ends here


//*************************Particle.function Definitions *******************************//
 /*Here we have 3 Particle.functions exposed namely,
 1.upperthreshold with key upper
 2.lowerthreshold with key lower
 3.updatetime with key duration  
 */
 
// FUNCTION 1 
int upperthreshold(String command)
{
    
    EEPROM.write(2003,command.toInt());  // Crucial conversion
    System.reset();
    return 1 ;
}  

//FUNCTION 2
int lowerthreshold(String command)
{
    
    EEPROM.write(2004,command.toInt());  //Crucial conversion
    System.reset();
    return 2;

}

//FUNCTION 3
int updatetime(String command)
   {
       EEPROM.write(2005,command.toInt());
       System.reset();
       return 3 ;
       
   }










//**************************************Program ends here *************************************************//