网络对时

网络对时的详细介绍

创作者:liutao12345678 | 更新日期:2022-09-13 | 在线时长:128天
Esp8266自动对时

--use sjson--_G.cjson = sjson--modify DEVICEID INPUTID APIKEYDEVICEID = "19929"APIKEY = "4b6663196"INPUTID = "36"host = host or "www.bigiot.net"port = port or 8181LED = 2isConnect = falsegpio.mode(LED,gpio.OUTPUT)local function run() local cu = net.createConnection(net.TCP) cu:on("receive", function(cu, c) print(c) isConnect = true r = cjson.decode(c) if r.M == "say" then if r.C == "play" then gpio.write(LED, gpio.HIGH) tmr.alarm(0, 5000, 1, function() ok, played = pcall(cjson.encode, {M="time",F="Y-m-d H:i:S"}) cu:send( played.."\n" ) end ) end end end) cu:on('disconnection',function(scu) cu = nil isConnect = false --停止心跳包发送定时器,5秒后重试 tmr.stop(1) tmr.alarm(6, 5000, 0, run) end) cu:connect(port, host) ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY}) if ok then print(s) else print("failed to encode!") end if isConnect then cu:send(s.."\n") end tmr.alarm(1, 60000, 1, function() if isConnect then cu:send(s.."\n") end end)endrun()