2017-04-13 11 views
0

저는 이제 ESP8266MOD 무선 랜 칩으로 MQTT 클라이언트를 이식하는 C# .net Xamarin 개발자입니다.NodeMCU ESP8266MOD Esplorer MQTT 간단한 MQTT by foobarflies

아무리해도 나는 파일, gpio, http, i2c, mqtt, 그물, 노드, tmr, uart, wifi와 함께 fightanic에서 사용자 정의 NodeMCU 빌드를 번쩍 내 버렸습니다. 나는 simple MQTT project by foorbarflies을 팔로우하고있다. 내가 갓 플래시 칩에 다음 파일을 업로드 한

: 나는 명령 dofile("test.lua");

내가 얻을 보내

-- 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() 

.......

esplorer grab

"ping"또는 "successfully subscribed"와 같이 application.lua에서 문자열의 일부를 볼 수 있지만 아무것도 얻지 못하는 것 같습니다. 그것은 application.lua가 실행되지 않는 것과 같습니다.

도움을 주시면 감사하겠습니다. 미리 감사드립니다.

은 - 마크

업데이트

본인은 연결 객체를 구축하기 전에 직접 문자열을 추가하고 지금 작업 연결 개체에 잠기는 것 같다 있도록 인쇄.

+0

ESP8266/NodeMCU 관련 질문에 대해 투표를 한 사용자에게는 다음과 같은 질문이 있습니다. 질문에 대한 기술을 이해하지 못한다고해서 문제가 해결되었다는 의미는 아닙니다. IoT는 사실이며 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))에서 비동기 적으로 호출하고 아무것도하지 않고 오히려 먼저 연결 한 후 게시보다 브로커에 연결하는 가정합니다.