备忘.png
#include <WiFi.h>
#include <Gsender.h>
#pragma region Globals
const char* ssid = "FAST_C9E0"; // WIFI network name
const char* password = "ab13417665005ab"; // WIFI network password
uint8_t connection_state = 0; // 是否连接到WIFI
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 Password\r\n");
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!主题";
//gsender->Subject(邮件主题)->Send(smtp服务器端口,smtp服务器,发件人BASE64_邮件账号,发件人BASE64_邮件密码,发件人邮箱,收件人邮箱, 邮件内容)
if(gsender->Subject(subject)->Send(465,"smtp.qq.com","NTQ3MTc2MDUyQHFxLmNvbQ==","发件人BASE64_邮件密码","547176052@qq.com","547176052@qq.com", "啊Setup test内容555")) {
Serial.println("邮件发送成功");
} else {
Serial.print("邮件发送失败:");
Serial.println(gsender->getError());//打印错误消息
}
}
void loop(){}
网友评论