Wiznet makers

ronpang

Published December 11, 2023 ©

125 UCC

10 WCC

32 VAR

0 Contests

1 Followers

0 Following

Original Link

22. W5100S/W5500+RP2040 Raspberry Pi Pico<SMTP Send Email>

22. W5100S/W5500+RP2040 Raspberry Pi Pico<SMTP Send Email>

COMPONENTS Hardware components

WIZnet - W5100S-EVB-Pico

x 1


WIZnet - W5500-EVB-Pico

x 1


PROJECT DESCRIPTION

1 Introduction

In today's information age, the Internet has become an indispensable part of people's daily lives. As the most basic and widespread service in Internet applications, email's transmission protocol, SMTP, naturally plays a pivotal role. This article will give you an in-depth understanding of the definition, characteristics, application scenarios and protocol model of the SMTP protocol, helping you better understand this important tool. .

 W5100S/W5500 is an embedded Ethernet controller integrating a full hardware TCP/IP protocol stack. It is also an industrial-grade Ethernet control chip. This tutorial will introduce the basic principles, usage steps, application examples and precautions of W5100S/W5500 Ethernet FTP application to help readers better master this technology.

2 Protocol Introduction

2 .1 What is SMTP?

SMTP Simple Mail Transfer Protocol is an application layer protocol that can transfer email between different servers and computer networks. It does this by defining communication rules.

The original model was launched in 1982. According to RFC 821, the user creates a connection request. In response, the sending SMTP initiates a bidirectional connection with the receiving SMTP. In modern terms, they are SMTP client and SMTP server respectively. SMTP clients and SMTP servers communicate using commands and responses (more on this later), similar to real-life conversations.

RFC 821 also defines the model used by SMTP. You can see the scheme in the image below.

2.2 How does SMTP work?

An SMTP session begins when a client opens a Transmission Control Protocol connection (TCP connection, sometimes called TCP/IP) to an SMTP server. The server responds with an open message, represented by code 220. This process is often called the SMTP handshake.

Then, the client sends the HELO command and identifies itself. It usually follows the command with a domain name or IP address. In non-technical terms, the customer says, "Hello, my name is Customer John, I'm sending emails from gmail.com and my IP is 192.0.2.0". The server responds with code 250.

After that, the email transfer phase will begin. The email content will be transferred incrementally using the MAIL FROM (john@gmail.com), RCPT TO (oliver@gmail.com), and DATA ("Hey, how are you?") commands. If accepted by the server, the client transmits the email headers. Once everything has been transferred, an end-of-line indicator must be used.

If sent successfully, the server responds with code 250. The client will initiate the termination of the SMTP connection using the QUIT command and the server will close the transport channel with code 221.

2.3 SMTP、IMAP和POP3

Besides SMTP, the most common email protocols are IMAP and POP3. SMTP is an outbound email server used to send and deliver email. IMAP and POP3 are mail access protocols used to retrieve incoming mail from email servers.

IMAP is an Internet Mail Access Protocol that connects to a server and downloads mail upon request. Messages are not deleted after the connection is terminated. Using IMAP, users can access their email from any computer or device.

POP3 or Post Office Protocol 3 also connects to the server, but it downloads all incoming mail. Once completed, it will delete all emails from the server. Unlike IMAP, POP3 is device dependent.

2.4 SMTP application scenarios

The SMTP protocol is widely used in the following scenarios:

1. Secure transmission of internal corporate emails

2. Secure communication for online banking transactions

3. Transaction confirmation on e-commerce platform

4. Account verification for social networks

5.Subscription notifications from news media

6. Communication bridge between enterprises and customers

 

3 WIZnet Ethernet chip

WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison

ModelEmbedded CoreHost I/FTX/RX BufferHW SocketNetwork Performance
W5100STCP/IPv4, MAC & PHY8bit BUS, SPI16KB4Max.25Mbps
W6100TCP/IPv4/IPv6, MAC & PHY8bit BUS, Fast SPI32KB8Max.25Mbps
W5500TCP/IPv4, MAC & PHYFast SPI32KB8Max 15Mbps

W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.

W6100 supports IPv6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.

W5500 has more Sockets and send and receive buffers than W5100S.

4 Overview and usage of SMTP email sending examples

4.1 Flowchart

The running block diagram of the program is as follows:

4.2 Core preparation work

Software

Visual Studio Code

WIZnet UartTool

Hardware

W5100S IO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board

Micro USB interface data cable

TTL to USB

cable

4.3 Connection method

Connect the USB port of the PC through the data cable (mainly used for burning programs, but can also be used as a virtual serial port)

Convert TTL serial port to USB and connect the default pin of UART0:

RP2040 GPIO0 (UART0 TX) <----> USB_TTL_RX

RP2040 GPIO1 (UART0 RX) <----> USB_TTL_TX

