功能

LVGL++

  1. ​ MQTT

  2. ​ OTA

  3. ​ SMTP

实现

MQTT

巴法开放平台 (bemfa.com)

MQTT基础功能实现非常简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "head.h"
#include <PubSubClient.h>
const char *ssid = "";
const char *password = "";

const char *MQTT_SERVER = "bemfa.com";
const int MQTT_PORT = 9501;
const char *MQTT_ID = "";
const char *TOPIC = "";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50]; // 消息

int value = 0;

void reconnect()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
if (client.connect(MQTT_ID))
{
Serial.println("connected");
// 连接成功时订阅主题
client.subscribe(TOPIC);
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic); // 打印主题信息
Serial.print("] ");
String message;
for (int i = 0; i < length; i++)
{
message += (char)payload[i]; // 将每个字节转换为字符并添加到字符串中
}

Serial.println(message); // 打印接收到的字符串
Serial.println("CALL BACK");
}

void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
}

void setup()
{
Serial.begin(115200);
delay(10);

Serial.print("Connecting to ");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

client.setServer(MQTT_SERVER, MQTT_PORT); // 设定MQTT服务器与使用的端口,1883是默认的MQTT端口
client.setCallback(callback); // 设定回调方式,当ESP8266收到订阅消息时会调用此方法
}

在LVGL裸机中实现:在 lvgl 定时器中循环调用 client.loop() 函数即可

后续上 FreeRTOS 操作系统在task中实现应该更加简单

1
mqtt_timer = lv_timer_create(mqtt_timer_cb, 500, NULL);

image-20230630011438060

SMPT发邮件

找半天 BUG 发现是 Outlook 不接收我 qq 邮箱发的邮件,反过来发送就没问题了。

1
ESP_Mail_Client 550 The mail may contain inappropriate words or content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173


#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#else

// Other Client defined here
// To use custom Client, define ENABLE_CUSTOM_CLIENT in src/ESP_Mail_FS.h.
// See the example Custom_Client.ino for how to use.

#endif

#include <ESP_Mail_Client.h>

#define WIFI_SSID "vivoS12"
#define WIFI_PASSWORD "7bsgbsg7"

/** The smtp host name e.g. smtp.gmail.com for GMail or smtp.office365.com for Outlook or smtp.mail.yahoo.com */
#define SMTP_HOST "smtp.office365.com"

#define SMTP_PORT esp_mail_smtp_port_587 // port 465 is not available for Outlook.com

/* The log in credentials */
// #define AUTHOR_EMAIL "2028163967@qq.com"
// #define AUTHOR_PASSWORD "tvzbsiofyiwtbecg"
#define AUTHOR_EMAIL "Bsgbsg7@Outlook.com"
#define AUTHOR_PASSWORD "suyqnddqnzoxtlql"
/* Recipient email address */
#define RECIPIENT_EMAIL "2028163967@qq.com"

/* Declare the global used SMTPSession object for SMTP transport */
SMTPSession smtp;

void smtpCallback(SMTP_Status status);

void setup()
{
Serial.begin(115200);

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();

/* Set the network reconnection option */
MailClient.networkReconnect(true);
smtp.debug(1);
smtp.callback(smtpCallback);
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.login.user_domain = F("mydomain.net");
config.time.ntp_server = F("pool.ntp.org,time.nist.gov");
config.time.gmt_offset = 8;
config.time.day_light_offset = 0;


/* Declare the message class */
SMTP_Message message;

/* Set the message headers */
message.sender.name = F("Bsgbsg7's ESP Box");
message.sender.email = AUTHOR_EMAIL;
message.subject = F("FIRE!");
message.addRecipient(F("Master"), RECIPIENT_EMAIL);

String textMsg = "Hope you have a good day";
message.text.content = textMsg;
/** The Plain text message character set e.g.
* us-ascii
* utf-8
* utf-7
* The default value is utf-8
*/
message.text.charSet = F("us-ascii");

/** The content transfer encoding e.g.
* enc_7bit or "7bit" (not encoded)
* enc_qp or "quoted-printable" (encoded)
* enc_base64 or "base64" (encoded)
* enc_binary or "binary" (not encoded)
* enc_8bit or "8bit" (not encoded)
* The default value is "7bit"
*/
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;

message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal;

message.addHeader(F("Message-ID: <Bsgbsg7@Outlook.com>"));

/* Connect to the server */
if (!smtp.connect(&config))
{
ESP_MAIL_PRINTF("Connection error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());
return;
}

if (!smtp.isLoggedIn())
{
Serial.println("\nNot yet logged in.");
}
else
{
if (smtp.isAuthenticated())
Serial.println("\nSuccessfully logged in.");
else
Serial.println("\nConnected with no Auth.");
}

/* Start sending Email and close the session */
if (!MailClient.sendMail(&smtp, &message))
ESP_MAIL_PRINTF("Error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());

// to clear sending result log
// smtp.sendingResult.clear();
}

void loop()
{
}

/* 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())
{
// ESP_MAIL_PRINTF used in the examples is for format printing via debug Serial port
// that works for all supported Arduino platform SDKs e.g. AVR, SAMD, ESP32 and ESP8266.
// In ESP8266 and ESP32, you can use Serial.printf directly.

Serial.println("----------------");
ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
ESP_MAIL_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);

// In case, ESP32, ESP8266 and SAMD device, the timestamp get from result.timestamp should be valid if
// your device time was synched with NTP server.
// Other devices may show invalid timestamp as the device time was not set i.e. it will show Jan 1, 1970.
// You can call smtp.setSystemTime(xxx) to set device time manually. Where xxx is timestamp (seconds since Jan 1, 1970)

ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
ESP_MAIL_PRINTF("Date/Time: %s\n", MailClient.Time.getDateTimeString(result.timestamp, "%B %d, %Y %H:%M:%S").c_str());
ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str());
ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str());
}
Serial.println("----------------\n");

// You need to clear sending result as the memory usage will grow up.
smtp.sendingResult.clear();
}
}

image-20230630161623656

image-20230630173821049

image-20230630173955474

BUG