How to send an Email with Arduino and Ethernet Shield (W5100)

(Difference between revisions)
Jump to: navigation, search
(Under Construction)
Line 33: Line 33:
  
  
You need to change the  * (asterisk) !
+
You need to change the  * (asterisk) !
  
When you send the Email as HTML (with the Link) it might end up in your spam folder. So check also your spam folder for this email sent in HMTL format !
+
When you send the Email as HTML (with the Link) it might end up in your spam folder. So check also your spam folder for this email sent in HMTL format !
  
 
<br/><source lang="bascomavr">
 
<br/><source lang="bascomavr">
Line 49: Line 49:
  
 
' 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 NTP Server = 192.53.103.108
+
Const Ntp1 = 192 : Const Ntp2 = 53 : Const Ntp3 = 103 : Const Ntp4 = 108 ' Example NTP Server = 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 77: Line 77:
 
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
  
  
Wait 2 'Time to start the Debug Terminal after flashing
+
Wait 2 'Time to start the Debug Terminal after flashing
 
reset watchdog
 
reset watchdog
 
Print
 
Print
Line 93: Line 93:
 
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 116: Line 116:
  
 
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
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' SMTP Email Variables
+
' SMTP Email Variables
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
 
Dim Smtp_sock As Byte
 
Dim Smtp_sock As Byte
Line 128: Line 128:
 
Dim length as byte
 
Dim length as byte
  
Const Smtp_email_socket = 1     'Socket Number for SMTP
+
Const Smtp_email_socket = 1 'Socket Number for SMTP
  
  
  
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
' UDP Variables
+
' UDP Variables
 
'-------------------------------------------------------------------------------
 
'-------------------------------------------------------------------------------
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
  
  Reset Watchdog
+
Reset Watchdog
  
  ' 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
  
 
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' +
+
' +
' + SUB NAME:     Send_Email()
+
' + SUB NAME: Send_Email()
' +
+
' +
' + DESCRIPTION:   Send Email
+
' + DESCRIPTION: Send Email
' +
+
' +
' +
+
' +
' + RETURN:       None
+
' + RETURN: None
' +
+
' +
' + NOTES:         None
+
' + NOTES: None
' +
+
' +
 
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
'
 
'
Line 257: Line 257:
 
Sub Send_Email(byval Send_html As Byte) ' 0 = send Text, 1 = send HTML
 
Sub Send_Email(byval Send_html As Byte) ' 0 = send Text, 1 = send HTML
  
      '---------------------[SEND EMAIL SMTP]-----------------------------------------
+
'---------------------[SEND EMAIL SMTP]-----------------------------------------
    'for SMTP Email first we need a socket
+
'for SMTP Email first we need a socket
    idx = Getsocket(smtp_email_socket , Sock_stream , 4086 , 0)
+
idx = Getsocket(smtp_email_socket , Sock_stream , 4086 , 0)
    If idx = 255 Then print "SMTP Email Getsocket Error"
+
If idx = 255 Then print "SMTP Email Getsocket Error"
 
'(
 
'(
    result = Socketstat(idx , Sel_control)
+
result = Socketstat(idx , Sel_control)
  
    Select Case result
+
Select Case result
      Case Sock_closed : print "Sock_closedr"
+
Case Sock_closed : print "Sock_closedr"
      Case Sock_arp : print "SOCK_ARP"
+
Case Sock_arp : print "SOCK_ARP"
      Case Sock_listen : print "SOCK_LISTEN"
+
Case Sock_listen : print "SOCK_LISTEN"
      Case Sock_synsent : print "SOCK_SYNSENT"
+
Case Sock_synsent : print "SOCK_SYNSENT"
      Case Sock_synrecv : print "SOCK_SYNRECV"
+
Case Sock_synrecv : print "SOCK_SYNRECV"
      Case Sock_established : print "SOCK_ESTABLISHED"
+
Case Sock_established : print "SOCK_ESTABLISHED"
      Case Sock_close_wait : print "SOCK_CLOSE_WAIT"
+
Case Sock_close_wait : print "SOCK_CLOSE_WAIT"
      Case Sock_last_ack : print "SOCK_LAST_ACK"
+
Case Sock_last_ack : print "SOCK_LAST_ACK"
      Case Sock_fin_wait : print "SOCK_FIN_WAIT"
+
Case Sock_fin_wait : print "SOCK_FIN_WAIT"
      Case Sock_closing : print "SOCK_CLOSING"
+
Case Sock_closing : print "SOCK_CLOSING"
      Case Sock_time_wait : print "SOCK_TIME_WAIT"
+
Case Sock_time_wait : print "SOCK_TIME_WAIT"
      Case Sock_init : print "SOCK_INIT"
+
Case Sock_init : print "SOCK_INIT"
      Case Sock_udp : print "SOCK_UDP"
+
Case Sock_udp : print "SOCK_UDP"
      Case Sock_raw : print "SOCK_RAW"
+
Case Sock_raw : print "SOCK_RAW"
      Case Sock_macraw : print "SOCK_MACRAW"
+
Case Sock_macraw : print "SOCK_MACRAW"
      Case Sock_ppoe : print "SOCK_PPOE"
