ESP8266 Node MCU获得NTP时间并自行串口输出的LUA语言代码

把ssid和pwd改成自己的,波特率15200。

wifi.setmode(wifi.STATION)
wifi.sta.config{ssid="Tenda", pwd="13365632289"}
wifi.sta.autoconnect(1)

tmr.alarm(0, 1000, 1, function()
     if wifi.sta.getip() == nil then
         print("Connecting...")
     else
         tmr.stop(0)
         print("Connected, IP is "..wifi.sta.getip())
         sntp.sync({"194.94.224.118", "ptbtime1.ptb.de","ntp.shu.edu.cn","ptbtime1.ptb.de","cn.pool.net.org","ntp.shu.edu.cn","ntp.sjtu.edu.cn","202.112.10.36","202.112.29.82"},
          function(sec, usec, server, info)
          print('sync', sec, usec, server)
          print('@')
          end,
                function()
                print('failed!')
                end)
     end
end)
                                                
tmr.alarm(1, 1000, 1, function()
    secs = rtctime.get()
    if secs >10 then
    secs = secs + (8*3600)  
    time = rtctime.epoch2cal(secs)   
   print(string.format("%04d/%02d/%02d %02d:%02d:%02d", 
                        time["year"], 
                        time["mon"], 
                        time["day"], 
                        time["hour"], 
                        time["min"], 
                        time["sec"]))    
   else 
    -- print(secs)
   end                
end)

tmr.alarm(2,60*60*1000, 1,function()
                             again()
                                    end)

function again()
  sntp.sync({"194.94.224.118", "ptbtime1.ptb.de","ntp.shu.edu.cn","ptbtime1.ptb.de","cn.pool.net.org","ntp.shu.edu.cn","ntp.sjtu.edu.cn","202.112.10.36","202.112.29.82"}, 
                   function(_, _, server, _)
                --      print("aganin sync succeeded  " .. server)
                    end,
                  function(index)
                --   -     print("failed : "..index)
                  end)
end 

猜你喜欢

转载自blog.csdn.net/weixin_43829236/article/details/85001793