4.6.1 Execution
main.c - Initialize the chip and send an email.
- Code summary.
- Configure the network
parameters in main.h.
/** Wi-Fi Settings */ #define MAIN_WLAN_SSID "DEMO_AP" /**< Destination SSID */ #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WPA_PSK /**< Security manner */ #define MAIN_WLAN_PSK "12345678" /**< Password for Destination SSID */ #define MAIN_SENDER_RFC "<sender@gmail.com>" /* Set Sender Email Address */ #define MAIN_RECIPIENT_RFC "<recipient@gmail.com>" /* Set Recipient Email Address */ #define MAIN_EMAIL_SUBJECT "Hello from WINC1500!" #define MAIN_TO_ADDRESS "recipient@gmail.com" /* Set To Email Address */ #define MAIN_FROM_ADDRESS "sender@gmail.com" /* Set From Email Address */
- Initialize the socket
module and register the socket callback function.
/* Initialize socket module */ socketInit(); registerSocketCallback(socket_cb, resolve_cb);
- Connect to the AP.
/* Connect to router. */ m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
- After the device is
connected to the AP, try to connect to the SMTP server. Once connected,
the smtpStatehandler will be executed sequentially until the socket
status becomes
SocketComplete.
if (gu8SocketStatus == SocketInit) { if (tcp_client_socket < 0) { gu8SocketStatus = SocketWaiting; if (smtpConnect() != SOCK_ERR_NO_ERROR) { gu8SocketStatus = SocketInit; } } } else if (gu8SocketStatus == SocketConnect) { gu8SocketStatus = SocketWaiting; if (smtpStateHandler() != MAIN_EMAIL_ERROR_NONE) { ... } } else if (gu8SocketStatus == SocketComplete) { printf("main: Email was successfully sent.\r\n"); close_socket(); }
- Connect to the socket and
receive data following the SMTP status.
static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg) { ... case SOCKET_MSG_CONNECT: { if (pstrConnect && pstrConnect->s8Error >= SOCK_ERR_NO_ERROR) recv(tcp_client_socket, gcHandlerBuffer, ..., 0); } ... case SOCKET_MSG_RECV: { switch (gu8SmtpStatus) { case SMTP_INIT: ... case SMTP_HELO: ... case SMTP_AUTH: ... case SMTP_AUTH_USERNAME: ... case SMTP_AUTH_PASSWORD: ... case SMTP_FROM: ... case SMTP_RCPT: ... case SMTP_DATA: ... case SMTP_MESSAGE_DATAEND: ... } }
- Configure the network
parameters in main.h.
- Build the program and download it into the board.
- Start the application.
Note:
- To use Gmail, the root certificate must be installed. For more details about downloading the root certificate, refer to the “Atmel-42417-SAMD21-ATWINC1500-Platform_Getting_Started_Guide” document.
- If the server connection is
unstable, this example may not operate normally.
Limitations:
1. Email is sent to only one recipient.
2. Only plain text email is supported.