๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[UNO R4 WIFI example] Real-Time ClockRTCAlarm ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Arduino

[UNO R4 WIFI example] Real-Time ClockRTCAlarm

์ง•์ง•์•ŒํŒŒ์นด 2023. 11. 28. 16:37
728x90
๋ฐ˜์‘ํ˜•

< ๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ARDUINO docs ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-) >

 

โญ Arduino UNO R4 WiFi

Wi-Fi ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐํ•˜๊ณ  ๋„คํŠธ์›Œํฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ESP32-S3 ๋ชจ๋“ˆ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์Œ

HTTPS, MQTT, UDP๋ฅผ ํฌํ•จํ•œ ํ”„๋กœํ† ์ฝœ์ด ํ…Œ์ŠคํŠธ๋˜๊ณ  ์ง€์›๋จ

WiFiS3 Arduino UNO R4 Core ์™€ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

โญ WiFi Web Server LED Blink

๐ŸŒณ Print Date

// UNO R4 WiFi์˜ RTC๋Š” Renesas ์ฝ”์–ด ์— ํฌํ•จ๋œ RTC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šค
// ์‹œ๊ฐ„์„ ์„ค์ •/๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•Œ๋žŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ
#include "RTC.h"

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

  RTC.begin();
  
  // RTC์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๋ ค๋ฉด ์ผ, ์›”, ์—ฐ๋„, ์‹œ, ๋ถ„, ์ดˆ๋ฅผ ์ง€์ •ํ•˜๊ณ  ์š”์ผ๊ณผ ์ผ๊ด‘ ์ ˆ์•ฝ ๋ชจ๋“œ๋ฅผ ์ง€์ •
  RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);

  // setTime() : ์‹œ๊ฐ„์„ ์„ค์ •
  RTC.setTime(startTime);
}

void loop() {
  RTCTime currentTime;

  // Get current time from RTC
  // getTime() : ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
  RTC.getTime(currentTime);
  
  //Unix timestamp
  Serial.print("Unix timestamp: ");
  Serial.println(currentTime.getUnixTime());

  delay(1000);
}

 

๐ŸŒณ Print Date & Time

// UNO R4 WiFi์˜ RTC๋Š” Renesas ์ฝ”์–ด ์— ํฌํ•จ๋œ RTC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šค
// ์‹œ๊ฐ„์„ ์„ค์ •/๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•Œ๋žŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ
#include "RTC.h"

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

  RTC.begin();
  
  RTCTime startTime(28, Month::NOVEMBER, 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);
}

 

๐ŸŒณ AlarmCallback

// UNO R4 WiFi์˜ RTC๋Š” Renesas ์ฝ”์–ด ์— ํฌํ•จ๋œ RTC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šค
// ์‹œ๊ฐ„์„ ์„ค์ •/๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•Œ๋žŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ
#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);

  // ์ฃผ๊ธฐ์  ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ˜๋ณต ์ฝœ๋ฐฑ์„ ์„ค์ • (์•Œ๋žŒ ์ฝœ๋ฐฑ ํ•จ์ˆ˜, alarmtime ๊ธฐ๊ฐ„)
  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;
  }
}

// 2์ดˆ๋งˆ๋‹ค ํ‘œ์‹œ๋“ฑ์ด ๊นœ๋ฐ•
void periodicCallback()
{
  irqFlag = true;
}

 

๐ŸŒณ  Network Time Protocol

// UNO R4 WiFi์˜ RTC๋Š” Renesas ์ฝ”์–ด ์— ํฌํ•จ๋œ 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

// ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด NTP ์„œ๋ฒ„์— ์š”์ฒญ
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 = 9;
  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(){}

 

๐ŸŒณ  Network Time Protocol (ver. callback ํ•จ์ˆ˜๋กœ 1์ดˆ๋งˆ๋‹ค ์‹œ๊ฐ„ ๋ฐ›์•„์˜ค๊ธฐ)

// UNO R4 WiFi์˜ RTC๋Š” Renesas ์ฝ”์–ด ์— ํฌํ•จ๋œ 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

// ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด NTP ์„œ๋ฒ„์— ์š”์ฒญ
NTPClient timeClient(Udp);
RTCTime currentTime;
volatile bool irqFlag = false;

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();
    Serial.print("hihi");
    auto timeZoneOffsetHours = 9;
    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

    RTC.getTime(currentTime);
    // ์ฃผ๊ธฐ์  ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ˜๋ณต ์ฝœ๋ฐฑ์„ ์„ค์ • (์•Œ๋žŒ ์ฝœ๋ฐฑ ํ•จ์ˆ˜, alarmtime ๊ธฐ๊ฐ„)
    if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_1_SEC))
    {
        Serial.println("ERROR: periodic callback not set");
    }
}

void loop()
{
    // 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.
    if (irqFlag)
    {
        auto timeZoneOffsetHours = 9;
        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

        RTC.getTime(currentTime);
        Serial.println("The RTC was just set to: " + String(currentTime));
        irqFlag = false;
    }
}

// 1์ดˆ๋งˆ๋‹ค ์‹œ๊ฐ„ ๋ณ€๊ฒฝ
void periodicCallback()
{
    irqFlag = true;
}

728x90
๋ฐ˜์‘ํ˜•
Comments