ESP8266 Gmail

Hướng dẫn này sẽ cho bạn biết cách thiết lập ESP8266 để gửi email thông qua tài khoản Gmail của bạn. Email sẽ được gửi từ một tài khoản Gmail và có thể được nhận bởi bất kỳ tài khoản email nào.

ESP8266 NodeMCU gmail

Phần cứng cần chuẩn bị

1×ESP8266 NodeMCU ESP-12E
1×Recommended: ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP8266
1×(Khuyến nghị) Power Splitter for ESP8266 Type-C

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Chuẩn bị trước

Để sử dụng mã, bạn cần một tài khoản Gmail và một mật khẩu ứng dụng đặc biệt. Dưới đây là những chi tiết quan trọng bạn cần nhớ:

  1. Tạo một tài khoản Gmail mới dành riêng cho việc thử nghiệm thay vì dùng tài khoản thông thường của bạn, để tránh bất kỳ sự cố nào.
  2. Mật khẩu được sử dụng trong mã ESP8266 không giống với mật khẩu tài khoản Gmail của bạn. Bạn cần lấy một 'mật khẩu ứng dụng' từ tài khoản Google bằng cách làm theo một số hướng dẫn.

Dưới đây là các bước, lần lượt từng bước một:

  • Tạo một tài khoản Gmail mới (https://accounts.google.com/).
  • Đăng nhập vào tài khoản bạn đã tạo.
  • Đi tới Tài khoản Google của bạn (https://myaccount.google.com/).
  • Nhấp vào mục "Bảo mật".
bảo mật gmail
  • Kích hoạt Xác thực hai bước (Điều này là cần thiết trước khi bạn có thể sử dụng mật khẩu ứng dụng).
  • Truy cập trang Google App Passwords và tạo mật khẩu cho ứng dụng. Bạn có thể đặt tên cho nó theo ý thích.
mật khẩu ứng dụng google
  • Nhấp vào nút Tạo và mật khẩu gồm 16 chữ số sẽ được hiển thị cho bạn.
mật khẩu ứng dụng gmail
  • Lưu số 16 chữ số này. Bạn sẽ cần nó cho mã ESP8266 ở bước tiếp theo.

※ Lưu ý:

Giao diện người dùng của Google có thể thay đổi. Nếu bạn không thấy 'Mật khẩu ứng dụng' sau khi làm theo các bước ở trên, hãy tìm kiếm 'Cách lấy Mật khẩu ứng dụng Google' để tìm hướng dẫn cập nhật.

Mã ESP8266

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-gmail */ #include <ESP8266WiFi.h> #include <ESP_Mail_Client.h> #define WIFI_SSID "YOUR_WIFI_SSID" // CHANGE IT #define WIFI_PASSWORD "YOUR_WIFI_PASSWORD" // CHANGE IT // the sender email credentials #define SENDER_EMAIL "xxxxxx@gmail.com" // CHANGE IT #define SENDER_PASSWORD "xxxx xxxx xxxx xxxx" // CHANGE IT to your Google App password #define RECIPIENT_EMAIL "xxxxxx@gmail.com" // CHANGE IT #define SMTP_HOST "smtp.gmail.com" #define SMTP_PORT 587 SMTPSession smtp; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(300); } Serial.println(); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println(); String subject = "Email Notification from ESP8266"; String textMsg = "This is an email sent from ESP8266.\n"; textMsg += "Sensor value: "; textMsg += "15"; // OR replace this value read from a sensor gmail_send(subject, textMsg); } void loop() { // YOUR OTHER CODE HERE } void gmail_send(String subject, String textMsg) { // set the network reconnection option MailClient.networkReconnect(true); smtp.debug(1); smtp.callback(smtpCallback); Session_Config config; // set the session config config.server.host_name = SMTP_HOST; config.server.port = SMTP_PORT; config.login.email = SENDER_EMAIL; config.login.password = SENDER_PASSWORD; config.login.user_domain = F("127.0.0.1"); config.time.ntp_server = F("pool.ntp.org,time.nist.gov"); config.time.gmt_offset = 3; config.time.day_light_offset = 0; // declare the message class SMTP_Message message; // set the message headers message.sender.name = F("ESP8266"); message.sender.email = SENDER_EMAIL; message.subject = subject; message.addRecipient(F("To Whom It May Concern"), RECIPIENT_EMAIL); message.text.content = textMsg; message.text.transfer_encoding = "base64"; message.text.charSet = F("utf-8"); message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low; // set the custom message header message.addHeader(F("Message-ID: <abcde.fghij@gmail.com>")); // connect to the server if (!smtp.connect(&config)) { Serial.printf("Connection error, Status Code: %d, Error Code: %d, Reason: %s\n", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); return; } if (!smtp.isLoggedIn()) { Serial.println("Not yet logged in."); } else { if (smtp.isAuthenticated()) Serial.println("Successfully logged in."); else Serial.println("Connected with no Auth."); } // start sending Email and close the session if (!MailClient.sendMail(&smtp, &message)) Serial.printf("Error, Status Code: %d, Error Code: %d, Reason: %s\n", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); } // callback function to get the Email sending status void smtpCallback(SMTP_Status status) { // print the current status Serial.println(status.info()); // print the sending result if (status.success()) { Serial.println("----------------"); Serial.printf("Message sent success: %d\n", status.completedCount()); Serial.printf("Message sent failed: %d\n", status.failedCount()); Serial.println("----------------\n"); for (size_t i = 0; i < smtp.sendingResult.size(); i++) { // get the result item SMTP_Result result = smtp.sendingResult.getItem(i); Serial.printf("Message No: %d\n", i + 1); Serial.printf("Status: %s\n", result.completed ? "success" : "failed"); Serial.printf("Date/Time: %s\n", MailClient.Time.getDateTimeString(result.timestamp, "%B %d, %Y %H:%M:%S").c_str()); Serial.printf("Recipient: %s\n", result.recipients.c_str()); Serial.printf("Subject: %s\n", result.subject.c_str()); } Serial.println("----------------\n"); // free the memory smtp.sendingResult.clear(); } }

Hướng dẫn từng bước

Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:

  • Hãy tham khảo hướng dẫn ESP8266 - Cài đặt phần mềm nếu đây là lần đầu bạn sử dụng ESP8266.
  • Kết nối các thành phần như đã hiển thị trong sơ đồ.
  • Kết nối bo mạch ESP8266 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 phù hợp, chẳng hạn như NodeMCU 1.0 (ESP-12E Module), và cổng COM tương ứng của nó.
  • Nhấp vào biểu tượng Library Manager ở bên trái trong Arduino IDE.
  • Tìm kiếm ESP Mail Client và chọn bản do Mobizt phát hành.
  • Nhấn nút Install để thêm thư viện ESP Mail Client.
thư viện esp mail client cho ESP8266 NodeMCU
  • Sao chép mã ở trên và mở nó bằng Arduino IDE.
  • Cập nhật thông tin WiFi bằng cách thay đổi WIFI_SSIDWIFI_PASSWORD thành SSID và mật khẩu của mạng WiFi của bạn.
  • Nhập email và mật khẩu của bạn vào mã ở phần dưới SENDER_EMAILSENDER_PASSWORD.
  • Cập nhật địa chỉ email người nhận trong RECIPIENT_EMAIL thành email của bạn, nếu cần. Nó có thể giống với email của người gửi.

※ Lưu ý:

  • Địa chỉ email của người gửi nên là một tài khoản Gmail.
  • Mật khẩu của người gửi là mật khẩu ứng dụng được cấp ở bước trước.
  • Địa chỉ email của người nhận có thể thuộc bất kỳ loại nào.
  • Nhấp vào nút Tải lên trong Arduino IDE để gửi mã đến ESP8266.
  • Mở Trình theo dõi nối tiếp.
  • Kiểm tra kết quả trên Trình theo dõi nối tiếp.
COM6
Send
#### Message sent successfully > C: message sent successfully ---------------- Message sent success: 1 Message sent failed: 0 ---------------- Message No: 1 Status: success Date/Time: May 27, 2024 04:42:50 Recipient: xxxxxx@gmail.com Subject: Email Notification from ESP8266 ----------------
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Hãy kiểm tra hộp thư email của người mà bạn đã gửi email đến. Bạn sẽ tìm thấy một email tương tự như thế này:
ESP8266 NodeMCU gửi email

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.