+
Case Sock_ppoe : print "SOCK_PPOE"
    End Select
+
End Select
 
')
 
')
  
Line 287: Line 287:
  
  
    'connect to smtp server
+
'connect to smtp server
      result = Socketconnect(idx , 213.165.64.21 , 25)     ' smtp server and SMTP port 25
+
result = Socketconnect(idx , 213.165.64.21 , 25) ' smtp server and SMTP port 25
      '                       ^socket
+
' ^socket
      '                                 ^ ip address of the smtp server
+
' ^ ip address of the smtp server
      '                                               ^ port 25 for smtp
+
' ^ port 25 for smtp
      print "Connecting to GMX Email Server: " ; " 213.165.64.21 Port 25 "
+
print "Connecting to GMX Email Server: " ; " 213.165.64.21 Port 25 "
  
      print "Socketconnect Status = " ; result
+
print "Socketconnect Status = " ; result
      If result = 1 Then print "ERROR connecting to 213.165.64.21 Port 25"
+
If result = 1 Then print "ERROR connecting to 213.165.64.21 Port 25"
  
      Reset Watchdog
+
Reset Watchdog
  
      Do
+
Do
            result = Socketstat(idx , 0)       ' get socket status
+
result = Socketstat(idx , 0) ' get socket status
      Loop Until result = Sock_established
+
Loop Until result = Sock_established
  
      print "SMTP = " ; "SOCK_ESTABLISHED"
+
print "SMTP = " ; "SOCK_ESTABLISHED"
  
      Reset Watchdog
+
Reset Watchdog
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "220" Then
+
If Left(email_string , 3) = "220" Then
            print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
            Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
  
      print "Send " ; "HELO Arduino Ethernet{013}{010}"
+
print "Send " ; "HELO Arduino Ethernet{013}{010}"
      Tcp_var = Tcpwrite(idx , "HELO Arduino Ethernet{013}{010}" )       ' send username
+
Tcp_var = Tcpwrite(idx , "HELO Arduino Ethernet{013}{010}" ) ' send username
      print Tcp_var ; " bytes written"               ' number of bytes actual send
+
print Tcp_var ; " bytes written" ' number of bytes actual send
  
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
  
          If Left(email_string , 3) = "250" Then
+
If Left(email_string , 3) = "250" Then
            print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
            Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
      Reset Watchdog
+
Reset Watchdog
  
  
      print "Send " ; "AUTH LOGIN{013}{010}"
+
print "Send " ; "AUTH LOGIN{013}{010}"
  
      Tcp_var = Tcpwrite(idx , "AUTH LOGIN{013}{010}" )       'authentication login
+
Tcp_var = Tcpwrite(idx , "AUTH LOGIN{013}{010}" ) 'authentication login
  
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "334" Then
+
If Left(email_string , 3) = "334" Then
              print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
              Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
      Reset Watchdog
+
Reset Watchdog
  
      'The username must be Base64 encoded (and add the CR + LF)!
+
'The username must be Base64 encoded (and add the CR + LF)!
      Tcp_var = Tcpwrite(idx , "**********************=={013}{010}" )     'USERNAME
+
Tcp_var = Tcpwrite(idx , "**********************=={013}{010}" ) 'USERNAME
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "334" Then
+
If Left(email_string , 3) = "334" Then
              print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
              Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
      Reset Watchdog
+
Reset Watchdog
  
      'The password must be Base64 encoded (and add the CR + LF) !
+
'The password must be Base64 encoded (and add the CR + LF) !
      Tcp_var = Tcpwrite(idx , "********************=={013}{010}" )       'PASSWORD
+
Tcp_var = Tcpwrite(idx , "********************=={013}{010}" ) 'PASSWORD
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "235" Then
+
If Left(email_string , 3) = "235" Then
              print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
              Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
  
      Reset Watchdog
+
Reset Watchdog
  
      'Your Email address
+
'Your Email address
      Tcp_var = Tcpwrite(idx , "MAIL FROM: *********@***.com{013}{010}")
+
Tcp_var = Tcpwrite(idx , "MAIL FROM: *********@***.com{013}{010}")
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "250" Then
+
If Left(email_string , 3) = "250" Then
                print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
                Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
      Reset Watchdog
+
Reset Watchdog
  
      'The Email address you want to send the message to
+
'The Email address you want to send the message to
      Tcp_var = Tcpwrite(idx , "RCPT TO: ******@*****.com{013}{010}")
+
Tcp_var = Tcpwrite(idx , "RCPT TO: ******@*****.com{013}{010}")
  
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "250" Then
+
If Left(email_string , 3) = "250" Then
              print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
              Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
  
      Reset Watchdog
+
Reset Watchdog
  
      Tcp_var = Tcpwrite(idx , "DATA{013}{010}" )
+
Tcp_var = Tcpwrite(idx , "DATA{013}{010}" )
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
              If Left(email_string , 3) = "354" Then
+
If Left(email_string , 3) = "354" Then
                  print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
                  Exit Do
+
Exit Do
              End If
+
End If
              'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
  
      Reset Watchdog
+
Reset Watchdog
  
  
      Tcp_var = Tcpwrite(idx , "FROM: ********@***.de{013}{010}")
+
Tcp_var = Tcpwrite(idx , "FROM: ********@***.de{013}{010}")
      Tcp_var = Tcpwrite(idx , "TO: *******@****.com{013}{010}")
+
Tcp_var = Tcpwrite(idx , "TO: *******@****.com{013}{010}")
      Tcp_var = Tcpwrite(idx , "SUBJECT: Arduino Ethernet Bascom{013}{010}")
+
Tcp_var = Tcpwrite(idx , "SUBJECT: Arduino Ethernet Bascom{013}{010}")
      Tcp_var = Tcpwrite(idx , "X-Mailer: BASCOM SMTP{013}{010}")
+
Tcp_var = Tcpwrite(idx , "X-Mailer: BASCOM SMTP{013}{010}")
  
      If Send_html = 1 Then
+
If Send_html = 1 Then
          '----------------------Send the Email as HTML ---------------------------
+
'----------------------Send the Email as HTML ---------------------------
  
          Tcp_var = Tcpwrite(idx , "Mime-Version: 1.0;{013}{010}")
+
Tcp_var = Tcpwrite(idx , "Mime-Version: 1.0;{013}{010}")
          Tcp_var = Tcpwrite(idx , "Content-Type: text/html; charset={034}ISO-8859-1{034};{013}{010}")
+
Tcp_var = Tcpwrite(idx , "Content-Type: text/html; charset={034}ISO-8859-1{034};{013}{010}")
          Tcp_var = Tcpwrite(idx , "Content-Transfer-Encoding: 7bit;{013}{010}")
+
Tcp_var = Tcpwrite(idx , "Content-Transfer-Encoding: 7bit;{013}{010}")
  
          Tcp_var = Tcpwrite(idx , "{013}{010}")     'Empty line to switch to body