When wiring using module connection RP2040

RP2040 GPIO16 <----> W5100S MISO

RP2040 GPIO17 <----> W5100S CS

RP2040 GPIO18 <----> W5100S SCK

RP2040 GPIO19 <----> W5100S MOSI

RP2040 GPIO20 <----> W5100S RST

Connect the PC and device to the router LAN port through network cables

4.4 Main code overview

We are using the official ioLibrary_Driver library of WIZnet. The library supports rich protocols and is easy to operate. The chip integrates the TCP/IP protocol stack on the hardware. The library also encapsulates the protocols above the TCP/IP layer. We only need to simply call the corresponding function to complete the application of the protocol. .

Step 1: Reference the corresponding library file in the smtp_client.c file.

Step 2: Macro define the socket numbers used by DHCP, DNS, and SMTP, the protocol interaction cache size, and the number of DHCP and DNS retries.

Step 3: Define three functions, including a 1-second timer callback function (used to handle DHCP and DNS timeout processing), a network address setting function, and a DNS resolution function (used to resolve the SMTP server address).

Step 4: Define global variables, including static IP address information, sending and receiving cache space during protocol interaction, and parameters required for SMTP.

Step 5: The main function first initializes the serial port and SPI and detects the link. Then set the network address of W5100S. First use DHCP to obtain it. If it fails, use the preset static IP address. Then initialize the SMTP message. This step is mainly to convert our recipient, sender and other information into SMTP messages. Then comes the DNS initialization operation. After completion, DNS resolution will be performed to resolve the domain name of the SMTP server into an IP address. Finally, connect to the SMTP server and send the email.

#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dhcp.h"   // Use dhcp
#include "socket.h" // Use socket
#include "dns.h"    //Use dns
#include "smtp.h"   //Use smtp

#define SOCK_DHCP 0 // DHCP SOCKET
#define SOCK_DNS 1  //DNS SOCKET
#define SOCK_SMTP 2                      // SMTP SOCKET
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
#define DHCP_RETRY_COUNT 5               // DHCP retry times
#define DNS_RET_FAIL 0
#define DNS_RET_SUCCESS 1
#define DNS_RETRY 5 /* 5 times */

bool repeating_timer_callback(struct repeating_timer *t);
void network_init(wiz_NetInfo *conf_info);
void do_dns(uint8_t *domain_name, uint8_t *remote_ip);

/* Network information to be configured. */
wiz_NetInfo net_info = {
  .mac = {0x00, 0x44, 0x55, 0x66, 0xed, 0x2e}, // Configured MAC address
  .ip = {192, 168, 1, 10},                     // Configured IP address
  .sn = {255, 255, 255, 0},                    // Configured subnet mask
  .gw = {192, 168, 1, 1},                      // Configured gateway
  .dns = {8, 8, 8, 8},                         // Configured domain address
  .dhcp = NETINFO_DHCP};                       // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
static uint8_t http_tx_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
static uint8_t http_rx_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
   0,
};
uint8_t Domain_ip[4] = {
   0,
};
uint8_t Domain_name[] = "smtp.126.com";
static uint8_t breakout_flag = 0; // Define the DHCP acquisition flag
uint8_t Mail_Send_OK = 0;

int main()
{
   int i;
   struct repeating_timer timer; // Define the timer structure
   wiz_NetInfo get_info;
   /* MCU init */
   stdio_init_all();     // Initialize the main control peripheral
   wizchip_initialize(); // Initialize the chip interface

   /*dhcp init*/
   DHCP_init(SOCK_DHCP, ethernet_buf);                                   // DHCP initialization
   add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler

   /* mail message init*/
   mailmessage();

   /*dns init*/
   DNS_init(SOCK_DNS, ethernet_buf);
   printf("wiznet chip smtp example.\r\n");
   network_init(&net_info);              // Configuring Network Information
   print_network_information(&get_info); // Read back the configuration information and print it
   do_dns(Domain_name,Domain_ip);
   while (true)
  {
       do_smtp(SOCK_SMTP);
       if (Mail_Send_OK)
      {
           while (true)
              ;
      }
  }
}

4.5 Results demonstration

5 Precautions

The mailbox must be in SMTP mode, otherwise it cannot be sent.

The server and port number must be consistent with the server of the mailbox used.

If we want to use WIZnet's W5500 to implement the example in this chapter, we only need to modify two places:

(1) Find the wizchip_conf.h header file under library/ioLibrary_Driver/Ethernet/ and modify the WIZCHIP macro definition to W5500.

(2) Find the CMakeLists.txt file under library and set COMPILE_SEL to ON. OFF is W5100S and ON is W5500.

Documents
  • Code for this article

  • WIZnet Official Website

  • WIZnet Official IO library

  • YouTube Demo

Comments Write