首页 文章

NodeMCU ESP8266MOD Esplorer MQTT简单MQTT由foobarflies

提问于
浏览
0

我是一名C#.net Xamarin开发人员,他现在无法将MQTT客户端移植到ESP8266MOD wifi芯片,因为应该这样做的人没有 .

无论如何我不知道任何东西我已经从文件,gpio,http,i2c,mqtt,net,node,tmr,uart,wifi中的一个自定义NodeMCU构建 . 我正在关注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没有运行 .

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

  • 马克

更新

我在连接对象之前直接添加了一个字符串,然后打印出来,因此它似乎锁定在现在正在处理的连接对象上 .

1 回答

  • 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) )异步调用的,只是静默地假定连接到代理而不是先连接然后再发布 .

相关问题