Hi there. I have swapped out a photon for a core in a project that I have had running for a couple of months now. After modifying the onewire and dallastemp libraries I have gotten the code to compile and work seemingly fine except it fails to publish after its first round post bootting. That is if I reboot I get one set of data on dashboard.particle.io or by subscribing from the cli. Since the code work flawlessly on my core I am wondering if I am missing something that is photon specific. Also BTW have the core running a different task on the same network now and have had not problems with publishing. Thanks in advance for your help.
@cdrodriguez, when you say quit, can you be more specific to what happened?
Also, if your code can be shared, that will help a lot
Hi Kenneth. Thanks so much for responding. What I mean by quit is that it would not publish anything more, everything else continued to worked fine, e.g. spark.variable, spark.function. As for the code here is an excerpt:
//Spark Publish Variables and settings
int TTL = 60; // time to live
unsigned long LastPublish = Time.now()-TTL;
int TTL = 60; // time to live
unsigned long LastPublish = Time.now()-TTL;
int SumpSensorVal = 0;
char DoorState[20] = "UNKOWN";
double CurrentTemp = 0.0;
void setup() {
...
}
void loop() {
//... Get the values and strcpy(DoorState, "OPEN"); or strcpy(DoorState, "CLOSED");
// Publish the variables
if(Time.now() >= (LastPublish + TTL)){
Spark.publish("Garage_Temp", String(CurrentTemp), TTL, PRIVATE);
Spark.publish("Garage_Door", DoorState, TTL, PRIVATE);
Spark.publish("SumpSensor", String(SumpSensorVal), TTL, PRIVATE);
LastPublish = Time.now();
}
}
for a quick test can we try:
unsigned long old_time = millis();
void loop() {
//... Get the values and strcpy(DoorState, "OPEN"); or strcpy(DoorState, "CLOSED");
// Publish the variables
if(milis() - old_time >= 2000){
Spark.publish("Garage_Temp", String(CurrentTemp), TTL, PRIVATE);
Spark.publish("Garage_Door", DoorState, TTL, PRIVATE);
Spark.publish("SumpSensor", String(SumpSensorVal), TTL, PRIVATE);
old_time = millis();
}
}
That did the trick! Thank you so much. I have been struggling to figure this out for a while. Is the issue with Time.h?
Would this work to avoid the wrap around at about 49 Days
void loop() {
// check for millis() wrap around
if(millis() < old_time){old_time = millis();}
// Other code follows
}
Ha. i was suspecting that…
If you want to debug, print out Time.now()
and use it accordingly in your code
So I took your suggestion, what is interesting is that Time.now() returns on its first call
2507222946 and 1434175356 on its second from which point on it seems to be behaving.
unsigned long LastPublish = millis();
unsigned long CurrentTime = Time.now();
void setup() {
Time.zone(-7);
CurrentTime = Time.now();} // Doesn't seem to matter whether this is here or not
void loop() {
// Publish the variables
if(millis() - LastPublish >= 5000){
CurrentTime = Time.now();
Spark.publish("TimeNow", String(CurrentTime), TTL, PRIVATE);
LastPublish = millis();
}
}
the 1st publish was 2507222946
the 2nd publish was 1434175356
the 3rd was 1434175361
the 4th was 1434175366
and so on ...
I wonder if the fact that time is int32 rather uint32 is where I am running into a problem https://community.particle.io/t/time-class-and-signed-unix-time & [Solved/Fixed] Time.now() in setup() gives bad time after power on however that really doesnt explain why the code I have works fine on a core but fails on a photon
Use EllapsedMillis! (It’s in the libraries section in the WebIDE. Add it to your project). Code would look like this:
elapsedMillis tmrUpdate;
unsigned long updateInterval = 4000;
// Other Variables
void setup() {
// ... Setup code
tmrUpdate = 0;
}
// Other awesome functions
void loop()
{
//... Get the values and strcpy(DoorState, "OPEN"); or strcpy(DoorState, "CLOSED");
// Publish the variables
if(tmrUpdate > updateInterval){
Spark.publish("Garage_Temp", String(CurrentTemp), TTL, PRIVATE);
Spark.publish("Garage_Door", DoorState, TTL, PRIVATE);
Spark.publish("SumpSensor", String(SumpSensorVal), TTL, PRIVATE);
tmrUpdate = 0;
}
}
Thanks, I will definitely try this out. I just wonder what the issue is between the core and photon with Time.now().
Hi Harrison. Thanks for the suggestion and example code it worked great. I modified a little so as to allow for multiple variable publishing
elapsedMillis tmrUpdate;
unsigned long updateInterval = 1000;
int VTP = 1; // Variable to Publish
// Other Variables
void setup() {
// ... Setup code
tmrUpdate = 0;
}
// Other awesome functions
void loop()
{
//... Get the values
// Publish the variables
if(tmrUpdate > updateInterval){
switch(VTP){
case 1:
VTP++; Spark.publish("Name1", String(Variable1), TTL, PRIVATE);
tmrUpdate = 0; break;
case 2:
VTP++; Spark.publish("Name2", String(Variable2), TTL, PRIVATE);
tmrUpdate = 0; break;
//...
case N:
VTP=1; Spark.publish("NameN", String(VariableN), TTL, PRIVATE);
tmrUpdate = 0; break;
}
}