Newer
Older
m5stickcplus / src / ntp01.ino
@Motoki Motoki 22 days ago 1 KB Plus2
#include <M5Unified.h>
#include <WiFi.h>

const char* ssid = "ics-ap";
const char* password = "jikkenics";

void setup() {
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200; // ボーレートを115200に設定
  M5.begin(cfg);
  M5.Display.setRotation(3);
  M5.Display.setFont(&fonts::lgfxJapanGothic_16);
  M5.Display.println("NTP 01");
  M5.Display.setTextScroll(true);
  //  Serial.begin(115200);
  //  M5.Display.println("シリアル開始");

  WiFi.begin(ssid, password); // 接続開始
  while (WiFi.status() != WL_CONNECTED) { // 接続中...
    M5.Display.print(".");
    M5.delay(500);
  }
  // 接続完了!!
  char* ntpserver = "ntp.nict.jp" ;
  configTime(9 * 3600, 0, ntpserver) ; //GMTとの時差(秒) が9*3600, サマータイムで進める時間(秒)が0


  M5.Display.fillScreen(GREEN);
  M5.Display.setCursor(0, 40);
  M5.Display.setTextColor(BLACK, GREEN);
  M5.Display.print("  Wifi Connected!\n  ");
  String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス
  M5.Display.println(gotip);
  M5.delay(1500);
}

void loop() {
  M5.update();

  struct tm localTime;
  char buf[30];
  getLocalTime(&localTime);
  sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d\n",
          localTime.tm_year + 1900,
          localTime.tm_mon + 1,
          localTime.tm_mday,
          localTime.tm_hour,
          localTime.tm_min,
          localTime.tm_sec
         );
  M5.Display.setTextColor(GREEN,BLACK);
  M5.Display.print(buf);
  Serial.print(buf);

  if (M5.BtnA.wasReleased()) {
    M5.Display.print(WiFi.localIP().toString());
  }
  M5.delay(1000);
}