Arduino框架ESP32系列开发常用函数|API笔记

1.获取NTP时间

1.1 包含头文件

1
2
3
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

1.2 定义对象与选择时间服务器

1
2
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 8 * 3600,60000);

1.3 setup初始化

1
2
3
4
5
6
7
8
Serial.print("wifi connected");
WiFi.begin("***", "***"); // ssid , password
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
timeClient.begin();

1.4 获取时间

1
2
3
4
5
6
7
8
9
timeClient.update();
String time = timeClient.getFormattedTime(); //Hour:Minu:Sec格式字符串
unsigned long timestamp = timeClient.getEpochTime() - 8*3600; //UTC时间戳
struct tm *ptm = gmtime((time_t *)&timestamp);
String month,date,week_day,week[7]={"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
int Hour = timeClient.getHours();
month = String(ptm->tm_mon + 1); //月
date = (Hour < 8) ? String(ptm->tm_mday + 1) : String(ptm->tm_mday); //日
week_day = week[timeClient.getDay()]; //星期

1.5 完整实例

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
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 8 * 3600,60000);
const char *ssid = "***"; //WIFI名称
const char *password = "***"; //WIFI密码

void setup() {
Serial.begin(115200); //串口输出初始化
WiFi.begin(ssid, password); // 连接WIFI
Serial.print("wifi connected");
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
timeClient.begin();
}

void loop() {
timeClient.update();
String time = timeClient.getFormattedTime(); //Hour:Minu:Sec格式字符串
unsigned long timestamp = timeClient.getEpochTime() - 8*3600; //UTC时间戳
struct tm *ptm = gmtime((time_t *)&timestamp);
String month,date,week_day,week[7]={"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
int Hour = timeClient.getHours();
month = String(ptm->tm_mon + 1); //月
date = (Hour < 8) ? String(ptm->tm_mday + 1) : String(ptm->tm_mday); //日
week_day = week[timeClient.getDay()]; //星期

Serial.println("time:" + time);
Serial.println("timestamp:" + String(timestamp));
Serial.println("month:" + month);
Serial.println("date:" + date);
Serial.println("week:" + week_day);

delay(1000);
}

2.使用Ticker库进行多任务处理

2.1 作用

  • 操作硬件定时器从而实现多任务处理;当我们想要实现2个led灯每过1秒交替闪烁一次,往往使用delay函数延时1秒,过程中执行不了其他任务,而使用定时器即可解决这一问题

2.2 示意图

pSr9ubq.png

2.3 基本API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
seconds 秒数
callback 回调函数
arg 是回调函数的参数

/*单次执行*/
void once(float seconds, callback_function_t callback); //xx秒后只执行一次——不带参
void once(float seconds, callback_function_t callback,TArg arg); //xx秒后只执行一次——带参

void once_ms(float seconds, callback_function_t callback) //xx毫秒后只执行一次
void once_ms(float seconds, callback_function_t callback,TArg arg) //xx毫秒后只执行一次

/*周期执行*/
void attach(float seconds, callback_function_t callback); //每隔xx秒周期性执行
void attach(float seconds, callback_function_t callback,TArg arg); //每隔xx秒周期性执行

void attach_ms(float seconds, callback_function_t callback); //每隔xx毫秒周期性执行
void attach_ms(float seconds, callback_function_t callback,TArg arg); //每隔xx毫秒周期性执行

2.4 注意事项

  • Ticker定时调用的函数必须要“短小精悍”。在使用Ticker库时,定时调用函数必须要很快的执行完毕。否则会产生难以预料的问题。
  • 需要执行较大任务时,可定义一个全局计数变量在回调函数内自加或自减,当变量达到某个数值再执行该任务

2.5 实例:不带参周期执行

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
#include <Ticker.h>
#include <NTPClient.h>

/*定义任务对象*/
Ticker ticker1;
Ticker ticker2;

/*需要执行的任务*/
void function1()
{
Serial.println("function1已调用");
}

void function2()
{
Serial.println("function2已调用");
}

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

ticker1.attach(1,function1); //每1s执行一次function1
ticker2.attach_ms(200,function2); //每200ms执行一次function2
}

void loop() {

}

部分内容来源于网络