ESP8266 - RTC: ESP8266 - RTC | ESP8266 - RTC

이 튜토리얼은 ESP8266을 사용하여 RTC 모듈에서 날짜와 시간을 읽는 방법을 안내합니다. 자세히, 우리는 배울 것입니다:

준비물

1×ESP8266 NodeMCU Amazon
1×Micro USB Cable Amazon
1×Real-Time Clock DS3231 Module Amazon
1×CR2032 battery Amazon
1×Jumper Wires Amazon
1×(Optional) 5V Power Adapter for ESP8266 Amazon
1×(Optional) ESP8266 Screw Terminal Adapter Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

실시간 시계 DS3231 모듈에 관하여

ESP8266에는 millis() 및 micros()와 같이 시간과 관련된 특정 함수가 있습니다. 그러나 이 함수들은 시간(초, 분, 시, 일, 날짜, 월, 년)을 제공하지 않습니다. 이 정보를 얻으려면 DS3231 또는 DS1370과 같은 실시간 시계(RTC) 모듈을 사용해야 합니다. DS3231 모듈은 DS1370보다 더 높은 정밀도를 가지고 있습니다. 보다 자세한 정보는 DS3231 vs DS1307을 참조하세요.

RTC 모듈 핀아웃

실시간 시계 DS3231 모듈에는 10개의 핀이 있습니다:

  • 32K: 이 핀은 안정적이고 정확한 기준 클록을 제공합니다.
  • SQW: 이 핀은 1Hz, 4kHz, 8kHz 또는 32kHz에서 사각파를 생성하며 프로그래밍을 통해 관리할 수 있습니다. 또한 다양한 시간 의존적 애플리케이션에서 알람 인터럽트로 사용될 수 있습니다.
  • SCL: 이것은 I2C 인터페이스를 위한 직렬 클록 핀입니다.
  • SDA: 이것은 I2C 인터페이스를 위한 직렬 데이터 핀입니다.
  • VCC: 이 핀은 모듈에 전력을 공급합니다. 3.3V에서 5.5V 사이일 수 있습니다.
  • GND: 이것은 지상 핀입니다.

정상 작동을 위해서는 VCC, GND, SDA, SCL 이라는 네 개의 핀이 필요합니다.

Real-Time Clock DS3231 Module pinout

DS3231 모듈에는 CR2032 배터리가 삽입되면 주전원이 꺼져 있을 때에도 모듈의 시간을 계속 유지시켜 주는 배터리 홀더가 있습니다. 배터리가 없는 경우, 주전원이 끊어지면 시간 정보가 소실되어 다시 설정해야 합니다.

선연결

ESP8266 NodeMCU Real-Time Clock DS3231 wiring diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

ESP8266의 핀 배치ESP8266 및 다른 구성 요소에 전원을 공급하는 방법에 대해 더 많이 보십시오.

ESP8266 - DS3231 RTC 모듈

DS3231 RTC Module ESP8266
Vin 3.3V
GND GND
SDA GPIO4
SCL GPIO5

DS3231 RTC 모듈 프로그래밍 방법

라이브러리를 포함하세요.

#include <RTClib.h>

RTC 객체를 생성하십시오.

RTC_DS3231 rtc;

RTC 설정 과정을 시작하세요.

if (! rtc.begin()) { Serial.println("RTC를 찾을 수 없습니다"); while (1); }

스케치가 처음 컴파일될 때 컴퓨터의 날짜와 시간으로 RTC를 설정하세요.

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

RTC 모듈에서 날짜와 시간 데이터를 검색합니다.

