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.

wemosでntp

Posted at

概要

wemos d1で、ntpを使ってみた。

サンプルコード

#include <ESP8266WiFi.h>
#include <TZ.h>

const char * ssid = "****";
const char * password = "****";

void setup(void) {
	Serial.begin(115200);
	WiFi.begin(ssid, password);
	Serial.println("");
	while (WiFi.status() != WL_CONNECTED) 
	{
		delay(500);
		Serial.print(".");
	}
	Serial.println("");
	Serial.print("Connected to ");
	Serial.println(ssid);
	Serial.print("IP address: ");
	Serial.println(WiFi.localIP());
	configTime(TZ_Asia_Tokyo, "pool.ntp.org");
}
void loop(void) {
	static const char * wd[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	time_t t = time(NULL);
	struct tm * tm = gmtime(&t);
	char buff[12];
	sprintf(buff, "%04d/%02d/%02d(%s) ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday]);
	Serial.print("gmtime: ");
	Serial.print(buff);
	sprintf(buff, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
	Serial.println(buff);
	time_t t2 = mktime(tm) + 3600;
    struct tm * tm2 = localtime(&t2);
	Serial.print("asctime: ");
	Serial.println(asctime(tm2));
	time_t now = time(nullptr);
	Serial.print("Local time: ");
	Serial.println(ctime(&now));
	delay(3000);
}





以上。

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?