Lua Socket communication transmission instruction (Air202) to the relay

I am here with a Air202 the 2G module, the use of socket communication to send commands to the GPIO, relay control to the external buzzer:
before seen a lot of examples online, this time I put the Socket Communications and GPIO control relay brought together , two .lua creates the following files, the first main.lua, which stored some packages need to import, import your own if necessary to use the following code:

PROJECT = "SOCKET-TEST"
VERSION = "1.0.0"


require "log"
LOG_LEVEL = log.LOGLEVEL_TRACE
require "sys"
--1分钟查询一次GSM信号强度,1分钟查询一次基站信息
require "net"
net.startQueryAll(60000, 60000)
--加载硬件看门狗功能模块
require "wdt"
wdt.setup(pio.P0_30, pio.P0_31)
--加载网络指示灯功能模块
require "netLed"
netLed.setup(true, moduleType == 2 and pio.P1_1 or pio.P2_0, moduleType == 2 and nil or pio.P2_1)
require "socketGpio"
require "ntp"


require "errDump"
errDump.request("udp://ota.airm2m.com:9072")
--require "ledtest"    --led
ntp.timeSync()
--启动系统框架
sys.init(0, 0)
sys.run()

The second file is socketGpio.lua, method and stored inside the processing operation, as follows:

module(..., package.seeall)


require "pins" --用到了pin库,该库为luatask专用库,需要进行引用
-- if moduleType == 2 then
--     pmd.ldoset(5, pmd.LDO_VMMC)--使用某些GPIO时,必须在脚本中写代码打开GPIO所属的电压域,配置电压输出输入等级,这些GPIO才能正常工作
-- end

--设置继电器的GPIO口
local gpio1 = pins.setup(7, 0,pio.PULLDOWN)


require "socket"
--测试用的服务器信息
local testip, testport = "180.97.81.180", "54580"
--数据发送的消息队列
local msgQuene = {}
local function insertMsg(data)
    table.insert(msgQuene, data)
end
function waitForSend()
    return #msgQuene > 0
end
function outMsgprocess(socketClient)
    --队列中有消息
    while #msgQuene > 0 do
        --获取消息,并从队列中删除
        local outMsg = table.remove(msgQuene, 1)
        --发送这条消息,并获取发送结果
        local result = socketClient:send(outMsg)
        --发送失败的话立刻返回nil(等同于false)
        if not result then return end
    end
    return true
end



function inMsgProcess(socketClient)
    local result, data
    while true do
        result, data = socketClient:recv(2000)
        --接收到数据
        if result then --接收成功
            log.info("--------------inMsgProcess------------", data)
            --处理data数据
            -- if data:sub(1, 4) == "back" then --收到back开头的数据要回复相同的数据
            --     insertMsg(data)
            -- elseif
            if data == "open" then --打开继电器操作
                insertMsg("Opened")
                gpio1(1)--操作GPIO给高电平信号
                log.info("-----------------gpio is open----------------", data)
            elseif data == "close" then --关闭继电器操作
                insertMsg("Closed")
                gpio1(0)--操作GPIO给低电平信号
                log.info("-----------------gpio is close----------------", data)
            elseif data == "bin" then --收到bin要回复二进制数组0x11 0x22 0x33
                insertMsg(string.fromHex("112233"))
            elseif data == "time" then --收到time要回复当前时间的时间戳字符串
                insertMsg(tostring(os.time()))
            end
            --如果msgQuene中有等待发送的数据,则立即退出本循环
            if waitForSend() then return true end
        else --接收失败
            log.info("-----------------gpio break----------------", data)
            break

        end
    end
    --返回结果,处理成功返回true,处理出错返回false
    return result or data == "timeout"
end
--启动socket客户端任务
sys.taskInit(
    function()
        local retryConnectCnt = 0 --失败次数统计
        while true do
            --是否获取到了分配的ip(是否连上网)
            if socket.isReady() then
                --新建一个socket对象,如果是udp只需要把tcp改成udp即可
                local socketClient = socket.tcp()
                --尝试连接指定服务器
                if socketClient:connect(testip, testport) then
                    --连接成功
                    log.info("socketClient已成功连接", "connect success")
                    retryConnectCnt = 0 --失败次数清零
                    --循环处理接收和发送的数据
                    while true do
                        if not inMsgProcess(socketClient) then --接收消息处理函数
                            log.error("longlink.inMsgProcess error")
                            break
                        end
                        if not outMsgprocess(socketClient) then --发送消息处理函数
                            log.error("longlink.outMsgprocess error")
                            break
                        end
                    end
                else
                    log.info("socketClient连接失败了", "connect fail")
                    --连接失败
                    retryConnectCnt = retryConnectCnt + 1 --失败次数加一
                end
                socketClient:close()--断开socket连接
                if retryConnectCnt >= 5 then --失败次数大于五次了
                    link.shut()--强制断开TCP/UDP连接
                    retryConnectCnt = 0 --失败次数清零
                end
                sys.wait(5000)
            else
                retryConnectCnt = 0 --没连上网,失败次数清零
                --没连上网
                --等待网络环境准备就绪,超时时间是5分钟
                sys.waitUntil("IP_READY_IND", 300000)
                --等完了还没连上?
                if not socket.isReady() then
                    --进入飞行模式,20秒之后,退出飞行模式
                    net.switchFly(true)
                    sys.wait(20000)
                    net.switchFly(false)
                end
            end
        end
    end)
--启动心跳包任务
sys.taskInit(
    function()
        while true do
            if socket.isReady() then --连上网再开始运行
                insertMsg("heart beat")--队列里塞个消息
                sys.wait(10000)--等待10else --没连上网别忘了延时!不然会陷入while true死循环,导致模块无法运行其他代码
                sys.wait(1000)--等待1秒
            end
        end
    end)

PS: the code is tested and guaranteed no problem ~ ~ ~ ~

Air202 the 2G module
This is the 2G module board Air202
attached below wiring diagram, using the line connected to TTL usb

Here Insert Picture Description
The above is Air202 wiring diagram for connecting HOST_TX and then HOST_RX LuaTools of
the FIG. 2 is available from the relay wiring and wiring buzzer
Here Insert Picture Description
written before opening LuaTools software, burn after two good preparation lua lib files and packages
Here Insert Picture Description
will Air202 board lib package is loaded into it, I side of the path is E: \ LuaTools 1.6.4 \ LuaTools 1.6.4 \ script \ script_LuaTask_V2.3.5 \ lib, this is the package lib path Air202 the latest version the V2.3.5 can be downloaded from the internet

Here Insert Picture Description
Everything clicking the download button, burn the board, after the completion of exit after burning is ready, if there are socket communications address to write their own with their own writing, if you do not use the following URL test:
Here Insert Picture Description
Here Insert Picture Description
the right server has returned a heartbeat, left LuaTools is also connected to the transmission relay open command to open the buzzer sounds, the buzzer sending close command to close the relay closed.
Video connector attached below:

Luat remote control buzzer (socket communications)

Published an original article · won praise 1 · views 48

Guess you like

Origin blog.csdn.net/qq_16458561/article/details/105380963