카테고리 없음

esp32 oled 표준시간 출력 코드

하고싶은거하는여자 2024. 10. 27. 16:11
728x90
반응형
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

////////////////////////////////////////////////

#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"

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

const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
// Set Korea Timezone Offset (UTC+9)
const long gmtOffset_sec = 32400; // 9 hours * 3600 seconds

// Daylight Saving Time (DST) is not observed in Korea, so set to 0
const int daylightOffset_sec = 0;

// Remove the unused time_zone variable
// time_zone = "CET-1CEST,M3.5.0,M10.5.0/3";  // Not needed for Korea

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("No time available (yet)");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();
}

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

  // OLED 디스플레이 초기화
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 오류");
    while (true);
  }
  // OLED 디스플레이 클리어
  display.clearDisplay();
///////////////////////////////////////////////////////////////////////////////////
  // First step is to configure WiFi STA and connect in order to get the current time and date.
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);

  /**
   * NTP server address could be acquired via DHCP,
   *
   * NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
   * otherwise SNTP option 42 would be rejected by default.
   * NOTE: configTime() function call if made AFTER DHCP-client run
   * will OVERRIDE acquired NTP server address
   */
  esp_sntp_servermode_dhcp(1);  // (optional)

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");

  // set notification call-back function
  sntp_set_time_sync_notification_cb(timeavailable);

  /**
   * This will set configured ntp servers and constant TimeZone/daylightOffset
   * should be OK if your time zone does not need to adjust daylightOffset twice a year,
   * in such a case time adjustment won't be handled automagically.
   */
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

  /**
   * A more convenient approach to handle TimeZones with daylightOffset
   * would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
   * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
   */
  //configTzTime(time_zone, ntpServer1, ntpServer2);
}

void loop() {
 
  //printLocalTime();  // it will take some time to sync time :)
  time_t rawtime = time(nullptr);
  struct tm* timeinfo = localtime(&rawtime);

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10, 0);

  if (timeinfo->tm_hour < 10)  
    display.print("0");
  display.print(timeinfo->tm_hour);
  display.print(":");
  if (timeinfo->tm_min < 10)  
    display.print("0");
  display.print(timeinfo->tm_min);
  display.print(":");
  if (timeinfo->tm_sec < 10)
    display.print("0");
  display.print(timeinfo->tm_sec);

  display.setTextSize(2); // 글자 크기 설정
  display.setCursor(0, 16); // 커서 위치 설정

  if (timeinfo->tm_mday < 10)  display.print("0");
  display.print(timeinfo->tm_mday);
  display.print("/");
  if (timeinfo->tm_mon + 1 < 10)  display.print("0");
  display.print(timeinfo->tm_mon + 1);
  display.print("/");
  display.print(timeinfo->tm_year + 1900);
  display.display(); // 디스플레이에 표시
 

 


  Serial.print(timeinfo->tm_year + 1900);
  Serial.print("년 ");
  Serial.print(timeinfo->tm_mon + 1);
  Serial.print("월 ");
  Serial.print(timeinfo->tm_mday);
  Serial.print("일 ");

  Serial.print(timeinfo->tm_hour);
  Serial.print(":");
  Serial.print(timeinfo->tm_min);
  Serial.print(":");
  Serial.println(timeinfo->tm_sec);
 
 
  delay(1000);
}



728x90
반응형