本文利用D1 mini,連接網路後,截取網路時間。可利用的網路有二,均以http GET取的JSON格式回應。
1. http://worldclockapi.com/api/json/utc/now
此網頁回應的是世界協調時間(UTC, Coordinated Universal Time),與台北的時區不同,需自行+8。該網頁回應的JSON碼如下:
- {
- "$id": "1",
- "currentDateTime": "2022-01-02T13:45Z",
- "utcOffset": "00:00:00",
- "isDayLightSavingsTime": false,
- "dayOfTheWeek": "Sunday",
- "timeZoneName": "UTC",
- "currentFileTime": 132856047568459540,
- "ordinalDate": "2022-2",
- "serviceResponse": null
- }
2. http://worldtimeapi.org/api/timezone/Asia/Taipei
此網頁回應的資訊較多,可取得Taipei時區目前的時間(datetime)以及UTC時間(utc_datetime),還可取得星期幾(day_of_week)、每年中第幾天(day_of_year)以及每年中第幾周(week_number)的資訊,但在跨年當周的周數計算可能會有出入。
- {
- "abbreviation": "CST",
- "client_ip": "112.78.66.92",
- "datetime": "2022-01-02T21:46:12.324416+08:00",
- "day_of_week": 0,
- "day_of_year": 2,
- "dst": false,
- "dst_from": null,
- "dst_offset": 0,
- "dst_until": null,
- "raw_offset": 28800,
- "timezone": "Asia/Taipei",
- "unixtime": 1641131172,
- "utc_datetime": "2022-01-02T13:46:12.324416+00:00",
- "utc_offset": "+08:00",
- "week_number": 52
- }
- // Flag's Block 產生的草稿碼
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include "data/webpages.h"
- #include <FlagHTTPClient.h>
- #include <ArduinoJson.h>
- #include <FlagJSON.h>
- String time_JSON_response;
- String datetime;
- String day_of_week;
- String date;
- String time_now;
- String week_day;
- ESP8266WebServer _esp8266WebServer(80);
- FlagHTTPClient _httpClient;
- JsonVariant _jsonRoot;
- DynamicJsonBuffer _jsonBuffer;
- void handleRoot() {
- #ifndef WEBPAGE_IN_PROGMEM
- _esp8266WebServer.send(200, "text/html", mainPage);
- #else
- _esp8266WebServer.send_P(200, PSTR("text/html"), mainPage);
- #endif
- }
- void handleNotFound() {
- #ifndef WEBPAGE_IN_PROGMEM
- _esp8266WebServer.send(404, "text/html", errorPage);
- #else
- _esp8266WebServer.send_P(404, PSTR("text/html"), errorPage);
- #endif
- }
- void handleSetting() {
- #ifndef WEBPAGE_IN_PROGMEM
- _esp8266WebServer.send(200, "text/html", settingPage);
- #else
- _esp8266WebServer.send_P(200, PSTR("text/html"), settingPage);
- #endif
- }
- int _httpGET(String url) {
- _httpClient.end();
- _httpClient.begin(url);
- _httpClient.setTimeout(30000);
- return _httpClient.GET();
- }
- void Get_Internet_Time() {
- time_JSON_response = _httpClient.getString();
- _jsonBuffer.clear();
- _jsonRoot = _jsonBuffer.parse(time_JSON_response);
- datetime = flagJSON.getJSONDataAsString(_jsonRoot, u8"datetime");
- day_of_week = flagJSON.getJSONDataAsString(_jsonRoot, u8"day_of_week");
- date = datetime.substring(0, 10);
- time_now = datetime.substring(11, 19);
- if (day_of_week == u8"0") {
- week_day = u8"Sunday";
- } else if (day_of_week == u8"1") {
- week_day = u8"Monday";
- } else if (day_of_week == u8"2") {
- week_day = u8"Tuesday";
- } else if (day_of_week == u8"3") {
- week_day = u8"Wednesday";
- } else if (day_of_week == u8"4") {
- week_day = u8"Thursday";
- } else if (day_of_week == u8"5") {
- week_day = u8"Friday";
- } else if (day_of_week == u8"6") {
- week_day = u8"Saturday";
- }
- Serial.println((String(u8"date: ") + String(date)));
- Serial.println((String(u8"day: ") + String(week_day)));
- Serial.println((String(u8"time: ") + String(time_now)));
- Serial.println(u8"");
- }
- // setup() 會先被執行且只會執行一次
- void setup() {
- Serial.begin(9600);
- WiFi.begin(u8"YUPIN", u8"0987654321");
- while (!((WiFi.status() == WL_CONNECTED))) {
- delay(1000);
- Serial.println(u8"connecting ...");
- }
- Serial.println((WiFi.localIP().toString()));
- _esp8266WebServer.on("/", handleRoot);
- _esp8266WebServer.onNotFound(handleNotFound);
- _esp8266WebServer.on("/setting", handleSetting);
- _esp8266WebServer.begin();
- time_JSON_response = u8"";
- datetime = u8"";
- date = u8"";
- time_now = u8"";
- day_of_week = u8"";
- week_day = u8"";
- }
- // loop() 裡面的程式會不斷重複執行
- void loop() {
- if (_httpGET(u8"http://worldtimeapi.org/api/timezone/Asia/Taipei") > 0) {
- Get_Internet_Time();
- }
- delay(500);
- }
沒有留言:
張貼留言