【ntp サーバ esp32】日本でのおすすめは?
esp32でntpの時刻を表示させてみました。
arduino IDEにあるスケッチ例をちょっと変えてそのまま使えます。
cayenneに送信した時刻をシリアルモニタで見れるようにした備忘録です。
ntp サーバとは?
NTPサーバとは、インターネットなどのTCP/IPネットワーク上で現在時刻の情報を配信するサーバで、 NTP(Network Time Protocol)でサーバ間で時刻を同期したり、クライアントに正確な現在時刻を知らせる…
NTP(Network Time Protocol)ですので世界共通規格、どの国のntpサーバを使ってもいいってことのようです。
ntpサーバ 日本でのおすすめ
日本時間を表示させたいのでntpサーバ 日本でのおすすめということで調べたのですがなんと!…
NTPプロトコル上では協定世界時 (UTC) を使って時刻を送受信する。
ということが判ったので、どこでもいいというか、NTPはstratumと呼ばれる階層構造を持ち、最上位のサーバが正確な時計から標準時を取得し、下位のホストはそれを参照する事で時刻を合わせるという仕組みになっているようです。
最上位のNTPサーバはstratum 1で最下位はstratum 16になっています。
GPSや標準電波、原子時計などの正確な時刻源 (stratum 0) に直結されたNTPサーバはstratum 1です。
ntpサーバは全て調べていませんがこんな感じでした。
提供機関 | サーバ名 | IPアドレス | Stratum |
---|---|---|---|
アメリカNIST 「米国標準技術局」 | time-nw.nist.gov | 131.107.1.10 | 1 |
アメリカNASA | ntp.nasa.gov | 198.123.30.132 | 1 |
独立行政法人 情報通信研究機構 | ntp.jst.mfeed.ad.jp | 210.173.160.27 | 2 |
独立行政法人 情報通信研究機構のサーバ、ntp.jst.mfeed.ad.jp を使って紹介している情報が氾濫していますがちゃんと調べてからにしましょう!
東京大学や広島大学、福岡大学などのntpサーバはStratum 1でした。 (^^;
ということで、カズのntpサーバ 日本でのおすすめはntp.nasa.govかな?
ただ、単にカッコイイ!という理由です…(^^;
しかし、セキュリティの問題でアクセス出来ないこともあるようです。 (^^;
ということで日本標準時と直結したNTPサーバー「ntp.nict.jp」がおすすめです。
これは、日本標準時 直結の「Stratum 1」サーバーですのでntpサーバの日本でのおすすめは ntp.nict.jp です。 (^^;
esp32 ntpサーバ スケッチ
arduino IDEのサンプルスケッチを少し変更しました。(ほとんどそのまま (^^;)
#include <WiFi.h> #include "time.h" const char* ssid = "*****"; //Wi-Fiアクセスポイント名 const char* password = "********"; //Wi-Fiパスワード const char* ntpServer = "ntp.nict.jp"; // 日本標準時直結 Stratum 1 const long gmtOffset_sec = 9*3600; //協定世界時 UTCより9時間の時差に対応 const int daylightOffset_sec = 0; //夏時間は日本では必要ありません。 void printLocalTime() { struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; } Serial.println(&timeinfo, "%Y/%m/%d %H:%M:%S %a "); // 表示を変更しています。 } void setup() { Serial.begin(115200); //connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" CONNECTED"); //init and get the time configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); printLocalTime(); //disconnect WiFi as it's no longer needed WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } void loop() { delay(1000); printLocalTime(); }
Cayenne スマホでiot
カイエンのスマホでiot にntpサーバを組んでみました。
これはカズの「スマホで遠隔水やり&温度管理」のスケッチです。
海外旅行先からビニールハウスや植木の水やりをスマホで出来るようにしたものですが、農業をされている方も使えます。
//WIFI情報 char ssid[] = " Wi-Fi アクセスポイント名"; char password[]="Wi-F- パスワード"; //カイエン ユーザネーム,MQTTパスワード,ID char username[] = "*****************"; char mqtt_password[] = "***************"; char cliend_id[] = "************************"; //=========================== Cayenne #include <CayenneMQTTESP32.h> #define CAYENNE_DEBUG #define CAYENNE_PRINT Serial; //=========================== NTP #include "time.h" const char* ntpServer = "ntp.nict.jp"; const long gmtOffset_sec = 9*3600; const int daylightOffset_sec = 0; //=========================== 温度 #include <OneWire.h> #include <DallasTemperature.h> OneWire GPIOPIN(32); // pin DallasTemperature sensors(&GPIOPIN); byte i; byte addr[10]; int cnt=0; int cnt2=0; float temp[11]; //========================== Set Up void setup() { Serial.begin(115200); Cayenne.begin(username, mqtt_password, cliend_id, ssid, password); pinMode(2, OUTPUT); digitalWrite(2, HIGH); //========================= Cayenne sensors.begin(); //========================= NTP configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); printLocalTime(); } void loop() { Cayenne.loop(); } CAYENNE_IN(1){ digitalWrite(2, getValue.asInt()); if(digitalRead(2)==HIGH){ Serial.println("HIGH"); }else{ Serial.println("LOW"); } } CAYENNE_OUT_DEFAULT(){ printLocalTime(); // 時刻表示 for( cnt2 = 0; cnt2 < 10; cnt2++) { if ( !GPIOPIN.search(addr)) { Serial.println("== END of address =="); GPIOPIN.reset_search(); cnt=0; return; } Serial.print("シリアルNo: "); for( i = 0; i < 8; i++) { Serial.print(addr[i], HEX); } cnt++; sensors.requestTemperatures(); temp[cnt]= sensors.getTempCByIndex(cnt-1); Serial.print(" 温度"); Serial.print(temp[cnt]); Serial.print("ºC "); Serial.print("温度センサー番号 : "); Serial.println(cnt); Cayenne.virtualWrite(cnt+1, temp[cnt], TYPE_TEMPERATURE, UNIT_CELSIUS); //摂氏 } } //========================== NTP GET void printLocalTime(){ struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; } Serial.println(&timeinfo, "%Y/%m/%d %H:%M:%S %a "); }