Time.timeStr() returning wrong data? Anybody else?

I’m using this function to display the current time Time.timeStr()

It was working just fine for some time but all the sudden today it started returning wrong info:

Serial.println(Time.timeStr()); // Wed May 21 01:08:47 2014

Here is what is being output from the Photon,

This wrong date started being output at around 12PM Sunday November 22 for some reason. It was working perfectly fine for about 4 days before that.

Does anybody else see this same issue?

@RWB, could your Photon have lost powe and reset and not resync its RTC to the cloud? Do you wait in your code for the time to sync before continuing to your loop?

@peekay123

It’s running on a 1200mAh LiPo battery, it’s been working fine for probably a week now.

It did loose power the other day but it started back up after I started charging the battery and the time was fine after that also so the power loss did not immediately start affecting the time output.

Here is the code.

// This #include statement was automatically added by the Spark IDE.
#include "SHT1x/SHT1x.h"

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);

int temp_f;
int humidity;




int LED = D7;

void setup()
{
    pinMode(LED, OUTPUT);
    
    Serial.begin(9600); // Open serial connection to report values to host
    
    Serial.println("Starting up");
    
    Time.zone(-5);

}

void loop()
{
    //float temp_f;
    //float humidity;
    
    // Read values from the sensor
    
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();
    
    
    
    Particle.publish("TempSensor1", String::format("Temp=%dF; Humidity=%d%%; Time= %s", temp_f, humidity, Time.timeStr().c_str()));
    
    // Print the values to the serial port
    Serial.print("Temperature: ");
    Serial.print(temp_f, 1);
    Serial.print("F. Humidity: ");
    Serial.print(humidity);
    Serial.println("%");
    
    Serial.println(Time.timeStr()); // Wed May 21 01:08:47 2014
    
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    
    System.sleep(SLEEP_MODE_DEEP, 300);
    //delay(60000);
}

@RWB, I’m not sure what happened but you might want to add this in your setup, before the Time.zone(-5); call:

while(Time.year() <= 1970)
{
Spark.syncTime();  
Spark.process();
delay(100);
}

This will ensure you have a valid time in the RTC before you continue.

1 Like

@peekay123

You Da Man :smiley:

I’m not really sure why this started hapenning all the sudden but its fixed now.

Now the next thing that needs fixed is to that if the Photon can’t find a WiFi connection it just stays on until it finds one even it takes 24 hours for me to come home with my phones WiFi. This kills the battery quickly. I need to set this up so if there is no WiFi connection detected in 15 seconds then it should go back to deep sleep and try again 5 mins later vs just staying on and killing the battery.

I’ll read the docs to figure this out but if you know the best way to do this then fell free to share how you would do it.

@RWB, you will need to use SYSTEM_MODE(MANUAL) for that so you have full control of both wifi and cloud connections. You could use waitFor() with a timeout in combination with WiFi.ready() for example. :smile:

@peekay123

Yea I was reading the docs and saw there are these Wifi.ready() functions that can be used to accomplish what I’m wanting to do.

I just haven’t gotten to coding that but now that I see the temp logger that is supposed to run 45 days only last a few days due to the WiFi connection not being available all the time it’s clear the manual WiFi modes are needed.s

As always, Thanks for your help! You guys make life so much easier :smiley:

1 Like