Appearance
question:- LuaTools需要PROJECT和VERSION这两个信息 PROJECT = "scdemo" VERSION = "1.0.0" log.info("main", PROJECT, VERSION) -- 一定要添加sys.lua !!!! sys = require("sys") sysplus = require("sysplus") libnet = require "libnet" -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦 if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then pm.power(pm.PWK_MODE, false) end --============================================================= -- 测试网站 https://netlab.luatos.com/ 点击 打开TCP 获取测试端口号 -- 要按实际情况修改 local host = "112.125.89.8" -- 服务器ip或者域名, 都可以的 local port = 43255 -- 服务器端口号 local is_udp = false -- 如果是UDP, 要改成true, false就是TCP local is_tls = false -- 加密与否, 要看服务器的实际情况 --============================================================= -- 处理未识别的网络消息 local function netCB(msg) log.info("未处理消息", msg[1], msg[2], msg[3], msg[4]) end -- 统一联网函数 sys.taskInit(function() ----------------------------- -- 统一联网函数, 可自行删减 ---------------------------- if wlan and wlan.connect then -- wifi 联网, ESP32系列均支持, 要根据实际情况修改ssid和password!! local ssid = "luatos1234" local password = "12341234" log.info("wifi", ssid, password) -- TODO 改成自动配网 wlan.init() wlan.setMode(wlan.STATION) -- 默认也是这个模式,不调用也可以 wlan.connect(ssid, password, 1) elseif mobile then -- EC618系列, 如Air780E/Air600E/Air700E -- mobile.simid(2) -- 自动切换SIM卡, 按需启用 -- 模块默认会自动联网, 无需额外的操作 elseif w5500 then -- w5500 以太网 w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00) w5500.config() --默认是DHCP模式 w5500.bind(socket.ETH0) elseif socket then -- 适配了socket库也OK, 就当1秒联网吧 sys.timerStart(sys.publish, 1000, "IP_READY") else -- 其他不认识的bsp, 循环提示一下吧 while 1 do sys.wait(1000) log.info("bsp", "本bsp可能未适配网络层, 请查证") end end -- 默认都等到联网成功 sys.waitUntil("IP_READY") sys.publish("net_ready") end) -- 演示task local function sockettest() -- 等待联网 sys.waitUntil("net_ready") socket.sntp() -- 开始正在的逻辑, 发起socket链接,等待数据/上报心跳 local taskName = "sc" local topic = taskName .. "_txrx" log.info("topic", topic) local txqueue = {} sysplus.taskInitEx(sockettask, taskName, netCB, taskName, txqueue, topic) while 1 do local result, tp, data = sys.waitUntil(topic, 30000) log.info("event", result, tp, data) if not result then -- 等很久了,没数据上传/下发, 发个日期心跳包吧 table.insert(txqueue, string.char(0)) sys_send(taskName, socket.EVENT, 0) elseif tp == "uplink" then -- 上行数据, 主动上报的数据,那就发送呀 table.insert(txqueue, data) sys_send(taskName, socket.EVENT, 0) elseif tp == "downlink" then -- 下行数据,接收的数据, 从ipv6task来的 -- 其他代码可以通过 sys.publish() log.info("socket", "收到下发的数据了", #data) end end end function sockettask(d1Name, txqueue, rxtopic) -- 打印准备连接的服务器信息 log.info("socket", host, port, is_udp and "UDP" or "TCP", is_tls and "TLS" or "RAW") -- 准备好所需要的接收缓冲区 local rx_buff = zbuff.create(1024) local netc = socket.create(nil, d1Name) socket.config(netc, nil, is_udp, is_tls) log.info("任务id", d1Name) while true do -- 连接服务器, 15秒超时 log.info("socket", "开始连接服务器") sysplus.cleanMsg(d1Name) local result = libnet.connect(d1Name, 15000, netc, host, port) if result then log.info("socket", "服务器连上了") libnet.tx(d1Name, 0, netc, "helloworld") else log.info("socket", "服务器没连上了!!!") end while result do -- 连接成功之后, 先尝试接收 -- log.info("socket", "调用rx接收数据") local succ, param = socket.rx(netc, rx_buff) if not succ then log.info("服务器断开了", succ, param, ip, port) break end -- 如果服务器有下发数据, used()就必然大于0, 进行处理 if rx_buff:used() > 0 then log.info("socket", "收到服务器数据,长度", rx_buff:used()) local data = rx_buff:query() -- 获取数据 sys.publish(rxtopic, "downlink", data) rx_buff:del() end -- log.info("libnet", "调用wait开始等待消息") -- 等待事件, 例如: 服务器下发数据, 有数据准备上报, 服务器断开连接 result, param, param2 = libnet.wait(d1Name, 15000, netc) log.info("libnet", "wait", result, param, param2) if not result then -- 网络异常了, 那就断开了, 执行清理工作 log.info("socket", "服务器断开了", result, param) break elseif #txqueue > 0 then -- 有待上报的数据,处理之 while #txqueue > 0 do local data = table.remove(txqueue, 1) if not data then break end result,param = libnet.tx(d1Name, 15000, netc,data) log.info("libnet", "发送数据的结果", result, param) if not result then log.info("socket", "数据发送异常", result, param) break end end end -- 循环尾部, 继续下一轮循环 end -- 能到这里, 要么服务器断开连接, 要么上报(tx)失败, 或者是主动退出 libnet.close(d1Name, 5000, netc) -- log.info(rtos.meminfo("sys")) sys.wait(30000) -- 这是重连时长, 自行调整 end end sys.taskInit(sockettest) -- 演示定时上报数据, 不需要就注释掉 sys.taskInit(function() sys.wait(5000) while 1 do sys.publish("sc_txrx", "uplink", os.date()) sys.wait(3000) end end) -- 演示uart数据上报, 不需要就注释掉 if rtos.bsp() == "EC618" then uart.setup(1, 115200) -- 注意, 是UART1, 不是虚拟串口, 演示目的 uart.on(1, "recvice", function(id, len) while 1 do local s = uart.read(1, 1024) if #s == 0 then break end sys.publish("sc_txrx", "uplink", s) end end) end -- 用户代码已结束--------------------------------------------- -- 结尾总是这一句 sys.run() -- sys.run()之后后面不要加任何语句!!!!! 在以上代码中添加一个协议,用于向客户端输出温度,水位,等
answer:可以通过创建一个新的协议函数来向客户端输出温度、水位等信息。以下是一个示例代码: ```lua -- 自定义协议回调函数 local function customProtocol(topic, data) -- 温度传感器数据 local temperature = 25.5 -- 水位传感器数据 local waterLevel = 50 -- 将温度和水位数据转换为字符串 local payload = string.format("Temperature: %.2f, Water Level: %d", temperature, waterLevel) -- 发送数据到客户端 if topic == "get_temperature" then log.info("customProtocol", "Get temperature request received.") libnet.tx("sc", 0, topic, payload) elseif topic == "get_water_level" then log.info("customProtocol", "Get water level request received.") libnet.tx("sc", 0, topic, payload) else log.info("customProtocol", "Unknown request received.") end end -- 注册自定义协议回调函数 sys.subscribe("sc_txrx", customProtocol) ``` 在以上代码中,创建了一个customProtocol函数来处理客户端发送的请求。根据不同的请求类型,生成温度和水位数据的字符串,并使用libnet.tx函数发送回客户端。然后通过sys.subscribe函数订阅"sc_txrx"主题,将customProtocol函数与该主题关联起来。
question:– 自定义协议回调函数 local function customProtocol(topic, data) – 温度传感器数据 local temperature = 25.5 – 水位传感器数据 local waterLevel = 50 – 将温度和水位数据转换为字符串 local payload = string.format(“Temperature: %.2f, Water Level: %d”, temperature, waterLevel) – 发送数据到客户端 if topic == “get_temperature” then log.info(“customProtocol”, “Get temperature request received.”) libnet.tx(“sc”, 0, topic, payload) elseif topic == “get_water_level” then log.info(“customProtocol”, “Get water level request received.”) libnet.tx(“sc”, 0, topic, payload) else log.info(“customProtocol”, “Unknown request received.”) end end – 注册自定义协议回调函数 sys.subscribe(“sc_txrx”, customProtocol) 这段代码应该添加在哪里
answer:这段代码应该添加在原始代码中的用户代码部分的末尾。具体如下所示: ```lua ... sys.taskInit(sockettest) -- 注册自定义协议回调函数 local function customProtocol(topic, data) -- 温度传感器数据 local temperature = 25.5 -- 水位传感器数据 local waterLevel = 50 -- 将温度和水位数据转换为字符串 local payload = string.format("Temperature: %.2f, Water Level: %d", temperature, waterLevel) -- 发送数据到客户端 if topic == "get_temperature" then log.info("customProtocol", "Get temperature request received.") libnet.tx("sc", 0, topic, payload) elseif topic == "get_water_level" then log.info("customProtocol", "Get water level request received.") libnet.tx("sc", 0, topic, payload) else log.info("customProtocol", "Unknown request received.") end end sys.subscribe("sc_txrx", customProtocol) -- 结尾总是这一句 sys.run() ``` 在以上代码中,先定义了customProtocol函数来处理客户端发送的请求,然后使用sys.subscribe函数将customProtocol函数与"sc_txrx"主题相关联。这样在收到"sc_txrx"主题的消息时,会自动调用customProtocol函数来处理客户端的请求。
question:在以上代码中,写入一个用于向服务端输出温度,水位,等
answer:要向服务器输出温度、水位等信息,可以在sockettest函数中添加相应的逻辑来实现。以下是示例代码: ```lua -- 演示task local function sockettest() -- 等待联网 sys.waitUntil("net_ready") socket.sntp() -- 开始正在的逻辑, 发起socket链接,等待数据/上报心跳 local taskName = "sc" local topic = taskName .. "_txrx" log.info("topic", topic) local txqueue = {} sysplus.taskInitEx(sockettask, taskName, netCB, taskName, txqueue, topic) while 1 do local result, tp, data = sys.waitUntil(topic, 30000) log.info("event", result, tp, data) if not result then -- 等很久了,没数据上传/下发, 发个日期心跳包吧 table.insert(txqueue, string.char(0)) sys_send(taskName, socket.EVENT, 0) elseif tp == "uplink" then -- 上行数据, 主动上报的数据,那就发送呀 table.insert(txqueue, data) sys_send(taskName, socket.EVENT, 0) elseif tp == "downlink" then -- 下行数据,接收的数据, 从ipv6task来的 -- 其他代码可以通过 sys.publish() log.info("socket", "收到下发的数据了", #data) end end end -- 温度传感器模拟数据 local function getTemperature() return math.random(10, 40) -- 随机生成10-40之间的整数 end -- 水位传感器模拟数据 local function getWaterLevel() return math.random(0, 100) -- 随机生成0-100之间的整数 end sys.taskInit(sockettest) -- 演示定时上报数据 sys.taskInit(function() sys.wait(5000) while 1 do -- 获取温度和水位数据 local temperature = getTemperature() local waterLevel = getWaterLevel() -- 构建数据包 local data = string.format("Temperature: %d, Water Level: %d", temperature, waterLevel) -- 发送数据到服务器 sys.publish("sc_txrx", "uplink", data) sys.wait(3000) end end) -- 结尾总是这一句 sys.run() ``` 在以上代码中,首先定义了getTemperature和getWaterLevel函数来模拟温度传感器和水位传感器的数据获取。然后在定时上报数据的部分,通过获取温度和水位数据,并构建数据包,然后使用sys.publish函数将数据发送到服务器。这样就可以实现向服务器输出温度、水位等信息。
question:在以上代码中,写入一个协议用于向服务端输出温度,水位,等
answer:要向服务端输出温度、水位等信息,可以在`sockettest`函数中添加相应的逻辑来实现。以下是示例代码: ```lua -- 演示task local function sockettest() -- 等待联网 sys.waitUntil("net_ready") socket.sntp() -- 开始正在的逻辑, 发起socket链接,等待数据/上报心跳 local taskName = "sc" local topic = taskName .. "_txrx" log.info("topic", topic) local txqueue = {} sys.taskInitEx(sockettask, taskName, netCB, taskName, txqueue, topic) while 1 do local result, tp, data = sys.waitUntil(topic, 30000) log.info("event", result, tp, data) if not result then -- 等很久了,没数据上传/下发, 发个日期心跳包吧 table.insert(txqueue, string.char(0)) sys_send(taskName, socket.EVENT, 0) elseif tp == "uplink" then -- 上行数据, 主动上报