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 *************************************************//
Just some notes:
this does logically not make sense. In order to connect to the Particle cloud you need the WiFi on before that.
And WiFi.on()
alone does not cause WiFi.ready()
ever to become true. For that you'd need to call WiFi.connect()
- or let Particle.connect()
do that, but as said after you switched the WiFi module on.
Also this is an impossible condition
You can under no circumstance ever be still in the "trying to connect" to the WiFi network state but already connected to Particle cloud.
There is no need to convert event
to String()
. Particle.publish()
will happily accept a C-string as is. And I'd use %.1f
instead of full width floats
via %f
.
And your code is quite convoluted with all these Serial.println()
lines. You can have the same thing happen with adding a set of \r\n
at the end of any actual Serial.print()
statement. And there is also a Serial.printf()
/Serial.printlnf()
that allows you to format your string like with sprintf()
which helps reducing these mulit-line blocks of Serial.print()
statements into a single line of code.
You may also want to add some sanity checks in your Particle.function()
callbacks to guard against invalid command
parameters.
You are also EEPROM.write()
ing a float
but that will give you wrong values in many cases as the second parameter is a uint8_t
.
In your place I'd rather create a struct
that holds your values and use EEPROM.put()
and EEPROM.get()
instead. That also reduces write cycles and hence flash wear.
I'm also not quite getting the use of this statement
The result of that is not 2000 but rather write_time
so what is all that extra effort for?
When you put the values for Photon in, you get
clear = (2047 - 48) - (2047 - 48 - x) =
= 2047 - 48 - 2047 + 48 + x = x
thank you Sir.
I have learnt from you about my mistakes I made.And you are guiding me lot to get better day by day.I am very much thankful to you.
Yes the last statement ‘clear’ should be changed.When I was calculating to erase the memory based on how much time I write to memory.Later I realized that so lenghthy statement was not required.I have changed it already.
One thing I didn’t get that you have told to perform some 'Sanity checks on Particle.function().This point I didn’t get Sir.
I would like to inform you that here in my Particle.function() I am providing upperlimit,lower limit and duration from CLI so the moment the values are sent the Device will reset automatically ,like I have used system.reset() function.Earlier to this whenever the new limit values were updated from CLI I was pressing the RESET butto manually.So to avoid this intervention I have used system.reset().I even tried by calling these Particle Function in void loop if so then no need to call 'system.reset() ’ or press RESET button manually.But to avoid the read cycles I placed the code in SETUP section.
Since the command
parameter for these functions is a string, you could send any "insane" string into your function resulting in a wrong value being stored in EEPROM. To avoid that and the possibly bad side-effects of that you should check the sanity of that parameter and only apply the value when suitable.
I gathered the intent of the System.reset()
already, but I'd not see need for that.
You can apply the new setting without a reset too.
Just change the functions to something like this
const int minRefresh = 10.0;
const int maxRefresh = 3600.0;
int updatetime(String command)
{
int val = command.toInt();
if (minRefresh <= val && val <= maxRefresh)
{
refresh = val;
EEPROM.write(2005, val);
return val; // return the new value and carry on with that
}
return -1; // there was an error
}
Sir I have checked that whenever we use Particle.function() and send Commands since we are storing in EEPROM we need to RESET the device then only the new values will be updated.
If suppose I keep my Particle.function() prototype inside VOID LOOP then no need to RESET.This was the thing I found when I was trying to code the other day.I will try your method and check again Sir.
But this is what I faced while coding and testing
That doesn't make sense.
The same with this
I'm starting to wonder how much programming background actually have.
Not much experience,still a fresher
Since your function name updatetime()
was a bit misleading I had chosen the wrong variables.
I’ve now updated the snippet above - have another look.
I tested the above code ,it worked Sir.
This s the difficulty I faced that passing command value into VOID LOOP so I used EEPROM.write() to save those commands and then read back them.This was my own logic.
But your logic is awesome Sir saving EEPROM write and read cycles.I learnt the way to think in this way too
Thank you sir.
When your variables are declared globally (=outside of any function declaration) you don’t need to pass the values around (let alone go via EEPROM) but when manipulated in one function all other functions will see the new value as soon they access the variable next time.
Then Can I declare those variables as “Static”?
Why would you want to do that?
Static does the same right?
The variable value persists between functions
Nope it does not - that's another unsolicited change that could just cause other troubles. Why do you not just go with the answers you get?
That's all it needs for variables to be visible to all functions in the module.
Declaring a global variable static
in C++ means that each module gets its own instance of the variable which might cause problems when you expect this variable to share the same value between modules.
Have a read