Getting started with Arduino UNO R3 and Ethernet Shield (W5100)

(Difference between revisions)
Jump to: navigation, search
Line 8: Line 8:
  
 
The usage of Easy TCP/IP in combination with that example is also the same.
 
The usage of Easy TCP/IP in combination with that example is also the same.
 +
 +
 +
 +
Another difference is that in this example we do not use the Bascom-AVR bootloader. We use here the existing Arduino bootloader.
 +
 +
To get started with this see here:  [http://wiki.mcselec.com/Getting_started_with_Arduino_UNO http://wiki.mcselec.com/Getting_started_with_Arduino_UNO]
  
 
== Source Code:<br/> ==
 
== Source Code:<br/> ==
Line 42: Line 48:
  
 
' NTP Server IP address (here you can change the NTP server of your choice)
 
' NTP Server IP address (here you can change the NTP server of your choice)
Const Ntp1 = 192 : Const Ntp2 = 53 : Const Ntp3 = 103 : Const Ntp4 = 108       ' Example = 192.53.103.108
+
Const Ntp1 = 192 : Const Ntp2 = 53 : Const Ntp3 = 103 : Const Ntp4 = 108 ' Example = 192.53.103.108
  
Const Use_ping = 1                                         'use PING NTP server for checking NTP server availability before move on
+
Const Use_ping = 1 'use PING NTP server for checking NTP server availability before move on
  
  
 
$regfile = "m328pdef.dat"
 
$regfile = "m328pdef.dat"
$crystal = 16000000                                         '16MHz
+
$crystal = 16000000 '16MHz
 
$hwstack = 100
 
$hwstack = 100
 
$swstack = 100
 
$swstack = 100
Line 60: Line 66:
 
Dim Ip_sntp_server As Long
 
Dim Ip_sntp_server As Long
 
'assign the IP number of a SNTP server
 
'assign the IP number of a SNTP server
Ip_sntp_server = Maketcp(ntp1 , Ntp2 , Ntp3 , Ntp4)         'assign IP of NTP SERVER
+
Ip_sntp_server = Maketcp(ntp1 , Ntp2 , Ntp3 , Ntp4) 'assign IP of NTP SERVER
  
  
Line 76: Line 82:
 
Enable Interrupts
 
Enable Interrupts
  
  'Here you need MAC address, the ip address you like to use, subnet mask, gateway and localport
+
'Here you need MAC address, the ip address you like to use, subnet mask, gateway and localport
  Config Tcpip = Noint , _
+
Config Tcpip = Noint , _
            Mac = 1.1.94.127.1.255 , _
+
Mac = 1.1.94.127.1.255 , _
            Ip = 192.168.2.254 , _
+
Ip = 192.168.2.254 , _
            Submask = 255.255.255.0 , _
+
Submask = 255.255.255.0 , _
            Gateway = 192.168.2.1 , _
+
Gateway = 192.168.2.1 , _
            Localport = 1000 , _
+
Localport = 1000 , _
            Tx = $55 , Rx = $55 , _
+
Tx = $55 , Rx = $55 , _
            Chip = W5100 , _
+
Chip = W5100 , _
            Spi = 1
+
Spi = 1
  
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' Config Clock
+
' Config Clock
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
 
Dim Local_time As Long
 
Dim Local_time As Long
Line 98: Line 104:
  
 
Config Timer1 = Timer , Prescale = 256
 
Config Timer1 = Timer , Prescale = 256
On Timer1 Timer_irq Saveall                                 'the SAVEALL option is NEEDED !
+
On Timer1 Timer_irq Saveall 'the SAVEALL option is NEEDED !
 
Const Timer_preload = 3036
 
Const Timer_preload = 3036
 
Enable Timer1
 
Enable Timer1
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' UDP
+
' UDP
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
Dim Idx As Byte                                             ' socket number
+
Dim Idx As Byte ' socket number
Dim Result As Word                                         ' result
+
Dim Result As Word ' result
 
Dim S(255) As Byte
 
Dim S(255) As Byte
 
Dim R(255) As Byte
 
Dim R(255) As Byte
 
Dim Sstr As String * 255
 
Dim Sstr As String * 255
Dim Temp As Byte , Temp2 As Byte                           ' temp bytes
+
Dim Temp As Byte , Temp2 As Byte ' temp bytes
 
Dim Ip As Long
 
Dim Ip As Long
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' PING NTP SERVER to check if NTP server is available
+
' PING NTP SERVER to check if NTP server is available
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
 
#if Use_ping = 1
 
#if Use_ping = 1
  Dim J As Byte , Res As Byte
+
Dim J As Byte , Res As Byte
  Dim Ii As Byte
+
Dim Ii As Byte
  Dim Ping_check_ok As Bit
+
Dim Ping_check_ok As Bit
  
  ' FILL PING ARRAY
+
' FILL PING ARRAY
  S(1) = 8                                                   'type is echo
+
S(1) = 8 'type is echo
  S(2) = 0                                                   'code
+
S(2) = 0 'code
  
  S(3) = 0                                                   ' for checksum initialization
+
S(3) = 0 ' for checksum initialization
  S(4) = 0                                                   ' checksum
+
S(4) = 0 ' checksum
  S(5) = 0                                                   ' a signature can be any number
+
S(5) = 0 ' a signature can be any number
  S(6) = 1                                                   '   signature
+
S(6) = 1 ' signature
  S(7) = 0                                                   ' sequence number - any number
+
S(7) = 0 ' sequence number - any number
  S(8) = 1
+
S(8) = 1
  S(9) = 65
+
S(9) = 65
  
  Dim W As Word At S + 2 Overlay                             'same as dta(3) and dta(4)
+
Dim W As Word At S + 2 Overlay 'same as dta(3) and dta(4)
  Dim B As Byte
+
Dim B As Byte
  W = Tcpchecksum(s(1) , 9)                                   ' calculate checksum and store in dta(3) and dta(4)
+
W = Tcpchecksum(s(1) , 9) ' calculate checksum and store in dta(3) and dta(4)
  
  'Ip = Maketcp(192.53.103.108)                               'try to check this google server
+
'Ip = Maketcp(192.53.103.108) 'try to check this google server
  Print "PING NTP Server : " ; Ip2str(ip_sntp_server)
+
Print "PING NTP Server : " ; Ip2str(ip_sntp_server)
  
  'Print "Socket " ; Idx ; " " ; Idx
+
'Print "Socket " ; Idx ; " " ; Idx
  Setipprotocol Idx , 1                                       'set protocol to 1
+
Setipprotocol Idx , 1 'set protocol to 1
  'the protocol value must be set BEFORE the socket is openend
+
'the protocol value must be set BEFORE the socket is openend
  
  Idx = Getsocket(idx , Sock_ipl_raw , 5000 , 0)             'RAW Socket
+
Idx = Getsocket(idx , Sock_ipl_raw , 5000 , 0) 'RAW Socket
  
  ' We try the PING 3 times (but if we get an answer we exit the for loop)
+
' We try the PING 3 times (but if we get an answer we exit the for loop)
  Do
+
Do
      Incr Ii
+
Incr Ii
  
      Result = Udpwrite(ip_sntp_server , 7 , Idx , S(1) , 9)   'write ping data               '
+
Result = Udpwrite(ip_sntp_server , 7 , Idx , S(1) , 9) 'write ping data '
      Print "Ping Number : " ; ii
+
Print "Ping Number : " ; ii
      Waitms 200                                               ' depending on the hops, speed, etc
+
Waitms 200 ' depending on the hops, speed, etc
      Result = Socketstat(idx , Sel_recv)                     'check for data
+
Result = Socketstat(idx , Sel_recv) 'check for data
    ' Print "REC:" ; Result
+
  ' Print "REC:" ; Result
      If Result >= 11 Then
+
If Result >= 11 Then
  
        Res = Tcpread(idx , R(1) , Result)                   'get data with TCPREAD !!!
+
Res = Tcpread(idx , R(1) , Result) 'get data with TCPREAD !!!
  
      '     Print "DATA RETURNED :" ; Res
+
' Print "DATA RETURNED :" ; Res
      '(                     '
+
'( '
          For J = 1 To Result
+
For J = 1 To Result
            Print R(j) ; " " ;
+
Print R(j) ; " " ;
          Next
+
Next
            Print
+
Print
      ')
+
')
            Print "PING OK --> Exit Do..Loop"
+
Print "PING OK --> Exit Do..Loop"
  
            Set Ping_check_ok
+
Set Ping_check_ok
  
          Exit Do                                         ' If we can an answer from NTP Server exit do ... loop
+
Exit Do ' If we can an answer from NTP Server exit do ... loop
  
      Else
+
Else
        Print "NTP Server not available"
+
Print "NTP Server not available"
      End If
+
End If
      Waitms 100
+
Waitms 100
  
  Loop Until Ii = 3
+
Loop Until Ii = 3
  
  Ii = 0
+
Ii = 0
 
#else
 
#else
  Dim Ping_check_ok As Bit
+
Dim Ping_check_ok As Bit
  Ping_check_ok = 1
+
Ping_check_ok = 1
 
#endif
 
#endif
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' SNTP
+
' SNTP
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
  
If Ping_check_ok = 1 Then                                   'When the NTP Serverer responded to PING
+
If Ping_check_ok = 1 Then 'When the NTP Serverer responded to PING
  'we need to get a socket first
+
'we need to get a socket first
  'note that for UDP we specify sock_dgram
+
'note that for UDP we specify sock_dgram
  Idx = Getsocket(idx , Sock_dgram , 1000 , 0)               ' get socket for UDP mode, specify port
+
Idx = Getsocket(idx , Sock_dgram , 1000 , 0) ' get socket for UDP mode, specify port
  Print "Socket " ; Idx ; " " ; Idx
+
Print "Socket " ; Idx ; " " ; Idx
  
  Local_time = Sntp(idx , Ip_sntp_server)                     ' get time from SNTP server
+
Local_time = Sntp(idx , Ip_sntp_server) ' get time from SNTP server
  Print "Local_time = " ; Local_time
+
Print "Local_time = " ; Local_time
  
  If Local_time < 100 Then                                   'try it again
+
If Local_time < 100 Then 'try it again
      Wait 1
+
Wait 1
      Local_time = Sntp(idx , Ip_sntp_server)                 ' get time from SNTP server
+
Local_time = Sntp(idx , Ip_sntp_server) ' get time from SNTP server
      Print "Second try ; Local_time = " ; Local_time
+
Print "Second try ; Local_time = " ; Local_time
  End If
+
End If
  
  Local_time = Local_time + 3600                             ' (UTC/GMT) time + 3600
+
Local_time = Local_time + 3600 ' (UTC/GMT) time + 3600
  Print Date(local_time) ; Spc(3) ; Time(local_time)
+
Print Date(local_time) ; Spc(3) ; Time(local_time)
 
End If
 
End If
  
 
'get a socket first
 
'get a socket first
 
'note that for UDP we specify sock_dgram
 
'note that for UDP we specify sock_dgram
Idx = Getsocket(idx , Sock_dgram , 1000 , 0)               ' get socket for UDP mode, specify port
+
Idx = Getsocket(idx , Sock_dgram , 1000 , 0) ' get socket for UDP mode, specify port
 
'Print "Socket " ; Idx ; " " ; Idx
 
'Print "Socket " ; Idx ; " " ; Idx
  
  
 
Do
 
Do
    If New_second = 1 Then
+
If New_second = 1 Then
      Reset New_second
+
Reset New_second
      'Do something every second ......
+
'Do something every second ......
    End If
+
End If
  
      'The UDP header give us info about Peersize, Peerport and peeraddress
+
'The UDP header give us info about Peersize, Peerport and peeraddress
      'which is the amount of data bytes received from peerport and peer ip address
+
'which is the amount of data bytes received from peerport and peer ip address
      Udpreadheader Idx                                   ' read the udp header (only the first 8 Bytes)
+
Udpreadheader Idx ' read the udp header (only the first 8 Bytes)
  
      If Peersize > 0 Then                                 ' the actual number of bytes (without header bytes)
+
If Peersize > 0 Then ' the actual number of bytes (without header bytes)
        Print "Size= " ; Peersize ; " By" ; " Port= " ; Peerport ; " Sender IP= " ; Ip2str(peeraddress) ; " Timestamp : " ; Date$ ; " / " ; Time$
+
Print "Size= " ; Peersize ; " By" ; " Port= " ; Peerport ; " Sender IP= " ; Ip2str(peeraddress) ; " Timestamp : " ; Date$ ; " / " ; Time$
          Temp = Udpread(idx , S(1) , Peersize)           ' read the number of Bytes into Array S
+
Temp = Udpread(idx , S(1) , Peersize) ' read the number of Bytes into Array S
                Print "DATA = " ;
+
Print "DATA = " ;
          For Temp = 1 To Peersize
+
For Temp = 1 To Peersize
              Print Chr(s(temp)) ;                         ' print what we have received over USART to debug interface
+
Print Chr(s(temp)) ; ' print what we have received over USART to debug interface
          Next
+
Next
          Print
+
Print
        'Send it back to sender IP, Sender Port
+
'Send it back to sender IP, Sender Port
        Result = Udpwrite(peeraddress , Peerport , Idx , S(1) , Peersize)       ' write the received data back
+
Result = Udpwrite(peeraddress , Peerport , Idx , S(1) , Peersize) ' write the received data back
      End If
+
End If
 
Loop
 
Loop
  
Line 240: Line 246:
  
 
Timer_irq:
 
Timer_irq:
  Incr Local_time
+
Incr Local_time
  Time$ = Time(local_time)
+
Time$ = Time(local_time)
  Date$ = Date(local_time)
+
Date$ = Date(local_time)
  Set New_second
+
Set New_second
 
Return
 
Return
  
Line 255: Line 261:
 
Return
 
Return
 
</source>
 
</source>
 +
  
  

Revision as of 15:33, 8 March 2013

Contents

Please read first this Article:

Getting started with Arduino and WIZ820io --> [1]


It uses the same source code exept we use here an Wiznet W5100 Chip intead of W5200 so only config TCPIP needs to be changed.

The usage of Easy TCP/IP in combination with that example is also the same.


Another difference is that in this example we do not use the Bascom-AVR bootloader. We use here the existing Arduino bootloader.

To get started with this see here:  http://wiki.mcselec.com/Getting_started_with_Arduino_UNO

Source Code:

The difference is only this part in the Bascom source code:


'Here you need MAC address, the ip address you like to use, subnet mask, gateway and localport
 Config Tcpip = Noint , _
 Mac = 1.1.94.127.1.255 , _
 Ip = 192.168.2.254 , _
 Submask = 255.255.255.0 , _
 Gateway = 192.168.2.1 , _
 Localport = 1000 , _
 Tx = $55 , Rx = $55 , _
 Chip = W5100 , _
 Spi = 1

Complete Source code:


' IP Address of Arduino = 192.168.2.254
' Port of Arduino = 1000
' We use UDP
'
 
'Use Hardware:
' - Arduino UNO R3
' - Arduino Ethernet Shield with W5100 Wiznet Chip
 
 
' NTP Server IP address (here you can change the NTP server of your choice)
Const Ntp1 = 192 : Const Ntp2 = 53 : Const Ntp3 = 103 : Const Ntp4 = 108 ' Example = 192.53.103.108
 
Const Use_ping = 1 'use PING NTP server for checking NTP server availability before move on
 
 
$regfile = "m328pdef.dat"
$crystal = 16000000 '16MHz
$hwstack = 100
$swstack = 100
$framesize = 400
 
Config Com1 = 57600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
 
'-------------------------------------------------------------------------------
' Server Variables
'-------------------------------------------------------------------------------
Dim Ip_sntp_server As Long
'assign the IP number of a SNTP server
Ip_sntp_server = Maketcp(ntp1 , Ntp2 , Ntp3 , Ntp4) 'assign IP of NTP SERVER
 
 
 
Wait 2
Print
Print "-----START Arduino UNO R3 + Ethernet Shield DEMO-----"
'-------------------------------------------------------------------------------
' Network Hardware
'-------------------------------------------------------------------------------
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
Spiinit
 
Print
Enable Interrupts
 
 'Here you need MAC address, the ip address you like to use, subnet mask, gateway and localport
 Config Tcpip = Noint , _
 Mac = 1.1.94.127.1.255 , _
 Ip = 192.168.2.254 , _
 Submask = 255.255.255.0 , _
 Gateway = 192.168.2.1 , _
 Localport = 1000 , _
 Tx = $55 , Rx = $55 , _
 Chip = W5100 , _
 Spi = 1
 
 
'-------------------------------------------------------------------------------
' Config Clock
'-------------------------------------------------------------------------------
Dim Local_time As Long
Dim New_second As Bit
 
Config Date = Dmy , Separator = .
Config Clock = User
 
Config Timer1 = Timer , Prescale = 256
On Timer1 Timer_irq Saveall 'the SAVEALL option is NEEDED !
Const Timer_preload = 3036
Enable Timer1
 
'-------------------------------------------------------------------------------
' UDP
'-------------------------------------------------------------------------------
Dim Idx As Byte ' socket number
Dim Result As Word ' result
Dim S(255) As Byte
Dim R(255) As Byte
Dim Sstr As String * 255
Dim Temp As Byte , Temp2 As Byte ' temp bytes
Dim Ip As Long
 
'-------------------------------------------------------------------------------
' PING NTP SERVER to check if NTP server is available
'-------------------------------------------------------------------------------
#if Use_ping = 1
 Dim J As Byte , Res As Byte
 Dim Ii As Byte
 Dim Ping_check_ok As Bit
 
 ' FILL PING ARRAY
 S(1) = 8 'type is echo
 S(2) = 0 'code
 
 S(3) = 0 ' for checksum initialization
 S(4) = 0 ' checksum
 S(5) = 0 ' a signature can be any number
 S(6) = 1 ' signature
 S(7) = 0 ' sequence number - any number
 S(8) = 1
 S(9) = 65
 
 Dim W As Word At S + 2 Overlay 'same as dta(3) and dta(4)
 Dim B As Byte
 W = Tcpchecksum(s(1) , 9) ' calculate checksum and store in dta(3) and dta(4)
 
 'Ip = Maketcp(192.53.103.108) 'try to check this google server
 Print "PING NTP Server : " ; Ip2str(ip_sntp_server)
 
 'Print "Socket " ; Idx ; " " ; Idx
 Setipprotocol Idx , 1 'set protocol to 1
 'the protocol value must be set BEFORE the socket is openend
 
 Idx = Getsocket(idx , Sock_ipl_raw , 5000 , 0) 'RAW Socket
 
 ' We try the PING 3 times (but if we get an answer we exit the for loop)
 Do
 Incr Ii
 
 Result = Udpwrite(ip_sntp_server , 7 , Idx , S(1) , 9) 'write ping data '
 Print "Ping Number : " ; ii
 Waitms 200 ' depending on the hops, speed, etc
 Result = Socketstat(idx , Sel_recv) 'check for data
 ' Print "REC:" ; Result
 If Result >= 11 Then
 
 Res = Tcpread(idx , R(1) , Result) 'get data with TCPREAD !!!
 
 ' Print "DATA RETURNED :" ; Res
 '( '
 For J = 1 To Result
 Print R(j) ; " " ;
 Next
 Print
 ')
 Print "PING OK --> Exit Do..Loop"
 
 Set Ping_check_ok
 
 Exit Do ' If we can an answer from NTP Server exit do ... loop
 
 Else
 Print "NTP Server not available"
 End If
 Waitms 100
 
 Loop Until Ii = 3
 
 Ii = 0
#else
 Dim Ping_check_ok As Bit
 Ping_check_ok = 1
#endif
 
'-------------------------------------------------------------------------------
' SNTP
'-------------------------------------------------------------------------------
 
If Ping_check_ok = 1 Then 'When the NTP Serverer responded to PING
 'we need to get a socket first
 'note that for UDP we specify sock_dgram
 Idx = Getsocket(idx , Sock_dgram , 1000 , 0) ' get socket for UDP mode, specify port
 Print "Socket " ; Idx ; " " ; Idx
 
 Local_time = Sntp(idx , Ip_sntp_server) ' get time from SNTP server
 Print "Local_time = " ; Local_time
 
 If Local_time < 100 Then 'try it again
 Wait 1
 Local_time = Sntp(idx , Ip_sntp_server) ' get time from SNTP server
 Print "Second try ; Local_time = " ; Local_time
 End If
 
 Local_time = Local_time + 3600 ' (UTC/GMT) time + 3600
 Print Date(local_time) ; Spc(3) ; Time(local_time)
End If
 
'get a socket first
'note that for UDP we specify sock_dgram
Idx = Getsocket(idx , Sock_dgram , 1000 , 0) ' get socket for UDP mode, specify port
'Print "Socket " ; Idx ; " " ; Idx
 
 
Do
 If New_second = 1 Then
 Reset New_second
 'Do something every second ......
 End If
 
 'The UDP header give us info about Peersize, Peerport and peeraddress
 'which is the amount of data bytes received from peerport and peer ip address
 Udpreadheader Idx ' read the udp header (only the first 8 Bytes)
 
 If Peersize > 0 Then ' the actual number of bytes (without header bytes)
 Print "Size= " ; Peersize ; " By" ; " Port= " ; Peerport ; " Sender IP= " ; Ip2str(peeraddress) ; " Timestamp : " ; Date$ ; " / " ; Time$
 Temp = Udpread(idx , S(1) , Peersize) ' read the number of Bytes into Array S
 Print "DATA = " ;
 For Temp = 1 To Peersize
 Print Chr(s(temp)) ; ' print what we have received over USART to debug interface
 Next
 Print
 'Send it back to sender IP, Sender Port
 Result = Udpwrite(peeraddress , Peerport , Idx , S(1) , Peersize) ' write the received data back
 End If
Loop
 
End
 
Timer_irq:
 Incr Local_time
 Time$ = Time(local_time)
 Date$ = Date(local_time)
 Set New_second
Return
 
Getdatetime:
Return
 
Settime:
Return
 
Setdate:
Return


Author

MAK3

Personal tools
Namespaces
Variants
Actions
Navigation
Language