Shop OBEX P1 Docs P2 Docs Learn Events
DEMO: Using SNTP to sync the Spinneret's on-board RTC - Page 2 — Parallax Forums

DEMO: Using SNTP to sync the Spinneret's on-board RTC

2»

Comments

  • LtechLtech Posts: 366
    edited 2012-02-01 01:28
    There is some bug in the date code of SNTP !
    .... 1/30/2012 and 1/31/2012 give me wrong date of march first ????
    I first think my SNTP server was wrong, I try severals, The time and Year are always corect, but not the day and month !
    Then thinking there was a issue with US MM/DD/YYYY and EU DD/MM/YYYY ... . but not !

    But without code change today I get SMPT 2/0/2012 !!! The RTC is set correct on 2/1/2012 ????

    After the RTC update I put this code to compare to "DisplayHumanTim" from SNTP :


    PauseMSec(950)
    RTC.update
    PST.str(string("RTC time:",13))
    PST.str(RTC.FmtDateTime)
    PST.Char(13)
    PST.str(SNTP.GetReferenceIdentifier(@Buffer,string("----")))
    PST.Char(13)
    PST.Char(13)


    Result of terminal window:

    SNTP Server Sync time:
    (GMT + 1:00) 02/00/2012
    10:20:51

    RTC time:
    Wed Feb 01, 2012 10:20:51
    GPS
  • Beau SchwabeBeau Schwabe Posts: 6,547
    edited 2012-02-01 13:59
    I'll look at it in more detail later.... the problem is that the SNTP server reports the current time and date in the number of seconds that have elapsed since Jan 1, 1900 ... That's a lot of seconds since then with a few leap years in between.... Since this year is a leap year, it threw things off.
  • JohnR2010JohnR2010 Posts: 431
    edited 2012-02-14 07:02
    Beau, any news on leap year throwing the time off with this object? I have an app running on my spinner that has been working great for months but the other day I noticed the reported day set by this SNTP object to be exactly one day off. I know this worked fine last year and I don’t understand what is causing it to be one day behind. For example today is Feb 14, 2012 but when I make a call to get the SNTP time with your object it reports Feb 13, 2012? No errors everything looks fine just one day off?? I wonder if at the end of this month (since there are 29 days in February this year) it will go back to normal??

    Thanks for all the work on the object it really makes my solution look sharp!!
  • Beau SchwabeBeau Schwabe Posts: 6,547
    edited 2012-02-14 08:19
    JohnR2010,

    I think it's in my calculation of #Leap Years since 1900 but I need to test it for when Feb 29th rolls over. I think there needs to be another check in February to see if the current year is a leap year. In the current Spinneret code it checks for previous leap years, thus where the one day difference error is occurring ... when the current year is a leap year.

    I have not tested this in the Spinneret yet, but only in the Perl code that I originally used to debug in the first place. I still need to implement a check for Feb rollover and if the current year is a leap year or not.

    Here is the Perl code if you or anyone is interested... cut-n-paste this to a file on a Linux operating system, then change the permissions of the file to 555 ... i.e. chmod 555 timetest.pl
    #!/usr/bin/perl
    
     use IO::Socket::INET;
    $MySocket=new IO::Socket::INET->new(PeerPort=>123,  #open socket
            Proto=>'udp',
            PeerAddr=>'132.163.4.101');
    
    $data =     chr(227) . chr(0) . chr(0) . chr(148);  #LI/VN/Mode, Stratum, Poll, Precision
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Root Delay
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Root Dispersion
    $data = $data . "LOCL";                             #Reference Identifier
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Reference Timestamp
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Originate Timestamp
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Receive Timestamp
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Transmit Timestamp
    $data = $data . chr(0) . chr(0) . chr(0) . chr(0);
    
    $MySocket->send($data);			            #Waiting to receive data		
    $MySocket->recv($data,56);                          
    $MySocket->close();                                 #received data close socket
    
    @Chars = split("", $data);
    
    $n1 = ord($Chars[40]);				    #Upper 32 bits of Transmit Timestamp
    $n2 = ord($Chars[41]);
    $n3 = ord($Chars[42]);
    $n4 = ord($Chars[43]);
    
    $Seconds = $n4 + $n3*256 + $n2*65536 + $n1*16777216; #Seconds since Jan1, 1900
    
    $Zone =  -6;                                         #GMT - 6 time zone adjust
    #$Zone =  -5;                                         #use GMT - 5 during daylight savings
    
    $Seconds = $Seconds  + 3600*$Zone;
    
    print "Seconds since Jan1, 1900 : $Seconds \n\r";
    
    
    
    
    
    
    $Days = int((($Seconds >> 7)/675)+1);                #Days since Jan1, 1900     {divide seconds by 86,400 and add 1}
    print "   Days since Jan1, 1900 : $Days \n\r";
              
    $DW = ($Days-1) % 7; 			             #Day of Week               
    
    $Years = int($Days / 365);                           #Years since 1900
    print "  Years since Jan1, 1900 : $Years \n\r";
    
    $Days = $Days % 365;                                 #Days this year
    
    $LeapYears = int(($Years-1) / 4);		     #Leap Years since 1900
    $CurrentYear = $Years + 1900 ;                       #Current Year
    
    $Days = $Days - $LeapYears;                          #Leap Year Days Correction
    
    print "          Days this year : $Days \n\r";
    
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    $count = 1;					     #Routine to determine
     do {                                                #  - The Current Month 
    $Month = 30;                                         #  - The Current Date       
    $c1 = ($count & 1);                                  #
    $c2 = (($count & 8) >>3);                            # from the number of Days
    if ($c1 != $c2) {                                    # passed in the current year 
        $Month = $Month +1;                              
       }
    if ($count == 2) {
        $Month = 28; 
       }
    
    print "Month : $count Days in Month : $Month \n\r";
    
    if ($Days >= $Month) {
       $Days = $Days - $Month;
       if ($Days <= $Month) {
          $CurrentMonth = $count + 1;
          $CurrentDate  = $Days;  
          $count = 12;
          }     
       }
    $count++;
     } until ($count == 13);
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    $Seconds = $Seconds - ((($Years * 365)*675) *128);   #Seconds this year
    $SS = $Seconds;
    
    print "\n\r";
    print "$SS";
    
    $MM = int($SS / 60);                                 
    $SS = $SS - ($MM*60);                                #Current Seconds
    $HH = int($MM / 60);                                 
    $MM = $MM - ($HH*60);                                #Current Minutes
    $DD = int($HH / 24);                                 
    $HH = $HH - ($DD*24);                                #Current Hour
    
    print "\n\r";
    print " Date: $CurrentMonth / $CurrentDate / $CurrentYear \n\r";
    print " Time: $HH : $MM : $SS \n\r";
    
  • JohnR2010JohnR2010 Posts: 431
    edited 2012-04-09 08:30
    Beau, just as an update this code corrected itself after the end of leap day on March 1.
  • Beau SchwabeBeau Schwabe Posts: 6,547
    edited 2012-04-09 09:10
    JohnR,

    Thanks John, that's what I suspect. Looks like I need to add a check to validate if the current month happens to be February AND if that month is a leap year. Previous years it seems to calculate ok, but if the current month is February and also a leap year it is off by a day.
  • lopezblopezb Posts: 3
    edited 2013-02-18 13:44
    Hi, I am trying to use the latest code to update the RTC values, and it seems that it does not want to work for me. The problem I am experiencing is that the code is locked to CST even though I modify the code for EST. I also have an issue that it updates the week day string to a day prior, i.e. MON is shown as SUN. Also, I want the real time to synch to the SNTP server when I click a button on my webserver interface. How would I be able to tell it when to synch with real time?

    I'm new to these forums so if you need further details, I will be happy to fill you in more.

    Thank You!
    Brandon
  • Beau SchwabeBeau Schwabe Posts: 6,547
    edited 2013-02-18 13:53
    lopezb,

    Welcome to the forum ! feel free to ask any question you want. There are many people here that can help.


    Can you post the exact code that you are using? Use the 'File-->Archive-->Project' option in the Propeller Tool.
  • Mike GMike G Posts: 2,702
    edited 2013-02-18 16:22
    lopezb, Attached is spin source and HTML (rtc.htm) that updates the Spinneret's RTC by clicking a button. It uses Beau's latest SNTP updates. The timezone and day appear to update as expected.

    Google Code
Sign In or Register to comment.