Arduino UNO R4 WiFi — Часы реального времени
В этом руководстве вы узнаете, как работать с часами реального времени (RTC) на плате Arduino UNO R4 WiFi. Модуль RTC встроен в микроконтроллер UNO R4 WiFi (RA4M1).
Цели
Цели данного проекта:
Установить начальную дату RTC
Получить дату и время из RTC в календарном формате.
Получить время в формате Unix.
Необходимое оборудование и программное обеспечение
Arduino IDE (онлайн-версия или офлайн-версия)
Часы реального времени (RTC)
Доступ к RTC на UNO R4 WiFi осуществляется с помощью библиотеки RTC, которая входит в пакет плат UNO R4. Эта библиотека позволяет устанавливать и получать время, а также использовать будильники для вызова прерываний.
Важно
UNO R4 WiFi имеет вывод VRTC, который используется для поддержания работы встроенных RTC, даже когда питание платы отключено. Для этого подайте напряжение в диапазоне от 1,6 до 3,6 В на вывод VRTC.
Существует множество практических примеров использования RTC, и примеры, представленные на этой странице, помогут вам начать работу с ними.
Установка времени
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE)RTC.setTime(startTime)
Чтобы установить начальное время для RTC, вы можете создать объект RTCTime. Здесь вы можете указать день, месяц, год, час, минуту, секунду, а также задать день недели и режим летнего времени.
Затем для установки времени используйте метод setTime().
Пример:
#include "RTC.h"
void setup() {
Serial.begin(9600);
RTC.begin();
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(startTime);
}
void loop(){
}
Получение времени
RTC.getTime(currentTime)
Чтобы получить время, нам нужно создать объект RTCTime и использовать метод getTime() для получения текущего времени.
Этот пример устанавливает и получает время, сохраняя его в объекте RTCTime с именем currentTime.
#include "RTC.h"
void setup() {
Serial.begin(9600);
RTC.begin();
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(startTime);
}
void loop(){
RTCTime currentTime;
// Get current time from RTC
RTC.getTime(currentTime);
}
Вывод даты и времени
Приведённые выше примеры показывают, как установить и получить время и сохранить его в объекте. Эти данные можно извлечь с помощью ряда методов:
getDayOfMonth()getMonth()getYear()getHour()getMinutes()getSeconds()
Приведённый ниже пример выводит дату и время из объекта currentTime.
#include "RTC.h"
void setup() {
Serial.begin(9600);
RTC.begin();
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(startTime);
}
void loop() {
RTCTime currentTime;
// Get current time from RTC
RTC.getTime(currentTime);
// Print out date (DD/MM//YYYY)
Serial.print(currentTime.getDayOfMonth());
Serial.print("/");
Serial.print(Month2int(currentTime.getMonth()));
Serial.print("/");
Serial.print(currentTime.getYear());
Serial.print(" - ");
// Print time (HH/MM/SS)
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());
delay(1000);
}
Unix
currentTime.getUnixTime()
Чтобы получить временную метку Unix, используйте метод getUnixTime().
#include "RTC.h"
void setup() {
Serial.begin(9600);
RTC.begin();
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(startTime);
}
void loop() {
RTCTime currentTime;
// Get current time from RTC
RTC.getTime(currentTime);
//Unix timestamp
Serial.print("Unix timestamp: ");
Serial.println(currentTime.getUnixTime());
delay(1000);
}
Периодическое прерывание
Периодическое прерывание позволяет задать повторяющийся обратный вызов (callback).
Для этого вам нужно инициализировать периодический callback с помощью метода setPeriodicCallback():
RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)
Вам также потребуется создать функцию, которая будет вызываться:
void periodicCallback() { код для выполнения }
Примечание
Обработчик прерывания (IRQ) выполняется очень быстро. Размещение большого количества кода в нём не является хорошей практикой, поэтому в приведённом ниже примере мы только переключаем один флаг — irqFlag.
Приведённый ниже пример мигает светодиодом каждые 2 секунды:
#include "RTC.h"
volatile bool irqFlag = false;
volatile bool ledState = false;
const int led = LED_BUILTIN;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
// Initialize the RTC
RTC.begin();
// RTC.setTime() must be called for RTC.setPeriodicCallback to work, but it doesn't matter
// what date and time it's set to
RTCTime mytime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(mytime);
if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_2_SEC)) {
Serial.println("ERROR: periodic callback not set");
}
}
void loop(){
if(irqFlag){
Serial.println("Timed CallBack");
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
irqFlag = false;
}
}
void periodicCallback()
{
irqFlag = true;
}
Период можно задать с помощью следующих перечислений:
ONCE_EVERY_2_SECONCE_EVERY_1_SECN2_TIMES_EVERY_SECN4_TIMES_EVERY_SECN8_TIMES_EVERY_SECN16_TIMES_EVERY_SECN32_TIMES_EVERY_SECN64_TIMES_EVERY_SECN128_TIMES_EVERY_SECN256_TIMES_EVERY_SEC
Callback по будильнику
RTC.setAlarmCallback(alarm_cbk, alarmtime, am)
unsigned long previousMillis = 0;
const long interval = 1000;
bool ledState = false;
// Include the RTC library
#include "RTC.h"
void setup() {
//initialize Serial Communication
Serial.begin(9600);
//define LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the RTC
RTC.begin();
// RTC.setTime() must be called for RTC.setAlarmCallback to work, but it doesn't matter
// what date and time it's set to in this example
RTCTime initialTime(7, Month::JUNE, 2023, 13, 03, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(initialTime);
// Trigger the alarm every time the seconds are zero
RTCTime alarmTime;
alarmTime.setSecond(0);
// Make sure to only match on the seconds in this example - not on any other parts of the date/time
AlarmMatch matchTime;
matchTime.addMatchSecond();
//sets the alarm callback
RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime);
}
void loop() {
// in the loop, we continuously print the alarm's current state
// this is for debugging only and has no effect on the alarm whatsoever
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Serial.print("Alarm state: ");
Serial.println(ledState);
}
}
// this function activates every minute
// and changes the ledState boolean
void alarmCallback() {
if (!ledState) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
ledState = !ledState;
}
Протокол сетевого времени (NTP)
Чтобы получить и сохранить текущее время, мы можем отправить запрос на NTP-сервер pool.ntp.org. Это позволит получить временную метку UNIX и сохранить её в объекте RTC.
Исходный код на Github
/**
* RTC_NTPSync
*
* This example shows how to set the RTC (Real Time Clock) on the Portenta C33 / UNO R4 WiFi
* to the current date and time retrieved from an NTP server on the Internet (pool.ntp.org).
* Then the current time from the RTC is printed to the Serial port.
*
* Instructions:
* 1. Download the NTPClient library (https://github.com/arduino-libraries/NTPClient) through the Library Manager
* 2. Change the WiFi credentials in the arduino_secrets.h file to match your WiFi network.
* 3. Upload this sketch to Portenta C33 / UNO R4 WiFi.
* 4. Open the Serial Monitor.
*
* Initial author: Sebastian Romero @sebromero
*
* Find the full UNO R4 WiFi RTC documentation here:
* https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
*/
// Include the RTC library
#include "RTC.h"
//Include the NTP library
#include <NTPClient.h>
#if defined(ARDUINO_PORTENTA_C33)
#include <WiFiC3.h>
#elif defined(ARDUINO_UNOWIFIR4)
#include <WiFiS3.h>
#endif
#include <WiFiUdp.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int wifiStatus = WL_IDLE_STATUS;
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
NTPClient timeClient(Udp);
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void connectToWiFi(){
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (wifiStatus != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
wifiStatus = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to WiFi");
printWifiStatus();
}
void setup(){
Serial.begin(9600);
while (!Serial);
connectToWiFi();
RTC.begin();
Serial.println("\nStarting connection to server...");
timeClient.begin();
timeClient.update();
// Get the current date and time from an NTP server and convert
// it to UTC +2 by passing the time zone offset in hours.
// You may change the time zone offset to your local one.
auto timeZoneOffsetHours = 2;
auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
Serial.print("Unix time = ");
Serial.println(unixTime);
RTCTime timeToSet = RTCTime(unixTime);
RTC.setTime(timeToSet);
// Retrieve the date and time from the RTC and print them
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.println("The RTC was just set to: " + String(currentTime));
}
void loop(){}
Обратите внимание, что вам также потребуется создать новую вкладку с именем arduino_secrets.h. Она используется для хранения ваших учётных данных. В этот файл необходимо добавить:
#define SECRET_SSID "" //network name
#define SECRET_PASS "" //network password
Заключение
В этом руководстве показано, как использовать RTC на UNO R4 WiFi: установка начального времени, настройка будильника, а также получение времени в календарном или Unix-формате.
Подробнее об этой плате читайте в документации Arduino UNO R4 WiFi.