所有的联网功能代码如下:
update.request(update_callback,nil,nil,true) 每隔5分钟查询一次升级
ntp.timeSync(1) 一小时同步一次时间
下面是用户联网部分的代码:
while true do
while(fly_mode==0) do --进入飞行模式
sys.wait(100)
log.error("fly_mode=",fly_mode)
end
--离开飞行模式
while not socket.isReady() do sys.waitUntil("IP_READY_IND",5000) end
if(socket.isReady()) then
while(socketClient_test~=nil) do
socketClient_test:close()
sys.wait(1000)
end
socketClient_test = socket.tcp()
if(socketClient_test~=nil) then --TCP正常
connectResult =socketClient_test:connect("115.28.155.126", "1883")
if(connectResult) then
log.debug("connect yes")
user_socket_err=1 --允许传数据
socket_led__on()
if(socket_once_flag==0) then socket_send_data(RTU_zhiling_ShuXing) socket_once_flag=1 end
while socketClient_test:asyncSelect() do end
user_socket_err=10 --禁止传数据
socketClient_test:close()
socket_led__off()
sys.wait(1000)
else
socket_led__off()
socket_tcp_count=socket_tcp_count+1
audio.play(5,"TTS","服务器连接失败2",7)
end
if(socket_tcp_count>5) then
log.error("link.shut()")
link.shut()
socket_tcp_count=0
end
else
log.debug("tcp err")
end
else
server_no_response=server_no_response+1
if(server_no_response>2) then --等待3次不能联网(15秒), 开启飞行模式20S
log.error("server_no_response>2")
server_no_response=0
fly_mode=0
audio.play(10,"TTS","进入飞行模式",7)
net.switchFly(true)
sys.wait(20000)
net.switchFly(false)
audio.play(10,"TTS","关闭飞行模式",7)
fly_mode=1
end
log.debug("socket not Ready")
end
sys.wait(1000)
end