[Solved]Core doesn't get system time

Hi,

After the build night last wednesday I got the feeling of building again with my spark so I did. However, it seems to fail to retrieve the system time

It connects to the cloud via Wifi no problem after manual claim.
My code uses the Spark.time function, does not work. Time is 1 jan 00:00:00 1970.

I tried flashing the CC3000 chip again, no succes.
Sometimes randomly it does retrieve the time properly, but very rare.
I’m guessing something in my code blocks it from retrieving the code, but what?

below is the code I use (it is a bit messy, needs cleaning :wink: )

    // This #include statement was automatically added by the Spark IDE.
    #include "SparkButton/SparkButton.h"
    
    SparkButton klok = SparkButton();
    int hour;
    int minutes;
    int fiveminutes;
    int singleminutes;
    int sync = 0; // TODO: RESYNC AFTER X TIME
    int GMT = 1;  //GMT time diff, use 1 for +1 and -1 for -1 GMT
    int brightness = 50;
    
    void setup() {
        klok.begin();
        Serial.begin(9600);
        RGB.control(true);
        RGB.color(0,0,0);
        
    }
    
    void loop() {
        Serial.print(Time.timeStr());
        klok.ledOff(hour);
        hour = Time.hourFormat12();
        hour += GMT;
        minutes = Time.minute();
        
        if (hour == 12||hour == 0)
        {
            RGB.color(0,brightness,0);
        }
        
        else
        {
            klok.ledOn(hour,0,brightness,0);
        }
        
        fiveminutes = convertmin(minutes);
        singleminutes = minutes - (fiveminutes*5);
        
        if (fiveminutes > 0 && singleminutes > 0 )
        {
            if (fiveminutes != hour)
            {
                do {
                    klok.ledOff(fiveminutes);
                    Delay(500);
                    
                    for(int i = 0; i<singleminutes;i++)
                        {
                            Serial.print(Time.timeStr());
                            klok.ledOn(fiveminutes,brightness,0,brightness);
                            Delay(100);
                            klok.ledOff(fiveminutes);
                            Delay(200);
                        }
                    Delay(300);
                    klok.ledOn(fiveminutes,0,0,brightness);
                    Delay(5000);
                    
                } while (minutes == Time.minute());
            klok.ledOff(fiveminutes);
                
            }
            
            else if (fiveminutes == hour)
            {
                do {
                    klok.ledOn(fiveminutes,0,brightness,0);
                    Delay(500);
                    
                    for(int i = 0; i<singleminutes;i++)
                        {
                            Serial.print(Time.timeStr());
                            klok.ledOn(fiveminutes,brightness,brightness,brightness);
                            Delay(100);
                            klok.ledOn(fiveminutes,0,brightness,0);
                            Delay(200);
                        }
                    Delay(300);
                    klok.ledOn(fiveminutes,0,brightness,brightness);
                    Delay(5000);
                    
                } while (minutes == Time.minute());
            klok.ledOn(fiveminutes,0,brightness,0);
            }
                
        }
        
        else if (fiveminutes == 0 && singleminutes > 0)
        {
            if (hour != 1)
            {
                do {
                    for(int i = 0; i<singleminutes;i++)
                        RGB.color(0,0,0);
                        Delay(500);
                        
                        {
                            Serial.print(Time.timeStr());
                            RGB.color(brightness,0,brightness);
                            Delay(100);
                            klok.ledOff(1);
                            Delay(200);
                        }
                        Delay(300);
                        RGB.color(0,0,brightness);
                        Delay(5000);
                } while (minutes == Time.minute());
            RGB.color(0,0,0);
            }
                
            else if (hour == 12)
            {
                    do {
                    for(int i = 0; i<singleminutes;i++)
                        RGB.color(0,brightness,0);
                        Delay(500);
                        {
                            Serial.print(Time.timeStr());
                            RGB.color(brightness,brightness,brightness);
                            Delay(100);
                            RGB.color(0,brightness,0);
                            Delay(200);
                        }
                        Delay(300);
                        RGB.color(0,brightness,brightness);
                        Delay(5000);
                } while (minutes == Time.minute());
            klok.ledOn(1,0,brightness,0);
            RGB.color(0,brightness,0);
            }
        }
        
        else if (fiveminutes > 0 && singleminutes == 0)
        {
            if (hour != fiveminutes)
            {
                do {
                    klok.ledOn(fiveminutes,0,0,brightness);
                } while (minutes == Time.minute());
            }
            
            else if (hour == fiveminutes)
            {
                do {
                    klok.ledOn(fiveminutes,0,brightness,brightness);
                } while(minutes == Time.minute());
            }
        }
        
        else if (fiveminutes == 0 && singleminutes == 0)
        {
            do{
                Delay(1000); //nothing lol
            }while(minutes == Time.minute());
        }
    }// end main loop
    
    int convertmin(int input)
    {
        int temp = input/5;
        
        if (input < 5){ return 0;}
        
        if (temp >= 1 && temp <2)
            {
                temp = 1;
            }
            
            else if (temp >= 2 && temp <3)
            {
                temp = 2;
            }
            
            else if (temp >= 3 && temp <4)
            {
                temp = 3;
            }
            
            else if (temp >= 4 && temp <5)
            {
                temp = 4;
            }
            
            else if (temp >= 5 && temp <6)
            {
                temp = 5;
            }
            
            else if (temp >= 6 && temp <7)
            {
                temp = 6;
            }
            
            else if (temp >= 7 && temp <8)
            {
                temp = 7;
            }
            
            else if (temp >= 8 && temp <9)
            {
                temp = 8;
            }
            
            else if (temp >= 9 && temp <10)
            {
                temp = 9;
            }
            
            else if (temp >= 10 && temp <11)
            {
                temp = 10;
            }	
            
            else if (temp >= 11 && temp <12)
            {
                temp = 11;
            }
            
            else temp = 0;
            
        return temp;
       
    }

Calling Spark.syncTime() in setup() and say once a day in loop() should fix the problem.

http://docs.spark.io/firmware/#spark-synctime

1 Like

Sadly, it doesn’t. It takes about 2 hours to sync time.
Is it the use of the ugly Delays? I’m going to try to replace those.

EDIT: Replacing the ugly delays fixed it :smile:

Great to see you’ve got it working! Would you mind sharing the working code, or edit the one above accordingly, so others can use it for future reference?
I’ve marked the topic as [solved]. Should the issue return, feel free to post here again, and I’ll remove the tag.

OK, sorry I missed this the first time I read your code: This is a known problem that has not been fixed yet. On Spark, you have the normal wiring language delay(N); call (note the lowercase “d”) but due to a bug, you can also reach Delay(N); with a capital-D which is an internal function that should not be user visible.

Hope fully this will get fixed soon.

1 Like

@moors7 I am creating a project share soon :wink: Working on some additional features at the moment :smile:

@bko ah yes that explains haha, much appreciated :smile:

1 Like