Skip to content

Commit f2d5da7

Browse files
committed
串口日志输出添加时间戳
1 parent adaa7b4 commit f2d5da7

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/SmallDesktopDisplay.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void esp_reset(Button2 &btn);
9393
void scrollBanner();
9494
void weaterData(String *cityDZ, String *dataSK, String *dataFC); //天气信息写到屏幕上
9595
void refresh_AnimatedImage(); //更新右下角
96+
void log(String str); // 打印日志到串口
9697

9798
//创建时间更新函数线程
9899
Thread reflash_time = Thread();
@@ -186,6 +187,28 @@ String monthDay()
186187
return s;
187188
}
188189

190+
String intToString(int num)
191+
{
192+
String str = String(num);
193+
if (num < 10)
194+
{
195+
str = "0" + str;
196+
}
197+
return str;
198+
}
199+
200+
// 年-月-日 时:分:秒
201+
String dateTime()
202+
{
203+
return String(year()) + "-" + intToString(month()) + "-" + intToString(day()) + " " + intToString(hour()) + ":" + intToString(minute()) + ":" + intToString(second());
204+
}
205+
206+
// 打印日志到串口
207+
void log(String str)
208+
{
209+
Serial.println(dateTime() + " " + str);
210+
}
211+
189212
/* *****************************************************************
190213
* 函数
191214
* *****************************************************************/
@@ -788,7 +811,7 @@ void getCityWeater()
788811

789812
//启动连接并发送HTTP请求
790813
int httpCode = httpClient.GET();
791-
Serial.println("正在获取天气数据");
814+
log("正在获取天气数据");
792815
// Serial.println(URL);
793816

794817
//如果服务器响应OK则从服务器获取响应体信息并通过串口输出
@@ -813,11 +836,11 @@ void getCityWeater()
813836
// Serial.println(jsonFC);
814837

815838
weaterData(&jsonCityDZ, &jsonDataSK, &jsonFC);
816-
Serial.println("获取成功");
839+
log("天气数据获取成功");
817840
}
818841
else
819842
{
820-
Serial.println("请求城市天气错误:");
843+
log("请求城市天气错误:");
821844
Serial.print(httpCode);
822845
}
823846

@@ -1120,7 +1143,7 @@ time_t getNtpTime()
11201143
int size = Udp.parsePacket();
11211144
if (size >= NTP_PACKET_SIZE)
11221145
{
1123-
Serial.println("Receive NTP Response");
1146+
log("Receive NTP Response");
11241147
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
11251148
unsigned long secsSince1900;
11261149
// convert four bytes starting at location 40 to a long integer
@@ -1132,7 +1155,7 @@ time_t getNtpTime()
11321155
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
11331156
}
11341157
}
1135-
Serial.println("No NTP Response :-(");
1158+
log("No NTP Response :-(");
11361159
return 0; // 无法获取时间时返回0
11371160
}
11381161

@@ -1199,7 +1222,7 @@ void WIFI_reflash_All()
11991222
{
12001223
if (WiFi.status() == WL_CONNECTED)
12011224
{
1202-
Serial.println("WIFI connected");
1225+
log("WIFI connected");
12031226

12041227
// Serial.println("getCityWeater start");
12051228
getCityWeater();
@@ -1209,7 +1232,7 @@ void WIFI_reflash_All()
12091232
//其他需要联网的方法写在后面
12101233

12111234
WiFi.forceSleepBegin(); // Wifi Off
1212-
Serial.println("WIFI sleep......");
1235+
log("WIFI sleep......");
12131236
Wifi_en = 0;
12141237
}
12151238
else
@@ -1222,7 +1245,7 @@ void WIFI_reflash_All()
12221245
// 打开WIFI
12231246
void openWifi()
12241247
{
1225-
Serial.println("WIFI reset......");
1248+
log("WIFI reset......");
12261249
WiFi.forceSleepWake(); // wifi on
12271250
Wifi_en = 1;
12281251
}
@@ -1356,7 +1379,7 @@ void setup()
13561379
#endif
13571380

13581381
WiFi.forceSleepBegin(); // wifi off
1359-
Serial.println("WIFI休眠......");
1382+
log("WIFI sleep......");
13601383
Wifi_en = 0;
13611384

13621385
reflash_time.setInterval(300); //设置所需间隔 100毫秒

0 commit comments

Comments
 (0)