最新消息:

如何使用arduino发送邮件

Arduino 少儿编程 2565浏览 0评论

如何使用arduino发送邮件

如何使用arduino发送邮件

使用的硬件:

  1. wemos D1开发板(基于ESP8266)

使用的软件:

  1. arduino IDE

实现的效果:

  1. 让wemos D1开发板向指定邮箱发送电子邮件

制作步骤:

一、安装库

  1. 到GitHub网站下载库的zip文件,网址:https://github.com/tackelua/ESP8266GmailSender

如何使用arduino发送邮件

下载的zip文件

2.打开arduino IDE安装这个zip库:

如何使用arduino发送邮件

如何使用arduino发送邮件

这里面的路径是和arduino首选项中设置的是一样的,如下图所示:

如何使用arduino发送邮件

二、修改库文件

修改库文件夹中的Gsender.h文件,如下图所示:

如何使用arduino发送邮件

我这里修改成了网易163邮箱,SMTP端口是465,SMTP服务器是smtp.163.com。其中账号和密码要使用BASE64编码,编码网址为:

http://www.base64encode.org/

如何使用arduino发送邮件

三、示例代码

#include <ESP8266WiFi.h>

#include <Gsender.h>

 

#pragma region Globals

const char* ssid = “”;                           // WIFI network name

const char* password = “”;                       // WIFI network password

uint8_t connection_state = 0;                    // Connected to WIFI or not

uint16_t reconnect_interval = 10000;

#pragma endregion Globals

 

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)

{

static uint16_t attempt = 0;

Serial.print(“Connecting to “);

if(nSSID) {

WiFi.begin(nSSID, nPassword);

Serial.println(nSSID);

} else {

WiFi.begin(ssid, password);

Serial.println(ssid);

}

 

uint8_t i = 0;

while(WiFi.status()!= WL_CONNECTED && i++ < 50)

{

delay(200);

Serial.print(“.”);

}

++attempt;

Serial.println(“”);

if(i == 51) {

Serial.print(“Connection: TIMEOUT on attempt: “);

Serial.println(attempt);

if(attempt % 2 == 0)

Serial.println(“Check if access point available or SSID and Passwordrn”);

return false;

}

Serial.println(“Connection: ESTABLISHED”);

Serial.print(“Got IP address: “);

Serial.println(WiFi.localIP());

return true;

}

 

void Awaits()

{

uint32_t ts = millis();

while(!connection_state)

{

delay(50);

if(millis() > (ts + reconnect_interval) && !connection_state){

connection_state = WiFiConnect();

ts = millis();

}

}

}

 

void setup()

{

Serial.begin(115200);

connection_state = WiFiConnect();

if(!connection_state)  // if not connected to WIFI

Awaits();          // constantly trying to connect

 

Gsender *gsender = Gsender::Instance();    // Getting pointer to class instance

    String subject = “Subject is optional!”;

    if(gsender->Subject(subject)->Send(“xxxxx@xx.com”, “Setup test”)) {

        Serial.println(“Message send.”);

} else {

Serial.print(“Error sending message: “);

Serial.println(gsender->getError());

}

}

 

void loop(){}

四、发送之后的效果

如何使用arduino发送邮件

如何使用arduino发送邮件

邮件发送成功了!

您必须 登录 才能发表评论!