1. 程式人生 > >ESP8266 micropython 胡亂折騰的NTP自同步時鐘程式

ESP8266 micropython 胡亂折騰的NTP自同步時鐘程式

程式碼確實短,NTP之類的都已經預設封裝了 自己編譯ROM的話就可以修改NTP伺服器。。。。

湊合著看看吧

問題,ESP8266在聯網的時候是隻專心聯網的,這種出秒的時鐘 聯網的那瞬間會卡住不列印。。。。

 

 

import ntptime
import utime
#import time
import machine
IF_NTP=0
def ntpsettime(IF_NTP):
    try:
      ntptime.time()
      ntptime.settime()
      rtc=machine.RTC()
      tampon1=utime.time() 
      tampon2=tampon1+8*60*60
      rtc.datetime ( utime.localtime(tampon2)[0:3] + (0,) + utime.localtime(tampon2)[3:6] + (0,))
      IF_NTP=1
    except:
       print('XXX')
       IF_NTP=0
    return IF_NTP
#time.localtime()
#開始迴圈
utime.sleep(3)
IF_NTP=ntpsettime(IF_NTP)
count=0
while 1 :
  (year, month, mday, hour, minute, second, weekday, yearday)=utime.localtime()
  print (year,'-','%02d' % month, '-','%02d'% mday, ' ', '%2d'% hour, ':', '%02d'% minute, ':','%02d'% second, '  Week:',weekday+1, sep = '')
  count=count+1
  if ((count%(60*30)==0) or (count%10==0 and IF_NTP==0)):
      IF_NTP=ntpsettime(IF_NTP)
      count=0
  utime.sleep(1)