Newer
Older
d4 / ntp.ino
@21a5069 21a5069 on 25 Apr 2023 1 KB add ntp
void ntpsetup() {
    char* ntpserver = "ntp.nict.jp"; // 学内のNetworkを利用するときは、10.64.7.184 にする
    //char* ntpserver = "192.168.11.11"; // 学内のNetworkを利用するときは、10.64.7.184 にする
    configTime(9 * 3600, 0, ntpserver);//GMTとの時差(秒) が9*3600, サマータイムで進める時間(秒)が0

    struct tm localTime;
    getLocalTime(&localTime);
    int ntpy=localTime.tm_year + 1900;
    int ntpmon=localTime.tm_mon + 1;
    int ntpday=localTime.tm_mday;
    int ntphour=localTime.tm_hour;
    int ntpmin=localTime.tm_min;
    int ntpsec=localTime.tm_sec;
    nowy=ntpy;
    nowmon=ntpmon;
    nowd=ntpday;
    nows=ntpsec;
    nowh=ntphour;
    nowm=ntpmin;
    sprintf(timenow, "%04d/%02d/%02d\n  %02d:%02d:%02d",
    //sprintf(timenow, "%04d/%02d/%02d  %02d:%02d",
        ntpy,
        ntpmon,
        ntpday,
        ntphour,
        ntpmin
        ,ntpsec
    );
    //Serial.println(timenow);
}

void ntpget(){
    //time change
    if(systime-millis()/1000>=1){
        nows++;
    }
    if(nows>=60){
        nows=0;
        nowm++;
        if(nowm>=60){
        nowh++;
        nowm=0;
        }
        if(nowh>=24){
        ntpsetup();
        systime=millis()/1000;
        }
    }
    sprintf(timenow, "%04d/%02d/%02d\n  %02d:%02d:%02d",
    //sprintf(timenow, "%04d/%02d/%02d  %02d:%02d",
        nowy,
        nowmon,
        nowd,
        nowh,
        nowm
        ,nows
    );

    //For weather api
    if(nowm!=setm){
      reg=0;
    }
    //reget ntp
    if(millis()/1000-systime>=ntptime*60){
      ntpsetup();
      systime=millis()/1000;
    }
}