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,