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