我是一名C# .net Xamarin开发人员,现在被困在将MQTT客户端移植到ESP8266MOD wifi芯片上,因为应该这样做的人没有这么做。
无论如何,我不知道太多的东西,我闪现了一个自定义的NodeMCU构建从fightanic与文件,gpio,http,i2c,mqtt,net,node,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");命令
我得到了..。

似乎我应该看到来自application.lua的一些字符串,比如"ping“或"successfully”,但我什么也没得到。就像application.lua没有运行一样。
任何帮助都将不胜感激。提前谢谢。
--马克
更新
我直接在connect对象前面添加了一个字符串,它被打印出来了,所以它现在似乎锁定在connect对象上了。
发布于 2017-04-15 03:29:33
仅仅为了理解它的实际功能而费力地浏览这么多代码真的很难。在这里我们更喜欢minimal, complete, and verifiable examples。
看起来你理解你复制粘贴的代码背后的逻辑,对吧?该教程实际上非常不错--但它来自2015年10月7日。因为它已经相当老了,小故障和bug是可以预料到的。与此同时,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))异步调用的,并且只是默默地假定已连接到代理,而不是先连接然后发布。
https://stackoverflow.com/questions/43399563
复制相似问题