ESP8266 Mô-đun DS1307 RTC
Hướng dẫn này hướng dẫn bạn cách sử dụng ESP8266 để đọc ngày và giờ từ mô-đun DS1307 RTC. Cụ thể, chúng ta sẽ tìm hiểu:
Cách kết nối mô-đun DS1307 RTC (đồng hồ thời gian thực) với ESP8266.
Cách lập trình ESP8266 để đọc ngày và giờ (giây, phút, giờ, ngày trong tuần, ngày trong tháng, tháng và năm) từ mô-đun DS1307 RTC.
Cách lập trình ESP8266 để tạo lịch trình hàng ngày bằng mô-đun DS1307 RTC.
Cách lập trình ESP8266 để tạo lịch trình hàng tuần bằng mô-đun DS1307 RTC.
Cách lập trình ESP8266 để tạo lịch trình vào một ngày cụ thể bằng mô-đun DS1307 RTC.
| 1 | × | ESP8266 NodeMCU ESP-12E | | |
| 1 | × | Recommended: ESP8266 NodeMCU ESP-12E (Uno-form) | | |
| 1 | × | USB Cable Type-A to Type-C (for USB-A PC) | | |
| 1 | × | USB Cable Type-C to Type-C (for USB-C PC) | | |
| 1 | × | Real-Time Clock DS1307 Module | | |
| 1 | × | CR2032 battery | | |
| 1 | × | dây jumper | | |
| 1 | × | (Khuyến nghị) Screw Terminal Expansion Board for ESP8266 | | |
| 1 | × | (Khuyến nghị) Power Splitter for ESP8266 Type-C | | |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) | | |
ESP8266 có một số hàm liên quan đến thời gian, ví dụ millis(), micros(). Tuy nhiên, những hàm này không thể cung cấp thời gian ngày giờ (giây, phút, giờ, ngày trong tuần, ngày trong tháng, tháng và năm). Để lấy ngày giờ, cần sử dụng một module Đồng hồ thời gian thực (RTC) như DS3231 hoặc DS1370. Module DS3231 có độ chính xác cao hơn so với DS1370. Để biết thêm thông tin, xem DS3231 vs DS1307.
Mô-đun đồng hồ thời gian thực DS1307 có 12 chân, nhưng để sử dụng thông thường, nó chỉ cần 4 chân: VCC, GND, SDA và SCL.
Chân SCL: là chân xung đồng hồ cho giao diện I2C.
Chân SDA: là chân dữ liệu tuần tự cho giao diện I2C.
Chân VCC: cấp nguồn cho module. Điện áp có thể từ 3,3V đến 5,5V.
Chân GND: là chân đất.
Module DS1307 có một khay pin, khi lắp pin CR2032 sẽ duy trì thời gian trên module khi nguồn chính bị tắt. Nếu không có pin, thông tin thời gian sẽ bị mất khi nguồn chính bị ngắt và nó sẽ cần được đặt lại.

This image is created using Fritzing. Click to enlarge image
Xem thêm Sơ đồ chân ESP8266 và Cách cấp nguồn cho ESP8266.
| DS1307 RTC Module | ESP8266 |
| Vin | 3.3V |
| GND | GND |
| SDA | GPIO4 |
| SCL | GPIO5 |
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
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);
#include <RTClib.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup () {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
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);
}
Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:
Nối các linh kiện như được hiển thị trong sơ đồ.
Kết nối bảng ESP8266 với máy tính của bạn bằng cáp USB.
Mở Arduino IDE trên máy tính của bạn.
Chọn board ESP8266 phù hợp, ví dụ NodeMCU 1.0 (ESP-12E Module), và cổng COM tương ứng.
Nhấn vào biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
Tìm kiếm “RTClib” và xác định thư viện RTC từ Adafruit.
Nhấn nút Cài đặt để thêm thư viện RTC.
Sao chép mã và mở nó trong Arduino IDE.
Nhấp nút Tải lên trong Arduino IDE để biên dịch và tải lên mã vào ESP8266.
Mở Trình giám sát nối tiếp.
Xem kết quả trên Trình giám sát nối tiếp.
Date & Time: 2021/10/6 (Wednesday) 9:9:35
Date & Time: 2021/10/6 (Wednesday) 9:9:36
Date & Time: 2021/10/6 (Wednesday) 9:9:37
Date & Time: 2021/10/6 (Wednesday) 9:9:38
Date & Time: 2021/10/6 (Wednesday) 9:9:39
Date & Time: 2021/10/6 (Wednesday) 9:9:40
Date & Time: 2021/10/6 (Wednesday) 9:9:41
Date & Time: 2021/10/6 (Wednesday) 9:9:42
Date & Time: 2021/10/6 (Wednesday) 9:9:43
Date & Time: 2021/10/6 (Wednesday) 9:9:44
#include <RTClib.h>
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_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup () {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
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");
} 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);
}
#include <RTClib.h>
#define SUNDAY 0
#define MONDAY 1
#define TUESDAY 2
#define WEDNESDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6
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_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup () {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
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");
} 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);
}
#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
DateTime EVENT_START(2021, AUGUST, 15, 13, 50);
DateTime EVENT_END(2021, SEPTEMBER, 29, 14, 10);
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void setup () {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
if (now.secondstime() >= EVENT_START.secondstime() &&
now.secondstime() < EVENT_END.secondstime()) {
Serial.println("It is on scheduled time");
} 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);
}
Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.