Getting time from Photon to html

I’m not sure exactly what happened, but I solved my earlier data passing problem. “Can’t pass data to web page”. Now I just can’t figure out how to pass a variable String CheckTime = Time.timeStr();

I have spent a lot of time with bko’s great tutorial “Reading Spark variables with your own HTML File”, but can’t seem to get the variable passed. His tutorial did bring up a more fundamental question. In my project, I will be using deep sleep to preserve battery life. On my web page, I want to be sure it has come back from sleep and I have a current response from the Photon.

Here is how I see the data passing -please correct me if I am wrong here - The Photon will wake up a send data to the particle.io cloud. The last data sits there until it is updated again by the Photon. In the mean time, I can jquery the particle.io cloud for the latest data set even if the device is currently sleeping. If this isn’t right - i.e. if the query won’t work if the Photon is sleeping - I’ll have to use another approach and a pointer to some info do do this would be appreciated.

Another basic question: Does the json.coreInfo.last_heard provide reliable info on whether the Photon (I’m assuming there is a Photon analog for this “core” function.) is truly alive and transmitting? Could I use this reliably instead of a time stamp from the Photon? Again, the Photon may be asleep when the web page does its query, but the most recent time stamp will be an indicator of if it is responding.

If I do need to get the time from the Photon, I’m now at the purpose of my question. I have had no luck trying to read a Photon variable that holds Time.timestr(). Here is my Photon code:

    /*4/3/17 added the time stamp. Now back to html to figure out how to read and display it
    04/01/17 Getting percentfull to work with web page
    4/2/17 Working with Jeff this now works

    It is only the bare minimum to get the html file with gauges working*/

    // This #include statement was automatically added by the Particle IDE.
    #include <Particle.h>

    double emptydepth = 67.0; //Depth of tank in inches

    int maxbotix = A0; // This is the Maxbotix Analog Data out. 

    int analogvalue = 0; // Declaring the integer variable analogvalue, which we will use later to store the value from the Maxbotix.
    int range = 0; //Declaring a variable that will be the calulated range in inches

    int percentfull = 0; //Variable to hold % full

    float Vin = 3.3; // Using 3.3 V from the Battery Shield
    float constant = (3.3 / 4095) / (Vin / 512); // changed from 1024 to 512 for Max EZ
    //NOTE: New WR7060 reads in cm => Vcc/1024 per cm
    int correction = 8; // gap from the sensor to the top of the water in a full tank. Used in the map() function

    char CheckTime[26]; 
    // Next we go into the setup function.
    void setup() {

        pinMode(maxbotix,INPUT);  // reading the Maxbotix)
        
        Serial.begin(9600);
        // This Particle.variable def allows html to get variable from Particle cloud
        Particle.variable("Full", percentfull);
        Particle.variable("TimeNow", CheckTime);  
        
        Time.zone(-6);
    }

    // Next is the loop function...

    void loop() {

         
        analogvalue = analogRead(maxbotix);
        range = analogvalue * constant; 
        
        if (range <= emptydepth) {
        percentfull = map(range, 8, emptydepth, 100, 0);
        }
        // Added to account for screwy readings when targe is very close to sensor
        if (range > emptydepth) { //
            percentfull = 100;
        }
        
    	String CheckTime = Time.timeStr();		
    	//Inserted to test time string
    	Particle.publish("Time", CheckTime); 
    	
    	//Particle.publish("% Full", String(percentfull));
    	Serial.print("% Full = ");
    	Serial.print(percentfull);
    	Serial.print("  analogvalue = ");
    	Serial.println(analogvalue);
    	
    	delay(5000);

      
    }

Ignore the analog stuff. That is residue from my actual project. I have tried to strip out unnecessary stuff but left the setup. Also, the sketch does send the CheckTime to the console just fine.

And here is my copy of bko’s html/java updated to include my time variable in place of his temp.

<!DOCTYPE HTML>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"type="text/javascript" charset="utf-8"></script>
<body>
    <span id="Time"></span><br>
    <span id="tstamp"></span><br>

    <button id="connectbutton" onclick="start()">Read Time</button>

    <script type="text/javascript">

    function start(objButton) {

        document.getElementById("Time").innerHTML = "Waiting for data...";
        document.getElementById("tstamp").innerHTML ="";
        var deviceID = "mine";
        var accessToken = "mine";
        var varName = "TimeNow";

        requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("Time").innerHTML = json.result;
                 document.getElementById("Time").style.fontSize = "28px";
                 document.getElementById("tstamp").innerHTML = json.coreInfo.last_heard;
                 });
    }
    </script>
</body>
</html>

When I run this, the tstamp shows, but not my Time. I think I must not be understanding something about the time string and how it gets passed. Any help would be greatly appreciated.

Thank you,

Nope it doesn’t.
Particle.variables() can only be requested while the device is online and Particle.publish() is fire and forget, so you need to have a subscription set up when the publish happens.

1 Like

ScruffR - not what I wanted to hear, but information that will save headaches down the road.

I’ve been doing all my testing with the Photon looping so, in this case, that isn’t the problem. However, for my application, I need it to deep sleep to preserve the battery. I guess this means rather than querying from the html page, I’ll need to push my 2 or 3 variables from the Photon when it wakes up - right? If you have any pointers to examples of doing it this way, I’d appreciate it.

Thanks,

Hello. I am also getting the UTC time when I run this code. My time zone is 5 hours behind. How can I make the html page show this based on my Current. time?

In the OP, even though they set Time.zone(), the time is retrieved using Time.timeStr() which returns UTC time not local time. Try using Time.local().timeStr() instead.

Thanks for your reply.

I will give it a try. Thanks again!

I’d have to double check, but AFAIK Time.local() does not return a Time object.

However, Time.timeStr() should actually give you the local representation of the current time.

There is an opposite issue though that points out that it’s “impossible” to get the UTC representation of the time with timeStr()


and related to that

1 Like

I wasn’t too sure about the timeStr() component. The docs don’t include much about it. I just saw that logically, the Time didn’t have any modifiers such as local() or format() so it must be returning UTC.

As the first issue I linked above illustrates, there currently is no way to get anything but local time from these functions.