【ESP8266之LUA开发】十、MQTT建立客户端连接服务器,实现远程控制继电器

客户端之间进行透传的实例

ESP8266建立的MQTT客户端收到订阅主题广播的消息后,回显到串口。
同时1s向发布的主题发送“1111”

init.lua

configwifissid = "qqqqq";
configwifipwd="11223344";
MqttUserString = "yang";
MqttPwdString = "11223344";
MqttIPString = "47.93.19.134";
MqttPort = 1883;


clientid = wifi.sta.getmac()
SubscribeTopic = "/pub"
PublishTopic = "/sub"

wifi.setmode(wifi.STATIONAP)
apcfg={}
apcfg.ssid=configwifissid
apcfg.pwd=configwifipwd
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

mqttConnectedFlage = 0;

client = mqtt.Client(clientid, 120, MqttUserString,MqttPwdString)


tmr.alarm(3, 1000, 1, function()
    client:connect(MqttIPString, MqttPort,ConnectSuccess,ConnectFailed)
    print("connect......");
end)

function ConnectSuccess(client)
     tmr.stop(3);
     client:subscribe(SubscribeTopic, 0, subscribeSuccess)
     print("connected")
     --mqttClient = client;
     mqttConnectedFlage = 1;
end


function ConnectFailed(client,reason)
   mqttConnectedFlage = 0;
   tmr.start(3)
   print("failed reason: " .. reason)
end

function subscribeSuccess(client)
  print("subscribeSuccess")
end


client:on("message", function(client, topic, message)
  print(message);
end)


tmr.alarm(2, 1000, 1, function()
    if mqttConnectedFlage == 1 then
       client:publish(PublishTopic, "1111", 0,0)
    end
end)



MQTT建立客户端,发送Relay=1或者Relay=0实现远程控制继电器!

init.lua

--[[
GPIO0--3
GPIO1--10
GPIO2--4
GPIO3--9
GPIO4--2
GPIO5--1
GPIO9--11
GPIO10--12
GPIO12--6
GPIO13--7
GPIO14--5
GPIO15--8
GPIO16--0
0--GPIO16   1--GPIO5   2--GPIO4   3--GPIO0   4--GPIO2
5--GPIO14   6--GPIO12  7--GPIO13  8--GPIO15  9--GPIO3
10--GPIO1   11--GPIO9  12--GPIO10
]]

configwifissid = "qqqqq";
configwifipwd="11223344";
MqttUserString = "yang";
MqttPwdString = "11223344";
MqttIPString = "47.93.19.134";
MqttPort = 1883;

clientid = wifi.sta.getmac()
SubscribeTopic = "/pub"
PublishTopic = "/sub"

RelayPin = 2

wifi.setmode(wifi.STATIONAP)
apcfg={}
apcfg.ssid=configwifissid
apcfg.pwd=configwifipwd
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

print("dofile init.lua")

tmr.alarm(0, 3000, 0, function()
   dofile("mqtt.lua");
   print("dofile mqtt.lua")
end)

mqtt.lua

globalSendData ="nil"; 
globalSendData1="nil";
globalSendData2="nil";

UsartReceiveData="";
UsartReceiveDataCnt=0;
UsartReceiveDataCntCopy=0;


if  file.open("relay.lua", "r") then
    if  file.read() == "relay=1" then
        gpio.write(RelayPin,1)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=1")  
    else
        gpio.write(RelayPin,0)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=0")
    end
end
file.close() 


Mymqtt = mqtt.Client(clientid, 120,MqttUserString, MqttPwdString);

tmr.alarm(3, 1000, 1, function()
    Mymqtt:connect(MqttIPString, MqttPort, 0,ConnectSuccess,ConnectFailed)
end)

function ConnectSuccess(client)
     client:subscribe(SubscribeTopic, 0, subscribeSuccess)
     print("connected")
     mqttClient = client;
     tmr.stop(3);
     mqttConnectedFlage = 1;
end
function ConnectFailed(client,reason)
   mqttConnectedFlage = 0;
   print("failed reason: " .. reason)
   tmr.start(3)
end
function subscribeSuccess(client)
    print("subscribe success") 
end


Mymqtt:on("message", function(client, topic, data) 
    uart.write(0,data)
    if data == "relay=0" then
       gpio.write(RelayPin,0)
       gpio.mode(RelayPin,gpio.OUTPUT)
       if  file.open("relay.lua", "w+") then
           file.write("relay=0")
           file.close() 
       end
    elseif data == "relay=1"  then    
       gpio.write(RelayPin,1)
       gpio.mode(RelayPin,gpio.OUTPUT)
       if  file.open("relay.lua", "w+") then
           file.write("relay=1")
           file.close() 
       end
    end
end)


tmr.alarm(4, 10, 1, function()
    if  mqttClient ~= nil and mqttConnectedFlage == 1 then
        RelayNowState = gpio.read(RelayPin)
        if  RelayNowState ~= RelayNowStateCopy then
            RelayNowStateCopy = RelayNowState
            globalSendData1 = "relay="..RelayNowState
        end
    
        if  globalSendData1~="nil" then
            globalSendData=globalSendData1;
            globalSendData1="nil"
        elseif globalSendData2 ~="nil" then
            globalSendData=globalSendData2;
            globalSendData2="nil"
        else
            globalSendData="nil";    
        end
        if  globalSendData ~= "nil" then
            mqttClient:publish(PublishTopic,globalSendData, 0, 0, function(client) 
            end)
        end
    end 
    
    if UsartReceiveDataCnt ~= 0 then
       if UsartReceiveDataCntCopy == UsartReceiveDataCnt then
          UsartReceiveDataCnt=0;
          UsartReceiveDataCntCopy = 0;
          globalSendData2 = UsartReceiveData;
          UsartReceiveData="";
       else
          UsartReceiveDataCntCopy = UsartReceiveDataCnt;
       end
    end
end)


uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1)

uart.on("data",0,function(data) 
    UsartReceiveData = UsartReceiveData..data;
    UsartReceiveDataCnt = UsartReceiveDataCnt + 1;
end, 0)

你可能感兴趣的:(#,ESP8266学习)