+
Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch to body
  
          Tcp_var = Tcpwrite(idx , "<html>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "<html>{013}{010}")
          Tcp_var = Tcpwrite(idx , "<body>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "<body>{013}{010}")
          Tcp_var = Tcpwrite(idx , "<h2>The Link to Bascom Forum !</h2>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "<h2>The Link to Bascom Forum !</h2>{013}{010}")
          Tcp_var = Tcpwrite(idx , "Here is it: <a href={034}http://www.mcselec.com/index2.php?option=com_forum&Itemid=59{034}>Bascom Forum</a>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "Here is it: <a href={034}http://www.mcselec.com/index2.php?option=com_forum&Itemid=59{034}>Bascom Forum</a>{013}{010}")
          Tcp_var = Tcpwrite(idx , "<p>Best Regards,</p>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "<p>Best Regards,</p>{013}{010}")
          Tcp_var = Tcpwrite(idx , "<p>Arduino Ethernet Bascom</p>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "<p>Arduino Ethernet Bascom</p>{013}{010}")
          Tcp_var = Tcpwrite(idx , "</body>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "</body>{013}{010}")
          Tcp_var = Tcpwrite(idx , "</html>{013}{010}")
+
Tcp_var = Tcpwrite(idx , "</html>{013}{010}")
          Tcp_var = Tcpwrite(idx , ".{013}{010}")
+
Tcp_var = Tcpwrite(idx , ".{013}{010}")
      Else
+
Else
  
            '-----------------------Send only TEXT-----------------------------------
+
'-----------------------Send only TEXT-----------------------------------
  
          Tcp_var = Tcpwrite(idx , "{013}{010}")     'Empty line to switch from subject to body
+
Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch from subject to body
          Tcp_var = Tcpwrite(idx , "This is a test email from BASCOM-AVR SMTP{013}{010}")
+
Tcp_var = Tcpwrite(idx , "This is a test email from BASCOM-AVR SMTP{013}{010}")
          Tcp_var = Tcpwrite(idx , "Arduino Ethernet Bascom{013}{010}")
+
Tcp_var = Tcpwrite(idx , "Arduino Ethernet Bascom{013}{010}")
          email_string = "Timestamp = " + time$ + " / " + date$ + "{013}{010}"
+
email_string = "Timestamp = " + time$ + " / " + date$ + "{013}{010}"
          length = len(email_string)
+
length = len(email_string)
          Tcp_var = Tcpwrite(idx , email_string, length)
+
Tcp_var = Tcpwrite(idx , email_string, length)
          Tcp_var = Tcpwrite(idx , ".{013}{010}")     ' end with a single dot
+
Tcp_var = Tcpwrite(idx , ".{013}{010}") ' end with a single dot
      end if
+
end if
  
      Reset Watchdog
+
Reset Watchdog
  
      Do
+
Do
          Tcp_var = Tcpread(idx , email_string)                 ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "250" Then
+
If Left(email_string , 3) = "250" Then
                  print "Server Answer = " ; email_string         '--> 250 2.6.0 Message accepted
+
print "Server Answer = " ; email_string '--> 250 2.6.0 Message accepted
                  Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
  
      print "Send " ; "QUIT{013}{010}"
+
print "Send " ; "QUIT{013}{010}"
      Tcp_var = Tcpwrite(idx , "QUIT{013}{010}")
+
Tcp_var = Tcpwrite(idx , "QUIT{013}{010}")
  
  
      Do
+
Do
        Tcp_var = Tcpread(idx , email_string)                   ' get response from SMTP server
+
Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
          If Left(email_string , 3) = "221" Then
+
If Left(email_string , 3) = "221" Then
              print "Server Answer = " ; email_string
+
print "Server Answer = " ; email_string
              Exit Do
+
Exit Do
          End If
+
End If
          'The Watchdog will restart the micro in case of no or wrong answer
+
'The Watchdog will restart the micro in case of no or wrong answer
      Loop
+
Loop
  
      Print "Close Socket"
+
Print "Close Socket"
      Closesocket Idx                                 ' close the server connection
+
Closesocket Idx ' close the server connection
  
 
End Sub
 
End Sub
Line 499: Line 499:
 
'Here we send the email
 
'Here we send the email
  
call Send_Email(0)       'Send text Email
+
call Send_Email(0) 'Send text Email
 
wait 2 : reset watchdog
 
wait 2 : reset watchdog
call Send_Email(1)       'Send Email in HTML
+
call Send_Email(1) 'Send Email in HTML
  
  
 
Do
 
Do
    If New_second = 1 Then
+
If New_second = 1 Then
      Reset New_second
+
Reset New_second
      reset watchdog
+
reset watchdog
  
  
      'Do something every second ......
+
'Do something every second ......
  
      print "Tick"
+
print "Tick"
  
      incr second_counter
+
incr second_counter
      if second_counter = 180 then
+
if second_counter = 180 then
        second_counter = 0
+
second_counter = 0
        'Do something every 3 minutes ......
+
'Do something every 3 minutes ......
      end if
+
end if
    End If
+
End If
 
Loop
 
Loop
  
Line 525: Line 525:
  
 
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 543: Line 543:
 
== Terminal Ouput of Example<br/> ==
 
== Terminal Ouput of Example<br/> ==
  
<span style="color:#0000ff"><br/>-----START Arduino UNO R3 + Ethernet Shield EMAIL DEMO-----<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 = 416239374<br/>10.03.13&nbsp;&nbsp; 15:02:54<br/>Connecting to GMX Email Server:&nbsp; 213.165.64.21 Port 25<br/>Socketconnect Status = 0<br/>SMTP = SOCK_ESTABLISHED<br/>Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}<br/>Send HELO Arduino Ethernet<br/><br/>23&nbsp; bytes written<br/>Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}<br/>Send AUTH LOGIN<br/><br/>Server Answer = 334 VXNlcm5hbWU6<br/>Server Answer = 334 UGFzc3dvcmQ6<br/>Server Answer = 235 2.7.0 Go ahead {mp033}<br/>Server Answer = 250 2.1.0 ok {mp033}<br/>Server Answer = 250 2.1.5 ok {mp033}<br/>Server Answer = 354 mail.gmx.net Go ahead {mp033}<br/>Server Answer = 250 2.6.0 Message accepted {mp033}<br/>Send QUIT<br/><br/>Server Answer = 221 2.0.0 GMX Mailservices {mp033}<br/>Close Socket<br/>Connecting to GMX Email Server:&nbsp; 213.165.64.21 Port 25<br/>Socketconnect Status = 0<br/>SMTP = SOCK_ESTABLISHED<br/>Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}<br/>Send HELO Arduino Ethernet<br/><br/>23&nbsp; bytes written<br/>Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}<br/>Send AUTH LOGIN<br/><br/>Server Answer = 334 VXNlcm5hbWU6<br/>Server Answer = 334 UGFzc3dvcmQ6<br/>Server Answer = 235 2.7.0 Go ahead {mp033}<br/>Server Answer = 250 2.1.0 ok {mp033}<br/>Server Answer = 250 2.1.5 ok {mp033}<br/>Server Answer = 354 mail.gmx.net Go ahead {mp033}<br/>Server Answer = 250 2.6.0 Message accepted {mp033}<br/>Send QUIT<br/><br/>Server Answer = 221 2.0.0 GMX Mailservices {mp033}<br/>Close Socket<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/></span><br/>
+
<br/><span style="color:#0000ff">-----START Arduino UNO R3 + Ethernet Shield EMAIL DEMO-----<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 = 416239374<br/>10.03.13&nbsp;&nbsp; 15:02:54<br/>Connecting to GMX Email Server:&nbsp; 213.165.64.21 Port 25<br/>Socketconnect Status = 0<br/>SMTP = SOCK_ESTABLISHED<br/>Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}<br/>Send HELO Arduino Ethernet<br/><br/>23&nbsp; bytes written<br/>Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}<br/>Send AUTH LOGIN<br/><br/>Server Answer = 334 VXNlcm5hbWU6<br/>Server Answer = 334 UGFzc3dvcmQ6<br/>Server Answer = 235 2.7.0 Go ahead {mp033}<br/>Server Answer = 250 2.1.0 ok {mp033}<br/>Server Answer = 250 2.1.5 ok {mp033}<br/>Server Answer = 354 mail.gmx.net Go ahead {mp033}<br/>Server Answer = 250 2.6.0 Message accepted {mp033}<br/>Send QUIT<br/><br/>Server Answer = 221 2.0.0 GMX Mailservices {mp033}<br/>Close Socket<br/>Connecting to GMX Email Server:&nbsp; 213.165.64.21 Port 25<br/>Socketconnect Status = 0<br/>SMTP = SOCK_ESTABLISHED<br/>Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}<br/>Send HELO Arduino Ethernet<br/><br/>23&nbsp; bytes written<br/>Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}<br/>Send AUTH LOGIN<br/><br/>Server Answer = 334 VXNlcm5hbWU6<br/>Server Answer = 334 UGFzc3dvcmQ6<br/>Server Answer = 235 2.7.0 Go ahead {mp033}<br/>Server Answer = 250 2.1.0 ok {mp033}<br/>Server Answer = 250 2.1.5 ok {mp033}<br/>Server Answer = 354 mail.gmx.net Go ahead {mp033}<br/>Server Answer = 250 2.6.0 Message accepted {mp033}<br/>Send QUIT<br/><br/>Server Answer = 221 2.0.0 GMX Mailservices {mp033}<br/>Close Socket<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick<br/>Tick</span><br/>
  
 
== Telnet Exercise with sending an Email (if you encounter a problem)<br/> ==
 
