Playing around with the added functions.
SNTP should return a zero if there is no data. It does do that if the initial connection is not present.
If the connection is present and it gets data and then the connection is lost. In the following example, which is just a slightly modified version, from the help example, that works on the Arduino M2560 w/Ethernet Shield.
I added a UTC offset, since I am in the US CST, it is -6hrs.
With the Ethernet connection present all is well and SNTP is reported every minute through the loop.
Unplug cable and lsntp is something other than zero, so of cource another 6hrs is subtracted....etc....
If you enable the lsntp = 0 at the end of the loop. Then the program functions normally with or without the cable.
@MarkA- I have read the help. :) Not saying it is a bug. Just an observation.
[code:1:9070b1ba3a]
'
'Original code ported to work on Arduino M2560. MLM 11/18/2013
'-----------------------------------------------------------------------------------------
'name : sntp_SPI.bas RFC 2030
'copyright : (c) 1995-2012, MCS Electronics
'purpose : test SNTP() function
'micro : Mega88
'suited for demo : yes
'commercial addon needed : no
'-----------------------------------------------------------------------------------------
'$regfile = "m1280def.dat" ' specify the used micro
$regfile = "m2560def.dat" ' specify the used micro
$crystal = 16000000 ' used crystal frequency
$baud = 115200 ' use baud rate
$hwstack = 200
$swstack = 220
$framesize = 250
$programmer=23 'Define programmer as ARDUINO V2 (using stk500v2 protocol)
$framesize = 80 ' default use 40 for the frame space
$lib "datetime.lbx" ' this example uses date time routines
Const Ntp_one_hour_offset = 3600
Const Ntp_CST_offset = Ntp_one_hour_offset * -6 ' CST to UTC offset
Config Portg.5 = Output ' define here Pin for CS of MMC/SD Card
Mmc_cs Alias Portg.5
Set Mmc_cs ' disable SD
'SPI port for WIZnet on Ethernet Shield
Config Portb.4 = Output
Wiz5100_cs Alias Portb.4
Wiz5100_cs = 1 ' enable W5100
Waitms 500
'Config Spi for WIZnet
Config Portb.0 = Output
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
Spiinit
Enable Interrupts ' before we use config tcpip , we need to enable the interrupts
Print "Init TCP" ' display a message
Config Tcpip = Noint , _
Mac = 12.128.12.33.46.74 , _
Ip = 10.66.48.199 , _
Submask = 255.255.255.0, _
Gateway = 10.66.48.110 , _
Localport = 1000 , _
Tx = $55 , Rx = $55 , _
Chip = W5100 , _
Spi = 1 , _
Cs = Portb.4
Dim Var As Byte ' for i2c test
Dim Ip As Long ' IP number of time server
Dim Idx As Byte ' socket number
Dim Lsntp As Long ' long SNTP time
Dim booFirstPass as boolean
Print "SNTP demo"
'assign the IP number of a SNTP server
Ip = Maketcp(64.90.182.55 ) ' assign IP num NIST time.nist.gov port 37
Print "Connecting to : " ; Ip2str(ip)
'we will use Dutch format
Config Date = Dmy , Separator = -
'we need to get a socket first
'note that for UDP we specify sock_dgram
Idx = Getsocket(idx , Sock_dgram , 5000 , 0) ' get socket for UDP mode, specify port 5000
Print "Socket " ; Idx ; " " ; Idx
'UDP is a connection less protocol which means that you can not listen, connect or can get the status
'You can just use send and receive the same way as for TCP/IP.
'But since there is no connection protocol, you need to specify the destination IP address and port
'So compare to TCP/IP you send exactly the same, but with the addition of the IP and PORT
'The SNTP uses port 37 which is fixed in the tcp asm code
booFirstPass = 1 ' force SNTP to be read right away at reset.
Do
if booFirstPass = 0 then ' countdown as normal
Print "Count down to next SNTP time check...";
for var =60 to 1 step -1
Print var;".";
Waitms 1000
next var
Print
endif
Lsntp = Sntp(idx , Ip) ' get time from SNTP server
print "Sntp-lsntp=";Lsntp
Print "Non Adjusted - ";Date(lsntp) ; Spc(3) ; Time(lsntp)
'notice that it is not recommended to get the time every sec
'the time server might ban your IP
'it is better to sync once or to run your own SNTP server and update that once a day
'what happens is that IP number of timer server is send a diagram too
'it will put the time into a variable lsntp and this is converted to BASCOM date/time format
'in case of a problem the variable is 0
if Lsntp <> 0 then ' 0 indicates no connection / data from SNTP server, only adjust offset if valid time received
Lsntp = Lsntp + Ntp_CST_offset ' CST to UTC offset
Print "Adjusted - ";Date(lsntp) ; Spc(3) ; Time(lsntp)
endif
print "After Time Zone Adjustment-lsntp=";Lsntp
booFirstPass = 0 ' set flag to allow countdown to next SNTP login.
'Lsntp = 0
Loop
End
[/code:1:9070b1ba3a]
Terminal output with cable connected ............. All good.....
----------------------------------------------------------------------------
Init TCP
Init done
SNTP demo
Connecting to : 64.90.182.55
Socket 0 0
Sntp-lsntp=438131943
Non Adjusted - 18-11-13 23:19:03
Adjusted - 18-11-13 17:19:03
After Time Zone Adjustment-lsntp=438110343
Count down to next SNTP time check...60.59.58.57.56.55.54.53.52.51.50.49.
Terminal output with cable disconnected ......Shouldn't lsntp = 0??????
----------------------------------------------------------------------------------
19.18.17.16.15.14.13.12.11.10.9.8.7.6.5.4.3.2.1.
Sntp-lsntp=438110699
Non Adjusted - 18-11-13 17:24:59
Adjusted - 18-11-13 11:24:59
After Time Zone Adjustment-lsntp=438089099
Count down to next SNTP time check...60.59.58.57.56.55.54.53.52.51.50.49.
As you can see, zero was not returned from the SNTP function so the offset of -6 hrs was removed.
Thoughts?
Regards,
Mark
[b:9070b1ba3a][color=red:9070b1ba3a](BASCOM-AVR version : v2.0.7.7 , Latest : 2.0.7.6 )[/b:9070b1ba3a][/color:9070b1ba3a]
↧