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

(Difference between revisions)
Jump to: navigation, search
Line 268: Line 268:
 
== Terminal Output of example:<br/> ==
 
== Terminal Output of example:<br/> ==
  
-----START Arduino UNO R3 + Ethernet Shield DEMO-----<br/><br/>PING NTP Server : 192.53.103.108<br/>Ping Number : 1<br/>PING OK --> Exit Do..Loop<br/>Socket 0 0<br/>Local_time = 416063775<br/>08.03.13&nbsp;&nbsp; 14:16:15<br/>Size= 5 By&nbsp; Port= 50000 Sender IP= 192.168.2.100&nbsp; Timestamp : 08.03.13 / 14:16:45<br/>DATA = Hello<br/>
+
When using Easy TCP/IP and sending the text "Hello".
 +
 
 +
 
 +
 
 +
----
 +
 
 +
START Arduino UNO R3 + Ethernet Shield DEMO-----<br/><br/>PING NTP Server&nbsp;: 192.53.103.108<br/>Ping Number&nbsp;: 1<br/>PING OK --> Exit Do..Loop<br/>Socket 0 0<br/>Local_time = 416063775<br/>08.03.13&nbsp;&nbsp; 14:16:15<br/>Size= 5 By&nbsp; Port= 50000 Sender IP= 192.168.2.100&nbsp; Timestamp&nbsp;: 08.03.13 / 14:16:45<br/>DATA = Hello
  
 
== Author<br/> ==
 
== Author<br/> ==

Revision as of 14:37, 8 March 2013

Contents

Please read first this Article:

Getting started with Arduino and WIZ820io http://wiki.mcselec.com/Getting_started_with_Arduino_and_WIZ820io


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
See Getting started with Arduino and WIZ820io http://wiki.mcselec.com/Getting_started_with_Arduino_and_WIZ820io

to know how to use this example code.


Terminal Output of example:

When using Easy TCP/IP and sending the text "Hello".



START Arduino UNO R3 + Ethernet Shield DEMO-----

PING NTP Server : 192.53.103.108
Ping Number : 1
PING OK --> Exit Do..Loop
Socket 0 0
Local_time = 416063775
08.03.13   14:16:15
Size= 5 By  Port= 50000 Sender IP= 192.168.2.100  Timestamp : 08.03.13 / 14:16:45
DATA = Hello

Author

MAK3

Personal tools
Namespaces
Variants
Actions
Navigation
Language