== Telnet Exercise with sending an Email (if you encounter a problem)<br/> ==
Line 559: Line 559:
 
<br/>211 System status, or system help reply.<br/>214 Help message.<br/>220 Domain service ready.Ready to start TLS.<br/>221 Domain service closing transmission channel.<br/>250 Ok , Queuing For Node Node Started.requested Mail Action Okay , Completed.<br/>251 Ok , No Messages Waiting For Node Node.user Not Local , Will Forward To Forwardpath.<br/>252 OK, pending messages for node node started.Cannot VRFY user (e.g., info is not local), but will take message for this user and attempt delivery.<br/>253 OK, messages pending messages for node node started.<br/>354 Start mail input; end with &lt;CRLF&gt;.&lt;CRLF&gt;.<br/>355 Octet-offset is the transaction offset.<br/>421 Domain Service Not Available , Closing Transmission Channel.<br/>432 A password transition is needed.<br/>450 Requested mail action not taken: mailbox unavailable.ATRN request refused.<br/>451 Requested action aborted: local error in processing.Unable to process ATRN request now<br/>452 Requested action not taken: insufficient system storage.<br/>453 You have no mail.<br/>454 TLS not available due to temporary reason.Encryption required for requested authentication mechanism.<br/>458 Unable to queue messages for node node.<br/>459 Node node not allowed: reason.<br/>500 Command not recognized: command.Syntax error.<br/>501 Syntax error, no parameters allowed.<br/>502 Command not implemented.<br/>503 Bad sequence of commands.<br/>504 Command parameter not implemented.<br/>521 Machine does not accept mail.<br/>530 Must issue a STARTTLS command first.Encryption required for requested authentication mechanism.<br/>534 Authentication mechanism is too weak.<br/>538 Encryption required for requested authentication mechanism.<br/>550 Requested action not taken: mailbox unavailable.<br/>551 User not local; please try forwardpath.<br/>552 Requested mail action aborted: exceeded storage allocation.<br/>553 Requested action not taken: mailbox name not allowed.<br/>554 Transaction failed.
 
<br/>211 System status, or system help reply.<br/>214 Help message.<br/>220 Domain service ready.Ready to start TLS.<br/>221 Domain service closing transmission channel.<br/>250 Ok , Queuing For Node Node Started.requested Mail Action Okay , Completed.<br/>251 Ok , No Messages Waiting For Node Node.user Not Local , Will Forward To Forwardpath.<br/>252 OK, pending messages for node node started.Cannot VRFY user (e.g., info is not local), but will take message for this user and attempt delivery.<br/>253 OK, messages pending messages for node node started.<br/>354 Start mail input; end with &lt;CRLF&gt;.&lt;CRLF&gt;.<br/>355 Octet-offset is the transaction offset.<br/>421 Domain Service Not Available , Closing Transmission Channel.<br/>432 A password transition is needed.<br/>450 Requested mail action not taken: mailbox unavailable.ATRN request refused.<br/>451 Requested action aborted: local error in processing.Unable to process ATRN request now<br/>452 Requested action not taken: insufficient system storage.<br/>453 You have no mail.<br/>454 TLS not available due to temporary reason.Encryption required for requested authentication mechanism.<br/>458 Unable to queue messages for node node.<br/>459 Node node not allowed: reason.<br/>500 Command not recognized: command.Syntax error.<br/>501 Syntax error, no parameters allowed.<br/>502 Command not implemented.<br/>503 Bad sequence of commands.<br/>504 Command parameter not implemented.<br/>521 Machine does not accept mail.<br/>530 Must issue a STARTTLS command first.Encryption required for requested authentication mechanism.<br/>534 Authentication mechanism is too weak.<br/>538 Encryption required for requested authentication mechanism.<br/>550 Requested action not taken: mailbox unavailable.<br/>551 User not local; please try forwardpath.<br/>552 Requested mail action aborted: exceeded storage allocation.<br/>553 Requested action not taken: mailbox name not allowed.<br/>554 Transaction failed.
  
