2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Paper: NTP の使い方

Last updated at Posted at 2022-06-23

こちらを参考にしました。
[M5Stack] M5Stackで時刻情報を扱う方法~NTPサーバーに接続して、正しい時刻を表示させる~

IMG_20220623_125421.jpg

プログラム

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

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?