Declaring a variable for timeStr()?

I’m setting up a system to monitor water tanks back to a web page. I have a lot of the Photon FW and even some of the html/js figured out. I would like to pass a timestamp from Photon back to the web page so the user can see if there has been any significant delay since the last update. I don’t need to do any manipulation. Just read it and pass it.

There may be a better way to do this. If so, I am open to suggestions,but if not, I am having a hard time figuring out how to declare a variable to receive timeStr() so I can pass it back to the web page.

As far as I can tell from reading everything I can find - timeStr() returns a string. How do you declare a string variable on the Photon? Or, how can I set up a variable to receive timeStr()? I have tried declaring various forms of char, but get compile error messages complaining about mixing char and string variables.

Thanks for your help

Not directly answering the question, but if you’re using the publish system, there’s a time-stamp included.
As far as strings go: https://docs.particle.io/reference/firmware/photon/#string-char-array

Thanks Moors7. I should have been more clear. I’m passing the variable using Particle.variable() in the FW and using $.get in the js code.

After looking again more closely at your link, I found that if I declared char CheckTime[26]; then did String CheckTime = Time.timeStr(); it actually compiled.

Now back to html/java to see how the variable shows up there.

Thanks again.

@Cloudy,

Did this work for you using a Particle.variable? It compiles for me as well but the Particle.variable() does not return the time.

How are you trying?

Particle.variable() cannot expose the return value of a function call.
It’s only meant to expose the values of global variables (strings, int and double - I’ve learnt some time ago - although undocumented - console also understands bool).

@ScruffR,

As you know, I am working to add code to my devices to change with the upcoming switch away from Daylight Savings Time. I would like to have a variable I could check to ensure that the device is on the right time. Ideally, this would be a Particle.variable() I could query.

First, I tried the approach in this thread.

I defined a global variable - char CheckTime[26];
defined a Particle.variable("Offset", CheckTime);
and in Setup, I calculated the value - String CheckTime = Time.timeStr();

When I accessed this variable in the console - it had no value. This is why I posted my question to @Cloudy.

What ended up working was:

I defined a global variable - char currentOffsetStr[10];
defined a Particle.variable("Offset", currentOffsetStr);
In setup, I have these two lines:

float currentOffset = (Time.local() - Time.now()) / 3600.0;
snprintf(currentOffsetStr,sizeof(currentOffsetStr),"%2.1f UTC",currentoffset);

This gives a value of “-4 UTC” which is the correct value.

Open to suggestions on why the first approach does not work.

Thanks,

Chip

If you did this, then you have created a local variable which hid the global variable and hence the global never actually got set.
Just leave out the String (variable type introducing the redeclaration) in your assignment.

// either (my preference avoiding String)
char CheckTime[26];
void setup() {
  strcpy(CheckTime, Time.timeStr());
  Particle.variable("Offset", CheckTime);
}
// or when using String (... shudder ... ;-)
String CheckTime;
void setup() {
  CheckTime = Time.timeStr();
  Particle.variable("Offset", CheckTime);
}

1 Like

@ScruffR,

Thank you, I appreciate you setting me straight - again. I also appreciate the reminder not to use String.

Chip

1 Like