== Under Construction<br/> ==
 
  
......
 
  
 
[[Category:Arduino Ethernet with Bascom-AVR]]
 
[[Category:Arduino Ethernet with Bascom-AVR]]

Revision as of 15:21, 10 March 2013

Contents

Used Hardware

  • Arduino UNO R3
  • Ethernet Shield with W5100 Wiznet Chip  

Used Software

Bascom-AVR 2.0.7.6

Windows7 or 8


What you need to run the example

For the following example you need:

  • Email account which supportSMTP Authentication (AUTH LOGIN) on Port 25
  • Username and password Base64 encoded (you can use the Easy TCP/IP tool in Bascom-AVR to encode and decode)
  • IP address of your smtp server (if you do not know it just open in Windows the DOS command window and ping the server)
  • Your sender Email address and the receiver Email address

Don't use the Email Sub in the Do...Loop until you know what you are doing (Your mail provider could ban you if you send too much emails) !


This example use the base structure from here: http://wiki.mcselec.com/Getting_started_with_Arduino_UNO_R3_and_Ethernet_Shield_(W5100)


The focus of this article is Email and not other topics which are already descibed in other articels in this WIKI.


You need to change the  * (asterisk) !

When you send the Email as HTML (with the Link) it might end up in your spam folder. So check also your spam folder for this email sent in HMTL format !


' Send Email with Arduino Ethernet (W5100)
' Tested with GMX
 
 
'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 NTP Server = 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 submode = new
 
Config Com1 = 57600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
 
'-------------------------------------------------------------------------------
' Watchdog
'-------------------------------------------------------------------------------
config watchdog = 4096
Start watchdog
 
 
'-------------------------------------------------------------------------------
' 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 'Time to start the Debug Terminal after flashing
reset watchdog
Print
Print "-----START Arduino UNO R3 + Ethernet Shield EMAIL DEMO-----"
'-------------------------------------------------------------------------------
' Network Hardware
'-------------------------------------------------------------------------------
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
Spiinit
 
 
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
dim second_counter as byte
 
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
 
'-------------------------------------------------------------------------------
' SMTP Email Variables
'-------------------------------------------------------------------------------
Dim Smtp_sock As Byte
Dim Tcp_var As Byte
Dim email_string As String * 130
Dim length as byte
 
Const Smtp_email_socket = 1 'Socket Number for SMTP
 
 
 
'-------------------------------------------------------------------------------
' UDP Variables
'-------------------------------------------------------------------------------
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
 
 Reset Watchdog
 
 ' 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
 
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' +
' + SUB NAME: Send_Email()
' +
' + DESCRIPTION: Send Email
' +
' +
' + RETURN: None
' +
' + NOTES: None
' +
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'
 
 
Sub Send_Email(byval Send_html As Byte) ' 0 = send Text, 1 = send HTML
 
 '---------------------[SEND EMAIL SMTP]-----------------------------------------
 'for SMTP Email first we need a socket
 idx = Getsocket(smtp_email_socket , Sock_stream , 4086 , 0)
 If idx = 255 Then print "SMTP Email Getsocket Error"
'(
 result = Socketstat(idx , Sel_control)
 
 Select Case result
 Case Sock_closed : print "Sock_closedr"
 Case Sock_arp : print "SOCK_ARP"
 Case Sock_listen : print "SOCK_LISTEN"
 Case Sock_synsent : print "SOCK_SYNSENT"
 Case Sock_synrecv : print "SOCK_SYNRECV"
 Case Sock_established : print "SOCK_ESTABLISHED"
 Case Sock_close_wait : print "SOCK_CLOSE_WAIT"
 Case Sock_last_ack : print "SOCK_LAST_ACK"
 Case Sock_fin_wait : print "SOCK_FIN_WAIT"
 Case Sock_closing : print "SOCK_CLOSING"
 Case Sock_time_wait : print "SOCK_TIME_WAIT"
 Case Sock_init : print "SOCK_INIT"
 Case Sock_udp : print "SOCK_UDP"
 Case Sock_raw : print "SOCK_RAW"
 Case Sock_macraw : print "SOCK_MACRAW"
 Case Sock_ppoe : print "SOCK_PPOE"
 End Select
