0
0

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.

ESP32: getLocalTime の使い方

Last updated at Posted at 2022-11-26

getLocalTime が、一度目は、NTP サーバーから時刻を取得し、二度目からは、RTC から取得することを確認するプログラムです。
一度目の取得後に、wifi を切断します。

プログラム

image.png

localtime.ino
// ---------------------------------------------------------------
/*
	localtime.ino

					Nov/26/2022

*/
// ---------------------------------------------------------------
#include <WiFi.h>

const char* SSID = "*****";
const char* PASSWORD = "*****";

// ---------------------------------------------------------------
void connectWiFi(const char* ssid, const char* password)
{
	Serial.printf("Connecting to %s ", ssid);
	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED)
		{
		delay(500);
		Serial.print(".");
		}

	Serial.println(" CONNECTED");
}

// ---------------------------------------------------------------
void disconnectWiFi(void)
{
	WiFi.disconnect(true);
	WiFi.mode(WIFI_OFF);
}

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

	const char* ntpServer = "ntp.jst.mfeed.ad.jp";
	const long gmtOffset_sec = 9 * 3600;
	const int daylightOffset_sec = 0;

	configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

	while (!getLocalTime(&timeinfo))
       		{
		delay(1000);
		}

	Serial.println(&timeinfo, "FromNTP: %Y-%m-%d %H:%M:%S");
	disconnectWiFi();
}

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

	if(getLocalTime(&timeinfo))
		{
		Serial.println("*** success ***");
		Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
		Serial.println(&timeinfo, "%Y/%m/%d(%a) %H:%M:%S");
		Serial.println(&timeinfo, "%Y-%m-%d %H:%M:%S");
		}
	else
		{
		Serial.println("No time available (yet)");
		}
}
 
// ---------------------------------------------------------------
void setup()
{
	Serial.begin(115200);

	Serial.println("*** setup start ***");

	delay(1000);

	connectWiFi(SSID, PASSWORD);

	delay(1000);
	getTimeFromNTP();
	delay(1000);

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

// ---------------------------------------------------------------
void loop()
{
	printLocalTime();

	unsigned long time = millis();
	unsigned long sec = time / 1000;
	unsigned long min = time / 60000;
	unsigned long hour = time / 3600000;
	Serial.print(time);
	Serial.print(" ms\t");
	Serial.print(sec);
	Serial.print(" sec\t");
	Serial.print(min);
	Serial.print(" min\t");
	Serial.print(hour);
	Serial.println(" hour");


	delay(3000);
}

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

実行結果

$ cu -l /dev/ttyACM0 -s 115200
Connected.
FromNTP: 2022-11-26 11:12:20
*** setup end ***
*** success ***
Saturday, November 26 2022 11:12:21
2022/11/26(Sat) 11:12:21
2022-11-26 11:12:21
7968 ms	7 sec	0 min	0 hour
*** success ***
Saturday, November 26 2022 11:12:24
2022/11/26(Sat) 11:12:24
2022-11-26 11:12:24
10978 ms	10 sec	0 min	0 hour
*** success ***
Saturday, November 26 2022 11:12:27
2022/11/26(Sat) 11:12:27
2022-11-26 11:12:27
13978 ms	13 sec	0 min	0 hour

ESP32 は、ping に応答するので、次のようにして、ネットワークの遮断が確認できます。

$ ping -c 12 192.168.1.7
PING 192.168.1.7 (192.168.1.7) 56(84) bytes of data.
64 bytes from 192.168.1.7: icmp_seq=4 ttl=255 time=81.2 ms
64 bytes from 192.168.1.7: icmp_seq=5 ttl=255 time=95.9 ms
64 bytes from 192.168.1.7: icmp_seq=6 ttl=255 time=145 ms

--- 192.168.1.7 ping statistics ---
12 packets transmitted, 3 received, 75% packet loss, time 11180ms
rtt min/avg/max/mdev = 81.192/107.393/145.047/27.297 ms
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?