local serverip = "112.125.89.8" local serverport = 43531 local UDPport = 37834 local…
- User
local server_ip = "112.125.89.8" local server_port = 43531 local UDP_port = 37834 local ssl_port = 35528 local rxbuf = zbuff.create(8192) local function netCB(netc, event, param) if param ~= 0 then sys.publish("socket_disconnect") return end if event == socket.LINK then elseif event == socket.ON_LINE then socket.tx(netc, "hello,luatos!") elseif event == socket.EVENT then socket.rx(netc, rxbuf) socket.wait(netc) if rxbuf:used() > 0 then log.info("收到", rxbuf:toStr(0,rxbuf:used()):toHex()) log.info("发送", rxbuf:used(), "bytes") socket.tx(netc, rxbuf) end rxbuf:del() elseif event == socket.TX_OK then socket.wait(netc) log.info("发送完成") elseif event == socket.CLOSE then sys.publish("socket_disconnect") end end
local function socketTask() local netc = socket.create(nil, netCB) socket.debug(netc, true) socket.config(netc, nil, nil, nil, 300, 5, 6) --开启TCP保活,防止长时间无数据交互被运营商断线 while true do local succ, result = socket.connect(netc, server_ip, server_port) if not succ then log.info("未知错误,5秒后重连") else local result, msg = sys.waitUntil("socket_disconnect") end log.info("服务器断开了,5秒后重连") socket.close(netc) log.info(rtos.meminfo("sys")) sys.wait(5000) end end
local function UDPTask() local netc = socket.create(nil, netCB) socket.debug(netc, true) socket.config(netc, nil, true, nil, 300, 5, 6) --开启TCP保活,防止长时间无数据交互被运营商断线 while true do local succ, result = socket.connect(netc, server_ip, UDP_port) if not succ then log.info("未知错误,5秒后重连") else local result, msg = sys.waitUntil("socket_disconnect") end log.info("服务器断开了,5秒后重连") socket.close(netc) log.info(rtos.meminfo("sys")) sys.wait(5000) end end
local function SSLTask() local netc = socket.create(nil, netCB) socket.debug(netc, true) socket.config(netc, nil, nil, true, 300, 5, 6) --开启TCP保活,防止长时间无数据交互被运营商断线 while true do local succ, result = socket.connect(netc, server_ip, ssl_port) if not succ then log.info("未知错误,5秒后重连") else local result, msg = sys.waitUntil("socket_disconnect") end log.info("服务器断开了,5秒后重连") socket.close(netc) log.info(rtos.meminfo("sys")) sys.wait(5000) end end
function socketDemo() mobile.rtime(1) sys.taskInit(socketTask) end
function UDPDemo() mobile.rtime(1) sys.taskInit(UDPTask) end
function SSLDemo() mobile.rtime(1) sys.taskInit(SSLTask) end 在此代码中加入一个可以连续输出温度的代码
- Assistant
你可以使用以下的代码来连续输出温度:
local function printTemperature() while true do local temperature = adc.read(0) log.info("Temperature:", temperature) sys.wait(5000) --每5秒输出一次温度 end end function socketDemoWithTemperature() mobile.rtime(1) sys.taskInit(socketTask) sys.taskInit(printTemperature) end function UDPDemoWithTemperature() mobile.rtime(1) sys.taskInit(UDPTask) sys.taskInit(printTemperature) end function SSLDemoWithTemperature() mobile.rtime(1) sys.taskInit(SSLTask) sys.taskInit(printTemperature) end在
socketDemoWithTemperature、UDPDemoWithTemperature和SSLDemoWithTemperature函数中,我们通过添加sys.taskInit(printTemperature)来创建一个新的任务,用于连续输出温度。这样,在与服务器建立连接并进行数据交互的同时,温度也会持续输出。