DateTime now = rtc.now(); Serial.print("Date & Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(now.dayOfTheWeek()); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC);

ESP8266 코드 - 날짜와 시간을 얻는 방법

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-rtc */ #include <RTClib.h> RTC_DS3231 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // RTC 모듈 설정 if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1); } // 이 스케치가 컴파일된 PC의 날짜 및 시간으로 RTC를 자동으로 설정 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // 명시적인 날짜 및 시간으로 RTC를 수동으로 설정, 예를 들어 // 2021년 1월 21일 오전 3시를 설정하려면 아래를 호출: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); Serial.print("Date & Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC); delay(1000); // 1초 지연 }

사용 방법

Arduino IDE에서 ESP8266을 시작하는 방법은 다음 단계를 따르세요:

  • ESP8266을 처음 사용하는 경우 Arduino IDE에서 ESP8266 환경 설정하는 방법 튜토리얼을 확인하세요.
  • 다이어그램에 표시된 대로 구성 요소를 배선하세요.
  • USB 케이블을 사용하여 ESP8266 보드를 컴퓨터에 연결하세요.
  • 컴퓨터에서 Arduino IDE를 엽니다.
  • 올바른 ESP8266 보드를 선택하세요. 예를 들어, NodeMCU 1.0 (ESP-12E Module) 및 해당 COM 포트를 선택합니다.
  • Arduino IDE의 왼쪽 바에 있는 Libraries 아이콘을 클릭하세요.
  • “RTClib”을 검색하여 Adafruit의 RTC 라이브러리를 찾으세요.
  • RTC 라이브러리를 설치하려면 Install 버튼을 누르세요.
ESP8266 NodeMCU RTC library
  • 코드를 복사하고 Arduino IDE로 열어주세요.
  • IDE에서 Upload 버튼을 클릭하여 ESP8266으로 전송하세요.
  • 시리얼 모니터를 엽니다.
  • 시리얼 모니터에서 출력을 확인하세요.
COM6
Send
Date & Time: 2021/10/6 (Wednesday) 11:27:35 Date & Time: 2021/10/6 (Wednesday) 11:27:36 Date & Time: 2021/10/6 (Wednesday) 11:27:37 Date & Time: 2021/10/6 (Wednesday) 11:27:38 Date & Time: 2021/10/6 (Wednesday) 11:27:39 Date & Time: 2021/10/6 (Wednesday) 11:27:40 Date & Time: 2021/10/6 (Wednesday) 11:27:41 Date & Time: 2021/10/6 (Wednesday) 11:27:42 Date & Time: 2021/10/6 (Wednesday) 11:27:43 Date & Time: 2021/10/6 (Wednesday) 11:27:44
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

ESP8266 코드 - 일일 일정을 만드는 방법

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-rtc */ // Date and time functions using a DS3231 RTC connected via I2C and Wire lib #include <RTClib.h> // 13:50부터 14:10까지의 이벤트 uint8_t DAILY_EVENT_START_HH = 13; // 이벤트 시작 시간: 시 uint8_t DAILY_EVENT_START_MM = 50; // 이벤트 시작 시간: 분 uint8_t DAILY_EVENT_END_HH = 14; // 이벤트 종료 시간: 시 uint8_t DAILY_EVENT_END_MM = 10; // 이벤트 종료 시간: 분 RTC_DS3231 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } // sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.hour() >= DAILY_EVENT_START_HH && now.minute() >= DAILY_EVENT_START_MM && now.hour() < DAILY_EVENT_END_HH && now.minute() < DAILY_EVENT_END_MM) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

ESP8266 코드 - 주간 일정 만드는 방법

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-rtc */ // DS3231 RTC를 I2C와 Wire 라이브러리를 통해 연결하여 날짜 및 시간 함수 사용 #include <RTClib.h> // 변경할 수 없는 매개변수들 #define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 // 월요일에 있으며, 13:50부터 14:10까지의 이벤트 uint8_t WEEKLY_EVENT_DAY = MONDAY; uint8_t WEEKLY_EVENT_START_HH = 13; // 이벤트 시작 시간: 시 uint8_t WEEKLY_EVENT_START_MM = 50; // 이벤트 시작 시간: 분 uint8_t WEEKLY_EVENT_END_HH = 14; // 이벤트 종료 시간: 시 uint8_t WEEKLY_EVENT_END_MM = 10; // 이벤트 종료 시간: 분 RTC_DS3231 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // RTC 모듈 설정 if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } // 이 스케치가 컴파일된 PC의 날짜 & 시간으로 RTC를 설정 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // 명시적인 날짜 & 시간으로 RTC를 설정, 예: 2021년 1월 21일 오전 3시를 설정하려면 다음을 호출: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.dayOfTheWeek() == WEEKLY_EVENT_DAY && now.hour() >= WEEKLY_EVENT_START_HH && now.minute() >= WEEKLY_EVENT_START_MM && now.hour() < WEEKLY_EVENT_END_HH && now.minute() < WEEKLY_EVENT_END_MM) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

ESP8266 코드 - 특정 날짜에 일정을 만드는 방법

/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-rtc */ // DS3231 RTC를 I2C와 Wire 라이브러리를 사용하여 날짜와 시간 기능 사용 #include <RTClib.h> // 변경 불가능한 매개변수 #define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 #define JANUARY 1 #define FEBRUARY 2 #define MARCH 3 #define APRIL 4 #define MAY 5 #define JUNE 6 #define JULY 7 #define AUGUST 8 #define SEPTEMBER 9 #define OCTOBER 10 #define NOVEMBER 11 #define DECEMBER 12 // 2021년 8월 15일 13:50부터 2021년 9월 29일 14:10까지의 이벤트 DateTime EVENT_START(2021, AUGUST, 15, 13, 50); DateTime EVENT_END(2021, SEPTEMBER, 29, 14, 10); RTC_DS3231 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // RTC 모듈 설정 if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1); } // 이 스케치가 컴파일된 PC의 날짜와 시간으로 RTC를 설정 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // 명시적인 날짜와 시간으로 RTC를 설정하려면, 예를 들어 // 2021년 1월 21일 새벽 3시를 설정하려면 다음과 같이 호출하십시오: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.secondstime() >= EVENT_START.secondstime() && now.secondstime() < EVENT_END.secondstime()) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!