Arduino UNO R4 WiFi リアルタイムクロック

UNO R4 WiFiのリアルタイムクロック(RTC)にアクセスする方法を学びます。


AUTHOR: Karl Söderby


このチュートリアルでは、Arduino UNO R4 WiFiのリアルタイムクロック(RTC)にアクセスする方法を学びます。RTCは、UNO R4 WiFiのマイクロコントローラー(RA4M1)に内蔵されています。

目的

この記事では、以下を学びます。

  • RTCの開始日を設定する
  • カレンダー形式で、RTCから日付と時刻を取得する
  • Unix形式で時刻を取得する

必要なハードウェアとソフトウェア

リアルタイムクロック(RTC)

UNO R4 WiFiのRTCには、Renesas Coreに含まれる、RTCライブラリを使ってアクセスします。このライブラリでは、時刻の設定・取得に加え、アラームを使い割り込みを動作させることもできます。

i
UNO R4 WiFiには、VRTCピンが備わっています。これは、電源オフのときでも、ボード上のRTCを動作させ続けます。これを利用するには、1.6-3.6Vの範囲の電圧をVRTCピンに供給します。

RTCを利用する実用的な例がたくさんあります。このページで提供される例は、RTCを使い始めるのに役立ちます。

時刻設定

  • RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE)
  • RTC.setTime(startTime)

RTCの開始時刻を設定するには、RTCTimeを生成します。このとき、日・月・年・時・分・秒を設定できます。曜日と夏時間モードも設定できます。

その後時刻を設定するには、setTime()メソッドを使います。

例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#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()メソッドを使って、現在時刻を取得します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#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オブジェクトから、日付と時刻を表示します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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()メソッドを使います。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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);
}

周期割り込み

周期割り込みを使うと、定期的なコールバックを設定できます。

これを使うには、setPeriodicCallback()メソッドを使い、周期割り込みを初期化する必要があります。

  • RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)

呼び出される関数も作成する必要があります。

  • void periodicCallback() { code to be executed }
i
IRQの実行時間はとても短いことに注意してください。多くのコードを書くのはよくありません。以下の例では、irqFlagという一つのフラグを切り替えるだけにしています。

以下の例では2秒ごとにライトを点滅させます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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_SEC
  • ONCE_EVERY_1_SEC
  • N2_TIMES_EVERY_SEC
  • N4_TIMES_EVERY_SEC
  • N8_TIMES_EVERY_SEC
  • N16_TIMES_EVERY_SEC
  • N32_TIMES_EVERY_SEC
  • N64_TIMES_EVERY_SEC
  • N128_TIMES_EVERY_SEC
  • N256_TIMES_EVERY_SEC

アラームコールバック

  • RTC.setAlarmCallback(alarm_cbk, alarmtime, am)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}

Network Time Protocol(NTP)

現在時刻を取得し設定するには、NTPサーバー(pool.ntp.org)にリクエストを送ります。以下は、UNIX形式の時刻を取得し、RTCオブジェクトに格納します。

i
ソースコードは、Githubにもあります。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * 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という新しいタブを作成する必要があります。これは、ID/パスワードを格納するためのものです。このファイルには、以下を追加します。

1
2
#define SECRET_SSID "" //network name
#define SECRET_PASS "" //network password

まとめ

このチュートリアルでは、時刻の設定やアラームの設定、カレンダー形式とUNIX形式での時刻の取得など、UNO R4 WiFiのRTCの使い方を示しました。

このボードの詳細は、Arduino UNO R4 WiFiの文書を参照してください。

オリジナルのページ

https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc

最終更新日

November 12, 2023

inserted by FC2 system