Arduino UNO R4 Email

Hướng dẫn này sẽ chỉ bạn cách lập trình Arduino UNO R4 để gửi email từ tài khoản Gmail của bạn.

Arduino UNO R4 email

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi
1×Alternatively, DIYables STEM V4 IoT
1×(Tùy chọn) DIYables STEM V4 IoT
1×Cáp USB Type-C
1×(Tùy chọn) Breadboard
1×(Tùy chọn) Dây Jumper
1×(Tùy chọn) DC Power Jack
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Chuẩn Bị Trước

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

  • Tạo một tài khoản Gmail mới dành riêng cho mục đích thử nghiệm. Đừng sử dụng tài khoản thông thường của bạn để bảo vệ tài khoản khỏi các vấn đề bảo mật.
  • Mật khẩu trong code Arduino UNO R4 không giống với mật khẩu đăng nhập Gmail của bạn. Bạn cần tạo một "App Password" từ tài khoản Google của mình.

Bạn có thể tạo "App Password" từ tài khoản Google của mình bằng cách làm theo các bước sau:

gmail security
  • Bật tính năng 2-Step Verification (Cần bật 2-Step Verification để tạo app passwords).
  • Truy cập trang web Google App Passwords và tạo một app password. Bạn có thể đặt tên bất kỳ.
google app passwords
  • Nhấp vào nút Create để nhận mật khẩu 16 chữ số hiển thị như sau:
gmail app passwords
  • Vui lòng ghi nhớ và lưu lại chuỗi 16 chữ số này. Bạn sẽ cần nó cho code Arduino UNO R4 ở bước tiếp theo.

※ Lưu ý:

Giao diện của Google có thể được cập nhật. Nếu bạn không thể tìm thấy "App Passwords" như mô tả, hãy tìm kiếm trực tuyến "How to get Google App Passwords" để tìm hướng dẫn mới nhất.

Code Arduino UNO R4

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-email */ #include <WiFiS3.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 Arduino"; String textMsg = "This is an email sent from Arduino.\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("Arduino"); 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.println("Connection error: "); Serial.print("- Status Code: "); Serial.println(smtp.statusCode()); Serial.print("- Error Code: "); Serial.println(smtp.errorCode()); Serial.print("- Reason: "); Serial.println(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.println("Connection error: "); Serial.print("- Status Code: "); Serial.println(smtp.statusCode()); Serial.print("- Error Code: "); Serial.println(smtp.errorCode()); Serial.print("- Reason: "); Serial.println(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()) { for (size_t i = 0; i < smtp.sendingResult.size(); i++) { // get the result item SMTP_Result result = smtp.sendingResult.getItem(i); Serial.print("Status: "); if (result.completed) Serial.println("success"); else Serial.println("failed"); Serial.print("Recipient: "); Serial.println(result.recipients.c_str()); Serial.print("Subject: "); Serial.println(result.subject.c_str()); } Serial.println("----------------\n"); // free the memory smtp.sendingResult.clear(); } }

Các Bước Thực Hiện

Làm theo hướng dẫn từng bước sau:

  • Nếu đây là lần đầu bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối bo mạch Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi chạy Arduino IDE trên máy tính của bạn.
  • Chọn đúng bo mạch Arduino UNO R4 (Arduino UNO R4 R4 WiFi) và cổng COM.
  • Mở Library Manager bằng cách nhấp vào biểu tượng Library Manager ở phía bên trái màn hình Arduino IDE.
  • Tìm kiếm ESP Mail Client để tìm thư viện ESP Mail Client của Mobizt.
  • Nhấp vào nút Install để thêm thư viện ESP Mail Client.
Arduino UNO R4 esp mail client thư viện
  • Sao chép code ở trên và mở nó trong Arduino IDE.
  • Cập nhật tên mạng WiFi và mật khẩu của bạn tại các vị trí WIFI_SSIDWIFI_PASSWORD.
  • Cập nhật email và mật khẩu của bạn trong các hằng số: SENDER_EMAILSENDER_PASSWORD.
  • Cập nhật RECIPIENT_EMAIL với địa chỉ email mà bạn muốn nhận tin nhắn. Có thể giống với email người gửi.

※ Lưu ý:

  • Email người gửi nên sử dụng Gmail.
  • Sử dụng App password đã tạo trước đó làm mật khẩu người gửi.
  • Email người nhận có thể là bất kỳ loại email nào.
  • Nhấn nút Upload trong Arduino IDE để tải code lên Arduino UNO R4.
  • Mở Serial Monitor.
  • Xem kết quả trên Serial Monitor.
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 Arduino ----------------
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Kiểm tra hộp thư đến của người nhận. Bạn sẽ tìm thấy một email tương tự như này:
Arduino UNO R4 sends 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.

Bài hướng dẫn liên quan