1
1

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 1 year has passed since last update.

M5Stack Core2: NTP で時刻を合わせる

Last updated at Posted at 2021-09-02

こちらのプログラムを参考にしました。
Study:M5Stack Core2 / RTC & touch

IMG_20210902_105316.jpg

プログラム

ntp_adjust/ntp_adjust.ino
// ---------------------------------------------------------------
/*
	ntp_adjust.ino

					Sep/02/2021

*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <time.h>
#include <WiFiMulti.h>

// for WiFi
char ssid[] = "some-ssid";
char password[] = "some-password";
WiFiMulti wifiMulti;

// for Time
const char* ntpServer = "ntp.jst.mfeed.ad.jp";
const long  gmtOffset_sec = 9 * 3600;
const int   daylightOffset_sec = 0;
RTC_DateTypeDef RTC_DateStruct; // Data
RTC_TimeTypeDef RTC_TimeStruct; // Time
static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
struct tm timeinfo;
String dateStr;
String timeStr;

// ---------------------------------------------------------------
void getTimeFromNTP()
{
	// To get Time from NTP server
	configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
	while (!getLocalTime(&timeinfo))
		{
		delay(1000);
		}
}

// ---------------------------------------------------------------
void setNTP2RTC()
{
	// timeSet
	getTimeFromNTP();
	getLocalTime(&timeinfo);
	// read RTC
	M5.Rtc.GetTime(&RTC_TimeStruct);
	M5.Rtc.GetDate(&RTC_DateStruct);
	// --- to over write date&time
	RTC_DateStruct.Year = timeinfo.tm_year + 1900;
	RTC_DateStruct.Month = timeinfo.tm_mon + 1;
	RTC_DateStruct.Date = timeinfo.tm_mday;
	RTC_DateStruct.WeekDay = timeinfo.tm_wday;
	M5.Rtc.SetDate(&RTC_DateStruct);
	RTC_TimeStruct.Hours = timeinfo.tm_hour;
	RTC_TimeStruct.Minutes = timeinfo.tm_min;
	RTC_TimeStruct.Seconds = timeinfo.tm_sec;
	M5.Rtc.SetTime(&RTC_TimeStruct);
}

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
	M5.Lcd.fillScreen(BLACK);
	M5.Lcd.setTextColor(WHITE,BLACK);

	wifiMulti.addAP(ssid, password);

	Serial.println("*** setup *** end ***");
}

// ---------------------------------------------------------------
void loop()
{
	M5.Rtc.GetDate(&RTC_DateStruct);
	M5.Rtc.GetTime(&RTC_TimeStruct);
  M5.Lcd.setTextSize(3);
	M5.Lcd.setCursor(10, 60, 1);
	M5.Lcd.printf("%04d.%02d.%02d %s\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, wd[RTC_DateStruct.WeekDay]);
 M5.Lcd.setTextSize(5);
	M5.Lcd.setCursor(30, 140, 1);
	M5.Lcd.printf("%02d:%02d:%02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

	delay(500);
}

// ---------------------------------------------------------------

Arduino IDE

image.png

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?