Help us understand the problem. What is going on with this article?

ESP32 NTPを使って時刻をGET

More than 1 year has passed since last update.

ESP32のWifiを使ってネットワークから時刻を取得します。

Qiita初投稿です。 どんな感じに掲示されるのか確認。 
Aruduino SAMPLEにあるSimpleTimeをちょこっと手直しして日本時間対応にしただけのもの。 WifiのIDとパスワードを入れてコンパイル、ESP32へ転送して実行。
DHCPからIPアドレスが払い出されたら、時刻表示します。

SimpleTime.ino
#include <WiFi.h>
#include "time.h"

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

const char* ntpServer =  "ntp.jst.mfeed.ad.jp";  //日本のNTPサーバー選択
const long  gmtOffset_sec = 9 * 3600;  //9時間の時差を入れる
const int   daylightOffset_sec = 0;  //夏時間はないのでゼロ

void printLocalTime()
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%Y %m %d %a %H:%M:%S");  //日本人にわかりやすい表記へ変更
}

void setup()
{
  Serial.begin(115200);

  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");

  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}

これで、シリアルのターミナル画面に1秒おきに時刻が表示されます。

ふむふむ、なんとなくQittaへの寄稿方法がわかってきた。

coo0659
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした