lua mqtt透传部分

module(...,package.seeall)

require"utils"
require"pm"
require"sys"
require"mqtt"
require"mqttOutMsg"
require"mqttInMsg"
require "misc"
local UART_ID = 1
local mqttc22

local torigin =
{
    SERVERIP = "182.92.223.22",
    PORT = "1883",
    DEVICEID = "7735d490-4231-11ec-8470-1bf471b2fd10",
    TOKEN = "pWIUd6QEfKl0YUqIiDFj",
}
local function writevalw(filename,value)
    local filehandle = io.open(filename,"w")
    if filehandle then
        filehandle:write(value)
        filehandle:close()
    else
        print("文件不存在或文件输入格式不正确")
    end
end
local function deletefile(filename)
    os.remove(filename)
end
function split(input, delimiter,index)
    input = tostring(input)
    local pos = 0
    local i = 0
    for st,sp in function() return string.find(input, delimiter, pos, true) end do
        if (i==index) then return string.sub(input, pos, st - 1) end
        i= i + 1
        pos = sp + 1
    end
    if i==index then return string.sub(input, pos) end
    return nil
end
local function save()
    local jsondata = json.encode(torigin)
    writevalw("/user_config.txt",jsondata)
    log.info("save", "ok")
end
local function read()
    local USER_CONFIG=io.readFile("/user_config.txt")
    local cfgdata,result,errinfo = json.decode(USER_CONFIG)
    torigin=cfgdata
    log.info("save", "ok")
end
local function proc(data)
    if not data or string.len(data) == 0 then return end
    log.info("testUart.read",data)
    local a=split(data, " ",0)
    local b=split(data, " ",1)
    local c=split(data, " ",2)
    if(a=="set")
    then
        if(b=="serverip")then torigin["SERVERIP"]=c end
        if(b=="deviceid")then torigin["DEVICEID"]=c end
        if(b=="token")then torigin["TOKEN"]=c end
        uart_write(data)
    end
    if(a=="config")
    then
        if(b=="save") then save()  uart_write("save ok") end
        if(b=="read") then read()  uart_write("read  ok") end 
    end
    if(a=="print")
    then
        local jsondata = json.encode(torigin)
        log.info("jsondata", jsondata)
    end
    log.info(a,b,c)
    
end


local function read()
    local data = ""
    while true do        
        data = uart.read(UART_ID,"*l")
        if not data or string.len(data) == 0 then break end
        proc(data)
    end
end

function uart_write(s)
    log.info("testUart.write",s)
    uart.write(UART_ID,s.."\r\n")
end

local function writeOk()
    log.info("testUart.writeOk")
end


read()
pm.wake("testUart")

local port_mode=0-- 这里改成实际读取的pin脚值
if(port_mode==0) 
then
    uart.on(UART_ID,"receive",read)
    uart.on(UART_ID,"sent",writeOk)
    uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)
end
local revice_data=nil
local function read2()
    local data = ""
    while true do        
        data = uart.read(UART_ID,"*l")
        if not data or string.len(data) == 0 then break end
        log.info("tag", data)
        revice_data=data
    end
end


if(port_mode==1)
then
sys.taskInit(function()
    while not socket.isReady() do sys.wait(1000) log.info(".") end
    log.info("连接上网络")
    sys.wait(2000)
    mqttc2 = mqtt.client(misc.getImei(),300,torigin["TOKEN"])
    log.info("连接上服务器ing")
    while not mqttc2:connect(torigin['SERVERIP'], 1883) do sys.wait(2000) end
    log.info("连接上服务器")
    if mqttc2:subscribe("v1/devices/me/rpc/request/+") then log.info("订阅成功") end
    uart.on(UART_ID,"receive",read2)
    uart.on(UART_ID,"sent",writeOk)
    uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)
    while true do
        result,data = mqttc2:receive(500,"APP_SOCKET_SEND_DATA")
        if result then
            log.info("mqttInMsg.topic",data.topic)
            log.info("mqttInMsg.payload",data.payload)
            uart_write(data.payload)
        end
        if(revice_data)then
            mqttc2:publish("v1/devices/me/telemetry",revice_data)
            revice_data=nil
        end
    end
    mqttc2:disconnect()
end)
end

标签: none

添加新评论