๐ฉ๐ป 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
๋ฐ์ํ