')
 
 
 
 
 'connect to smtp server
 result = Socketconnect(idx , 213.165.64.21 , 25) ' smtp server and SMTP port 25
 ' ^socket
 ' ^ ip address of the smtp server
 ' ^ port 25 for smtp
 print "Connecting to GMX Email Server: " ; " 213.165.64.21 Port 25 "
 
 print "Socketconnect Status = " ; result
 If result = 1 Then print "ERROR connecting to 213.165.64.21 Port 25"
 
 Reset Watchdog
 
 Do
 result = Socketstat(idx , 0) ' get socket status
 Loop Until result = Sock_established
 
 print "SMTP = " ; "SOCK_ESTABLISHED"
 
 Reset Watchdog
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "220" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 
 print "Send " ; "HELO Arduino Ethernet{013}{010}"
 Tcp_var = Tcpwrite(idx , "HELO Arduino Ethernet{013}{010}" ) ' send username
 print Tcp_var ; " bytes written" ' number of bytes actual send
 
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 
 If Left(email_string , 3) = "250" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 Reset Watchdog
 
 
 print "Send " ; "AUTH LOGIN{013}{010}"
 
 Tcp_var = Tcpwrite(idx , "AUTH LOGIN{013}{010}" ) 'authentication login
 
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "334" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 Reset Watchdog
 
 'The username must be Base64 encoded (and add the CR + LF)!
 Tcp_var = Tcpwrite(idx , "**********************=={013}{010}" ) 'USERNAME
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "334" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 Reset Watchdog
 
 'The password must be Base64 encoded (and add the CR + LF) !
 Tcp_var = Tcpwrite(idx , "********************=={013}{010}" ) 'PASSWORD
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "235" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 
 Reset Watchdog
 
 'Your Email address
 Tcp_var = Tcpwrite(idx , "MAIL FROM: *********@***.com{013}{010}")
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "250" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 Reset Watchdog
 
 'The Email address you want to send the message to
 Tcp_var = Tcpwrite(idx , "RCPT TO: ******@*****.com{013}{010}")
 
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "250" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 
 Reset Watchdog
 
 Tcp_var = Tcpwrite(idx , "DATA{013}{010}" )
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "354" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 
 Reset Watchdog
 
 
 Tcp_var = Tcpwrite(idx , "FROM: ********@***.de{013}{010}")
 Tcp_var = Tcpwrite(idx , "TO: *******@****.com{013}{010}")
 Tcp_var = Tcpwrite(idx , "SUBJECT: Arduino Ethernet Bascom{013}{010}")
 Tcp_var = Tcpwrite(idx , "X-Mailer: BASCOM SMTP{013}{010}")
 
 If Send_html = 1 Then
 '----------------------Send the Email as HTML ---------------------------
 
 Tcp_var = Tcpwrite(idx , "Mime-Version: 1.0;{013}{010}")
 Tcp_var = Tcpwrite(idx , "Content-Type: text/html; charset={034}ISO-8859-1{034};{013}{010}")
 Tcp_var = Tcpwrite(idx , "Content-Transfer-Encoding: 7bit;{013}{010}")
 
 Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch to body
 
 Tcp_var = Tcpwrite(idx , "<html>{013}{010}")
 Tcp_var = Tcpwrite(idx , "<body>{013}{010}")
 Tcp_var = Tcpwrite(idx , "<h2>The Link to Bascom Forum !</h2>{013}{010}")
 Tcp_var = Tcpwrite(idx , "Here is it: <a href={034}http://www.mcselec.com/index2.php?option=com_forum&Itemid=59{034}>Bascom Forum</a>{013}{010}")
 Tcp_var = Tcpwrite(idx , "<p>Best Regards,</p>{013}{010}")
 Tcp_var = Tcpwrite(idx , "<p>Arduino Ethernet Bascom</p>{013}{010}")
 Tcp_var = Tcpwrite(idx , "</body>{013}{010}")
 Tcp_var = Tcpwrite(idx , "</html>{013}{010}")
 Tcp_var = Tcpwrite(idx , ".{013}{010}")
 Else
 
 '-----------------------Send only TEXT-----------------------------------
 
 Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch from subject to body
 Tcp_var = Tcpwrite(idx , "This is a test email from BASCOM-AVR SMTP{013}{010}")
 Tcp_var = Tcpwrite(idx , "Arduino Ethernet Bascom{013}{010}")
 email_string = "Timestamp = " + time$ + " / " + date$ + "{013}{010}"
 length = len(email_string)
 Tcp_var = Tcpwrite(idx , email_string, length)
 Tcp_var = Tcpwrite(idx , ".{013}{010}") ' end with a single dot
 end if
 
 Reset Watchdog
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "250" Then
 print "Server Answer = " ; email_string '--> 250 2.6.0 Message accepted
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 
 print "Send " ; "QUIT{013}{010}"
 Tcp_var = Tcpwrite(idx , "QUIT{013}{010}")
 
 
 Do
 Tcp_var = Tcpread(idx , email_string) ' get response from SMTP server
 If Left(email_string , 3) = "221" Then
 print "Server Answer = " ; email_string
 Exit Do
 End If
 'The Watchdog will restart the micro in case of no or wrong answer
 Loop
 
 Print "Close Socket"
 Closesocket Idx ' close the server connection
 
End Sub
 
 
 
 
 
reset watchdog
 
'Here we send the email
 
call Send_Email(0) 'Send text Email
wait 2 : reset watchdog
call Send_Email(1) 'Send Email in HTML
 
 
Do
 If New_second = 1 Then
 Reset New_second
 reset watchdog
 
 
 'Do something every second ......
 
 print "Tick"
 
 incr second_counter
 if second_counter = 180 then
 second_counter = 0
 'Do something every 3 minutes ......
 end if
 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

Terminal Ouput of Example


-----START Arduino UNO R3 + Ethernet Shield EMAIL DEMO-----
PING NTP Server : 192.53.103.108
Ping Number : 1
PING OK --> Exit Do..Loop
Socket 0 0
Local_time = 416239374
10.03.13   15:02:54
Connecting to GMX Email Server:  213.165.64.21 Port 25
Socketconnect Status = 0
SMTP = SOCK_ESTABLISHED
Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}
Send HELO Arduino Ethernet

