ここを参考に。
#include <WiFi.h> #include "time.h" const char* ssid = "ssid"; const char* password = "password"; const char* ntpServer = "ntp.nict.jp"; void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } void setup() { Serial.begin(115200); initWiFi(); configTime(0, 0, ntpServer); } void loop() { Serial.println(xx_time_get_time()); } int64_t xx_time_get_time() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL)); }
これをArduinode実行すると以下のような結果を得る。
1628596850612 1628596850613
この最小単位はミリ秒である。Pythonを使って確認する。
$ python
>>> from datetime import datetime
>>> print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
>>> a=1628596850612/1000
>>> b=1628596850613/1000
>>> print(datetime.fromtimestamp(a).strftime('%Y-%m-%d %H:%M:%S.%f'))
2021-08-10 15:00:50.612000
>>> print(datetime.fromtimestamp(b).strftime('%Y-%m-%d %H:%M:%S.%f'))
2021-08-10 15:00:50.61300050秒612,、50秒613、となり、ミリ秒単位で計測できていることがわかる。