2017-04-13 87 views
0

我是一位C#.net Xamarin开发人员,他现在坚持将一个MQTT客户端移植到一个ESP8266MOD wifi芯片上,因为那个应该这样做的人没有。NodeMCU ESP8266MOD Esplorer MQTT简单MQTT foobarflies

无论如何不知道什么我已经闪过一个自定义NodeMCU构建从文件,gpio,http,i2c,mqtt,网络,节点,tmr,uart,wifi的战斗。我正在关注simple MQTT project by foorbarflies

我已经上传了下列文件到刚闪过芯片:

-- file : config.lua 
local module = {} 

module.SSID = {} 
module.SSID["xxxxx xxxxxxx"] = "xxxxxxxxxxxxxxxxx" 

module.HOST = "mqtt.xxxxxxxxxxx.com" 
module.PORT = 1883 
module.ID = node.chipid() 

module.ENDPOINT = "nodemcu/" 
return module 

-- file: setup.lua 
local module = {} 

local function wifi_wait_ip() 
    if wifi.sta.getip()== nil then 
    print("IP unavailable, Waiting...") 
    else 
    tmr.stop(1) 
    print("\n====================================") 
    print("ESP8266 mode is: " .. wifi.getmode()) 
    print("MAC address is: " .. wifi.ap.getmac()) 
    print("IP is "..wifi.sta.getip()) 
    print("====================================") 
    app.start() 
    end 
end 

local function wifi_start(list_aps) 
    if list_aps then 
     for key,value in pairs(list_aps) do 
      if config.SSID and config.SSID[key] then 
       wifi.setmode(wifi.STATION); 
       wifi.sta.config(key,config.SSID[key]) 
       wifi.sta.connect() 
       print("Connecting to " .. key .. " ...") 
       --config.SSID = nil -- can save memory 
       tmr.alarm(1, 2500, 1, wifi_wait_ip) 
      end 
     end 
    else 
     print("Error getting AP list") 
    end 
end 

function module.start() 
    print("Configuring Wifi ...") 
    wifi.setmode(wifi.STATION); 
    wifi.sta.getap(wifi_start) 
end 

return module 


-- file : application.lua 
local module = {} 
m = nil 

-- Sends a simple ping to the broker 
local function send_ping() 
    m:publish(config.ENDPOINT .. "ping","id=" .. config.ID,0,0) 
end 

-- Sends my id to the broker for registration 
local function register_myself() 
    m:subscribe(config.ENDPOINT .. config.ID,0,function(conn) 
     print("Successfully subscribed to data endpoint") 
    end) 
end 

local function mqtt_start() 
    m = mqtt.Client(config.ID, 120) 
    -- register message callback beforehand 
    m:on("message", function(conn, topic, data) 
     if data ~= nil then 
     print(topic .. ": " .. data) 
     -- do something, we have received a message 
     end 
    end) 
    -- Connect to broker 
    m:connect(config.HOST, config.PORT, 0, 1, function(con) 
     register_myself() 
     -- And then pings each 1000 milliseconds 
     tmr.stop(6) 
     tmr.alarm(6, 1000, 1, send_ping) 
    end) 

end 

function module.start() 
    mqtt_start() 
end 

return module 



-- file : test.lua 
app = require("application") 
config = require("config") 
setup = require("setup") 

setup.start() 

我发送的命令dofile("test.lua");

和我.......

esplorer grab

看来我应该看到一些来自application.lua的字符串,如“ping”或“成功订阅”,但我什么也没有。这就像application.lua没有运行。

任何帮助,将不胜感激。提前致谢。

- 马克

更新

我的连接对象之前直接添加一个字符串,并将其印刷成似乎连接对象现在对工作被锁定了。

+0

给那些投票关闭ESP8266/NodeMCU问题的用户的一个注意事项:仅仅因为你不明白这些问题源自的技术并不意味着它们是无关紧要的。物联网是真实的,NodeMCU&Lua非常符合话题部分。 –

回答

0

为了理解它的实际作用,通过这么多的代码真的很难。在这里,我们更喜欢minimal, complete, and verifiable examples

看来你明白你复制代码背后的逻辑了吧?该教程实际上非常好 - 但它从2015年10月7日开始。由于这是相当古老的故障和错误是可以预料的。与此同时,NodeMCU固件发生了很大变化。

这个问题显然必须在application.lua。要了解NodeMCU MQTT,我建议你看看example in our documentation。它说:

m:connect("192.168.11.118", 1883, 0, function(client) 
    print("connected") 
    -- Calling subscribe/publish only makes sense once the connection 
    -- was successfully established. You can do that either here in the 
    -- 'connect' callback or you need to otherwise make sure the 
    -- connection was established (e.g. tracking connection status or in 
    -- m:on("connect", function)). 

    -- publish a message with data = hello, QoS = 0, retain = 0 
    client:publish("/topic", "hello", 0, 0, function(client) print("sent") end) 

send_ping(),然而,从异步定时器(tmr.alarm(6, 1000, 1, send_ping))调用,只是默默地承担连接到代理,而不是连接,然后再发布。