23  bytes written
Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}
Send AUTH LOGIN

Server Answer = 334 VXNlcm5hbWU6
Server Answer = 334 UGFzc3dvcmQ6
Server Answer = 235 2.7.0 Go ahead {mp033}
Server Answer = 250 2.1.0 ok {mp033}
Server Answer = 250 2.1.5 ok {mp033}
Server Answer = 354 mail.gmx.net Go ahead {mp033}
Server Answer = 250 2.6.0 Message accepted {mp033}
Send QUIT

Server Answer = 221 2.0.0 GMX Mailservices {mp033}
Close Socket
Connecting to GMX Email Server:  213.165.64.21 Port 25
Socketconnect Status = 0
SMTP = SOCK_ESTABLISHED
Server Answer = 220 mail.gmx.net GMX Mailservices ESMTP {mp033}
Send HELO Arduino Ethernet

23  bytes written
Server Answer = 250 mail.gmx.net GMX Mailservices {mp033}
Send AUTH LOGIN

Server Answer = 334 VXNlcm5hbWU6
Server Answer = 334 UGFzc3dvcmQ6
Server Answer = 235 2.7.0 Go ahead {mp033}
Server Answer = 250 2.1.0 ok {mp033}
Server Answer = 250 2.1.5 ok {mp033}
Server Answer = 354 mail.gmx.net Go ahead {mp033}
Server Answer = 250 2.6.0 Message accepted {mp033}
Send QUIT

Server Answer = 221 2.0.0 GMX Mailservices {mp033}
Close Socket
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick

Telnet Exercise with sending an Email (if you encounter a problem)

The AUTH LOGIN smtp command is used to login to the smtp server with Username and password (both must be sent Base64 encoded).

You can use the Easy TCP/IP Tool in Bascom-AVR to decode or encode Base64.


You can check all the single steps of the SMTP server communication with TELNET in a manual procedure:


1. Enabling Telnet Client in Windows 7
   http://social.technet.microsoft.com/wiki/contents/articles/910.enabling-telnet-client-in-windows-7.aspx
2. Start the DOS command window in Windows 7 and type telnet and return
3. Then you see:         Microsoft Telnet>         in the DOS command window.
4. Now type (we use as example GMX with Port 25):             open smtp.gmx.net 25             (25 is the port number)
5. The answer from the SMTP server will be: 220 mail.gmx.net GMX Mailservices ESMTP {mpXXX}
6. Then type:         AUTH LOGIN
7. The answer from the SMTP server will be: 334 VXNlcm5hbWU6
8. If you decode the   VXNlcm5hbWU6   for example with the Easy TCP/IP Tool in Bascom-AVR
    you will notice that the decoded text is Username:
9. You need now to type in the username Base64 encoded (which is usually your Email address Base64 encoded)
10. After this the SMTP server ask for the password (also Base64 encoded)
11. If you type in the wrong username or password the server's answer is 535 5.7.0 Incorrect username or password {mpXXX}
12. The correct username and password will be answerd by:   235 2.7.0 Go ahead {mpXXX}
13. ...
14. You can start the Email content with DATA and end it with <CR><LF>.<CR><LF> which is in Bascom {013}{010}.{013}{010}

SMTP Server Reply Codes

Code Description


211 System status, or system help reply.
214 Help message.
220 Domain service ready.Ready to start TLS.
221 Domain service closing transmission channel.
250 Ok , Queuing For Node Node Started.requested Mail Action Okay , Completed.
251 Ok , No Messages Waiting For Node Node.user Not Local , Will Forward To Forwardpath.
252 OK, pending messages for node node started.Cannot VRFY user (e.g., info is not local), but will take message for this user and attempt delivery.
253 OK, messages pending messages for node node started.
354 Start mail input; end with <CRLF>.<CRLF>.
355 Octet-offset is the transaction offset.
421 Domain Service Not Available , Closing Transmission Channel.
432 A password transition is needed.
450 Requested mail action not taken: mailbox unavailable.ATRN request refused.
451 Requested action aborted: local error in processing.Unable to process ATRN request now
452 Requested action not taken: insufficient system storage.
453 You have no mail.
454 TLS not available due to temporary reason.Encryption required for requested authentication mechanism.
458 Unable to queue messages for node node.
459 Node node not allowed: reason.
500 Command not recognized: command.Syntax error.
501 Syntax error, no parameters allowed.
502 Command not implemented.
503 Bad sequence of commands.
504 Command parameter not implemented.
521 Machine does not accept mail.
530 Must issue a STARTTLS command first.Encryption required for requested authentication mechanism.
534 Authentication mechanism is too weak.
538 Encryption required for requested authentication mechanism.
550 Requested action not taken: mailbox unavailable.
551 User not local; please try forwardpath.
552 Requested mail action aborted: exceeded storage allocation.
553 Requested action not taken: mailbox name not allowed.
554 Transaction failed.

Personal tools
Namespaces
Variants
Actions
Navigation
Language