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 *************************************************//