Using Time.hour()

Hello. I’m hitting a road block when trying to use Time.hour(). It locks up the photon as soon as it hits the line. This is just my test code to see if it can return the correct number.

   if(millis() - lastRequest >= 5000){
        
        lastRequest = millis();
 
        Particle.publish(String(Time.hour()));

   }

Am I missing something to make this work? My photon is running 0.6.0.

Thanks for any help!

Your code works fine for me. I don’t see how the code you posted would cause your Photon to “lock up”. What do you mean by “lock up” anyway? What are you observing? You should post the whole code that you are using in this test. BTW, you won’t get the expected number if you don’t set the time zone (unless you live where the offset is 0).

1 Like

I think I figured out what was causing it to fail. I was experimenting with using a timer like this:

Timer Mytimer(5000, read_light);

with this in setup:

void setup() {
   Mytimer.start();
}

and I had a function to read how bright the ambient light is. I removed this above code and just put the call to read_light() in the milli timer in the loop and it works just fine now to pull the hour. There must be some conflict with the built in timer and the Time call?

There should not be any conflict between a software timer and the Time.hour() function, so something else must have been going on.

1 Like

You’re right. I did a small sketch with just a software timer and a Time call and it worked fine. I also have SYSTEM_THREAD(ENABLED) to solve an issue of it just going to breathing green. Here is my full ugly code. Something is really makes it freak out if I uncomment the two lines for the Timer.

Thanks for your time in responding to me!

#include "application.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
char auth[] = "abc123";

SYSTEM_THREAD(ENABLED);

// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"

#define PIXEL_COUNT 1
#define PIXEL_PIN D0
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

//Timer Mytimer(5000, read_light);

unsigned long lastRequest  = 0;

int val;

int light_status = 0;
int light_brightness = 255;

int neo_red = 255;
int neo_green = 255;
int neo_blue = 255;

void setup() {

    pinMode(A4, OUTPUT);
    pinMode(A3, INPUT);

    strip.begin();
    strip.show(); // Initialize all pixels to 'off'

    //Mytimer.start();
    Blynk.begin(auth);

}

void loop() {

    Blynk.run();

   if(millis() - lastRequest >= 5000){

        lastRequest = millis();

        int hour = Time.hour();
        Particle.publish(String(hour));

        read_light();

   }


}

void read_light(){

    val = analogRead(A3);
    Particle.publish(String(val));

    if(val > 200){

        analogWrite(A4, 255);
        light_status = 1;
        Blynk.virtualWrite(V55, 1);

    } else {

        analogWrite(A4, 0);
        light_status = 0;
        Blynk.virtualWrite(V55, 0);

    }

}


BLYNK_WRITE(V55){

    int button_data = param.asInt();

    if(button_data){
        analogWrite(A4, light_brightness);
        light_status = 1;

    } else {    
        analogWrite(A4, 0);
        light_status = 0;
    }
}

BLYNK_WRITE(V56){

    light_brightness = param.asInt();

    if(light_status){
       analogWrite(A4, light_brightness);
    }

}

BLYNK_WRITE(V60){

    neo_red = param.asInt();

    strip.setPixelColor(0, strip.Color(neo_red, neo_green, neo_blue));
    strip.show();
}
BLYNK_WRITE(V61){

    neo_green = param.asInt();

    strip.setPixelColor(0, strip.Color(neo_red, neo_green, neo_blue));
    strip.show();
}
BLYNK_WRITE(V62){

    neo_blue = param.asInt();

    strip.setPixelColor(0, strip.Color(neo_red, neo_green, neo_blue));
    strip.show();
}

Don’t you have to import time or use the time library? I know you have to with Arduino in order to use Time.hour.

@tyler785, you need to treat timer callbacks like interrupt ISRs and keep them “lite”. As such, you can’t do cloud function calls in there and I’m not sure about the Blynk calls either.

@Kurticus, the Time library is built in to the system firmware and doesn’t require anything to work :wink:

1 Like

I’ve used timer calls with publish to particle in several different past projects with no ill effect. Some have been running for over a year.

@tyler785, interesting. Some users have had problems. I’ll have to ask around. Thanks for the info!

So far my experimenting has lead to

SYSTEM_THREAD(ENABLED);

and

Timer Mytimer(5000, read_light);

together do not mix at all with my code. 90% of the time after a reset or flash, the device goes into a fast cyan blink mode. It sometime comes out of it, sometime goes into SOS and sometimes fails to loop. I am just going to use millis(). It seems pretty stable with that route.

I tried a simple sketch with just the system_mode set and a timer and of course it works just fine. Maybe its Blynk? and the unique combination of of my code? Not sure. If I discover anything else, I’ll post here.

Some of this may not have anything to do with your code. I’ve seen this too lately with much simpler code than what you’re running. This seems to be an intermittent problem at Particle that has to do with their servers and load balancing. It seems to occur now and again, and was discussed in this thread. I’ve had problems this week with it taking a minute or so to connect with the cloud. It often helps to do a reset.

1 Like

The problem is not Particle.publish() but the Blynk calls, I’d say - it’s the limited stack size that plays a role and any nested function calls and indirections can suck up too much of that.
So just because some programs work that way it’s not a guarantee that others will behave the same. We’d need a running and a crashing code to point out the exact difference that causes the crash.

And the mix SYSTEM_THREAD() enabled and Timer is probably crashing due to Particle.publish() and Blynk calls relying on an established cloud connection which is not guaranteed when decoupling app and sys threads.
Add a waitUntil(Particle.connected) before starting your timer or a if (!Particle.connected()) return; in your timer callback and I’d think your troubles will reduce.

2 Likes

I’ll give it a shot. Thanks!