Created
April 25, 2019 16:45
-
-
Save Shinichi-Ohki/77b26d0ef50b7e52c6b74076fd5b73c5 to your computer and use it in GitHub Desktop.
Set RTC from NTP for M5StickC on Arduino IDE
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 <M5StickC.h> | |
#include <WiFi.h> | |
#include <time.h> | |
RTC_TimeTypeDef RTC_TimeStruct; | |
RTC_DateTypeDef RTC_DateStruct; | |
static const int JST = 3600 * 9; | |
static const char *wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"}; | |
const char* ssid = "Your-SSID"; | |
const char* password = "Your-Pass"; | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setRotation(3); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setTextSize(1); | |
M5.Lcd.setCursor(40, 0, 2); | |
M5.Lcd.println("RTC TEST"); | |
Serial.begin(115200); | |
delay(100); | |
Serial.print("\n\nStart\n"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(500); | |
} | |
Serial.println(); | |
Serial.printf("Connected, IP address: "); | |
Serial.println(WiFi.localIP()); | |
delay(1000); | |
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); | |
delay(1000); | |
time_t t; | |
struct tm *tm; | |
t = time(NULL); | |
tm = localtime(&t); | |
Serial.printf(" %04d/%02d/%02d(%s) %02d:%02d:%02d\n", | |
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, | |
wd[tm->tm_wday], | |
tm->tm_hour, tm->tm_min, tm->tm_sec); | |
RTC_TimeTypeDef TimeStruct; | |
TimeStruct.Hours = tm->tm_hour; | |
TimeStruct.Minutes = tm->tm_min; | |
TimeStruct.Seconds = tm->tm_sec; | |
M5.Rtc.SetTime(&TimeStruct); | |
RTC_DateTypeDef DateStruct; | |
DateStruct.WeekDay = tm->tm_wday; | |
DateStruct.Month = tm->tm_mon + 1; | |
DateStruct.Date = tm->tm_mday; | |
DateStruct.Year = tm->tm_year + 1900; | |
M5.Rtc.SetData(&DateStruct); | |
} | |
void loop() { | |
M5.Rtc.GetTime(&RTC_TimeStruct); | |
M5.Rtc.GetData(&RTC_DateStruct); | |
M5.Lcd.setCursor(0, 15); | |
M5.Lcd.printf("Data: %04d-%02d-%02d\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date); | |
M5.Lcd.printf("Week: %s\n", wd[RTC_DateStruct.WeekDay]); | |
M5.Lcd.printf("Time: %02d : %02d : %02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment