M5Paper: NTP の使い方

こちらを参考にしました。
[[M5Stack] M5Stackで時刻情報を扱う方法～NTPサーバーに接続して、正しい時刻を表示させる～](https://knt60345blog.com/m5stack-ntp/)

![IMG_20220623_125421.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/bd1be40f-858f-b907-bd25-2f7d40be1e2f.jpeg)

## プログラム ##

```text:ntp.ino
// ---------------------------------------------------------------
//	ntp.ino
//
//					Jun/23/2022
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <WiFi.h>
#include "time.h"

// ---------------------------------------------------------------
char ssid[] = "******";
char password[] = "******";

M5EPD_Canvas canvas(&M5.EPD);

#define JST 3600* 9

int ncount = 0;
// ---------------------------------------------------------------
void setup_wifi(){
	
	Serial.println("Connecting to ");
	Serial.println(ssid);

	// WiFi接続性改善のため、いったん切断
	WiFi.disconnect( true, true ); //WiFi OFF, eraseAP=true
	delay(500);
	
	// WiFi開始
	WiFi.begin(ssid, password);
 
	// Wi-Fi接続待ち
	while (WiFi.status() != WL_CONNECTED){
	delay(500);
	Serial.print(".");
//	M5.Lcd.print(".");
	}

	// WiFi接続成功メッセージの表示
	Serial.println("WiFi Connected.");

	canvas.drawString("WiFi Connected.", 250, 740);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

	// M5StackのIPアドレスを表示
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());

	const int width = 500;
	const int height = 300;
	canvas.setTextArea(50, 100, 500, height);
	canvas.println("IP address: ");
	canvas.println(WiFi.localIP());
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
}

// ---------------------------------------------------------------
void printLocalTime()
{
	struct tm timeinfo;

	if(!getLocalTime(&timeinfo)){
	Serial.println("Failed to obtain time");
//	M5.Lcd.println("Failed to obtain time");
	}
else
	{
	const int year = timeinfo.tm_year+1900;
	const short month = timeinfo.tm_mon + 1;
	const short day = timeinfo.tm_mday;
	const short hour = timeinfo.tm_hour;
	const short min = timeinfo.tm_min;
	const short sec = timeinfo.tm_sec;

	Serial.printf("%04d-%02d-%02d ", year, month, day);
	Serial.printf("%02d:%02d:%02d",hour,min,sec);

	const int width = 500;
	const int height = 300;
	canvas.setTextArea(50, 400, 500, height);
	canvas.printf("%04d-%02d-%02d ",year, month, day);
	canvas.printf("%02d:%02d:%02d",hour,min,sec);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
	}
}

// ---------------------------------------------------------------
void setup()
{
	Serial.print("*** setup *** start ***\r\n");

	// M5Stack objectの初期化
	M5.begin();
	M5.TP.SetRotation(90);
	M5.EPD.SetRotation(90);
	M5.EPD.Clear(true);
	canvas.createCanvas(540, 960);
	canvas.setTextDatum(TC_DATUM);
	canvas.setTextSize(4);

	Serial.print("... Initializing ...\r\n");

	canvas.drawString("... Initializing ...", 250, 640);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

	
	// Wi-Fi処理の開始
	setup_wifi();
	
	configTime(JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
	
	printLocalTime();

	WiFi.disconnect(true);
	WiFi.mode(WIFI_OFF);

	Serial.print("*** setup *** end ***\r\n");
}

// ---------------------------------------------------------------
void loop()
{
	Serial.printf("*** loop *** ncount = %d ***\r\n", ncount);
	delay(1000);
	printLocalTime();

	ncount++;
}

// ---------------------------------------------------------------
```

## Arduino IDE ##
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/c48e1f3b-5869-af7f-bdae-46d391f9fe21.png)