Last active
June 19, 2021 15:07
-
-
Save Shinichi-Ohki/d210293f1de322a4581069baab34ef79 to your computer and use it in GitHub Desktop.
M5Paper RTC set from NTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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