Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
M5Paper RTC set from NTP
#include <M5EPD.h>
#include <WiFi.h>
rtc_time_t RTCtime;
rtc_date_t RTCDate;
char timeStrbuff[64];
static const int JST = 3600 * 9;
static const char *wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
RTC_SLOW_ATTR bool ntpDataFlag = false;
const char* ssid = "ssid";
const char* password = "password";
time_t t;
struct tm *tm;
void setupTime() {
t = time(NULL);
tm = localtime(&t);
RTCtime.hour = tm->tm_hour;
RTCtime.min = tm->tm_min;
RTCtime.sec = tm->tm_sec;
M5.RTC.setTime(&RTCtime);
RTCDate.year = tm->tm_year + 1900;
RTCDate.mon = tm->tm_mon + 1;
RTCDate.day = tm->tm_mday;
M5.RTC.setDate(&RTCDate);
}
void setup() {
M5.begin();
M5.RTC.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.println();
Serial.printf("Connected, IP address: ");
Serial.println(WiFi.localIP());
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
delay(2000);
setupTime();
}
void loop() {
// put your main code here, to run repeatedly:
M5.RTC.getTime(&RTCtime);
M5.RTC.getDate(&RTCDate);
Serial.printf("%d/%02d/%02d (%s) %02d:%02d:%02d\n",
RTCDate.year, RTCDate.mon, RTCDate.day,wd[RTCDate.week],
RTCtime.hour, RTCtime.min, RTCtime.sec);
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment