本帖最后由 沙漠之痕 于 2018-11-7 13:10 编辑
一 . 在本地电脑上安装emqtt服务器:
1.首先安装补丁:把补丁拷贝到" C:\Windows\System32 "文件夹下
2. 控制台模式下启动:emqttd console
(1) 控制台下切换到emqtt的bin目录下。如:
快速切换方法:在此目录下按住“Shift”同时点击鼠标右键选择“在此处打开 命令窗口”
(2)启动mqtt服务器 “emqttd console”
3. 通过网页登陆查看链接信息 用户名:admin 密码:public
查看本机ip:
链接上一个客户端 我们再启动一个客户端让两个客户端通信 我们在查看下链接的客户端 客户端通信
此种方法会打开一个窗口,关掉窗口emqtt服务也关闭; 让emqtt服务器一直运行的 (1)控制台先定位到emqtt的bin目录下“D:\MqttServer\emqttd\bin” (2)emqttd install (3)启动emqttd服务:emqttd start (4)停止emqttd服务:emqttd stop (5)卸载emqttd服务:emqttd uninstall
二. ESP8266 模块连接emqtt服务器进行远程控制
init.lua文件: - RELAY_Pin = 1
- gpio.mode(RELAY_Pin, gpio.OUTPUT)
- gpio.write(RELAY_Pin,0)
- tmr.alarm(0,4000,0,function()
- dofile("mqtt.lua")
- end)
复制代码
init.lua文件结束 mqtt.lua文件: - MqttServerIP="192.168.0.105"
- MqttServerPort=1883
- MqttUserName="admin"
- MqttUserPassword="public"
- MqttLinkStat = 0
- UartReceData = ""
- UartReceCnt = 0
- UartReceTempCnt = 0
- wifi.setmode(wifi.STATION)
- local stacfg={
- ssid="qqqqq",
- pwd="11223344"
- }
- wifi.sta.config(stacfg)
- wifi.sta.autoconnect(1)
- Clientid = wifi.sta.getmac()
- ClientSubscribeTopic = "AAA"
- ClientPublishTopic = "111"
- MqttClient=mqtt.Client(Clientid,3,MqttUserName,MqttUserPassword)
- MqttClient:lwt(ClientPublishTopic,"offline",1,1)
- local function ConnectSuccess(client)
- print("MqttServer connect sucess")
- client:subscribe(ClientSubscribeTopic,0,function(client)print("subscribe sucess")end)
- MqttLinkStat = 1
- tmr.stop(3)
- end
- local function ConnectFailed(client, reason)
- print("MqttServer connect fail"..reason)
- MqttLinkStat = 0
- tmr.start(3)
-
- end
- tmr.alarm(3, 2000, 1, function()
- MqttClient:connect(MqttServerIP, MqttServerPort, 0,ConnectSuccess,ConnectFailed)
- end)
- MqttClient:on("offline", function(client, topic, data) tmr.start(3) end)
- MqttClient:on("message", function(client, topic, data)
- if data == "open" then
- gpio.write(RELAY_Pin,1)
- if gpio.read(RELAY_Pin)==1 then
- if MqttClient ~= nil then
- MqttClient:publish(ClientPublishTopic,"ON-RELAY",1,1,function(client) end)
- end
- end
- end
- if data == "close" then
- gpio.write(RELAY_Pin,0)
- if gpio.read(RELAY_Pin)==0 then
- if MqttClient ~= nil then
- MqttClient:publish(ClientPublishTopic,"OFF-RELAY",1,1,function(client) end)
- end
- end
- End
- uart.write(0,data)
- end)
- tmr.alarm(2,10,1,function()
- if UartReceCnt ~= 0 then
- if UartReceTempCnt == UartReceCnt then
- UartReceTempCnt = 0
- UartReceCnt = 0
- --send to-Mqtt
- if MqttClient ~= nil then
- MqttClient:publish(ClientPublishTopic,UartReceData,1,1,function(client) end)
- end
- else
- UartReceTempCnt = UartReceCnt
- end
- end
- end)
- uart.on("data",0,function(data)
- UartReceData = UartReceData..data
- UartReceCnt = UartReceCnt + 1
- end,0)
- printip = 0
- wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
- printip = 0
- end)
- wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
- if printip == 0 then
- print("+IP "..T.IP)
- end
- printip = 1
- end)
复制代码mqtt.lua文件结束
模块通过串口发送数据
PC客户端发送”open”开继电器
PC客户端发送”close”关继电器
mqtt客户端.zip
(118.4 KB, 下载次数: 34)
|