2016-10-21 9 views
1

Dears, esp8266에서 nodemcu 빌드에 ​​mqtt를 사용하려고합니다. ADC, enduser_setup, 파일, GPIO, HTTP, MQTT, 그물, 노드, 흐름, PWM, TMR, UART, 와이파이nodemcu (esp8266)의 mqtt에 대한 Keepalive timer가 응답하지 않습니다.

버전 :에 의해 구동 나는 현재 가

모듈을 사용하는 사용자 지정 빌드 (https://nodemcu-build.com/index.php)

를 사용하고 루아 SDK 1.5.1에서 5.1.4 (e67da894) 나는 connect_to_mqtt_broker()를 호출하고있는 프로그램의 초기화에 따라서
---MQTT client--- 
print("--------------> Create mqtt clinet") 
--set up MQTT client 
-- init mqtt client with keepalive timer 120sec 
m = mqtt.Client("ESP"..node.chipid(), KEEP_ALIVE_TMR, USER, PASSWORD) 
m:lwt(PUB_TOPIC, "offline", 0, 0) 
m:on("offline", function(conn) 
     print("["..tmr.time().."(s)] - Mqtt client gone offline") 

end) 
m:on("message", function(conn, topic, data) 
     --receive_data(data, topic) 
     print("Data received: "..data) 
     led(200,50,50,30) 
     receive_data(data, topic) 
     led(0,204,0,150) 
end) 

function connect_to_mqtt_broker() 


    print("Connecting to broker...") 
    m:connect(BROKER, PORT, 0, 1, function(client) 
            print("connected") 
            print("["..tmr.time().."(s)] - Client connected to broker: "..BROKER) 
            m:subscribe(SUB_TOPIC,0, function(conn) 
             print("Subscribed to "..SUB_TOPIC.." topic") 
             led(0,204,0,150) 
            end) 
            m:publish(PUB_TOPIC,"Hello from: "..node.chipid()..RESTART_REASON,0,0, function(conn) 
             print("sent") 
            end) 
            end, 
            function(client, reason) 
            print("failed reason: "..reason) 
            end) 

end 
어 ich 완벽하게 작동하고 주제를 구독하고 게시 할 수 있습니다.

문제는 Keepalive 타이머가 올바르지 않다는 것입니다. 예를 들어 설명해 드리겠습니다. KEEP_ALIVE_TMR = 120을 설정하고 esp8266이 mqtt 브로커에 성공적으로 연결된 후 라우터에서 wifi를 비활성화하고 초를 계산하기 시작합니다.

m:on("offline", function(conn) 
     print("["..tmr.time().."(s)] - Mqtt client gone offline") 

end) 

내가 비활성화 무선 랜이 순간부터 정확히 120초를 해고해야하지만, 어떤 알 수없는 이유로 이런 일이되지 않습니다 오프라인 이벤트를 KEEP_ALIVE_TMR에 따르면. 일반적으로 이벤트는 약 10-15 분 후에 발생합니다. 나는이 지연의 이유를 이해하지 못하고 어려움을 겪고 있습니다. 이 이상한 일이 일어나는 이유가 있습니까?

+0

아래의 답변을 시도? – cagdas

답변

0

autoreconnect 플래그가 설정된 경우에도 동일한 문제가 발생했습니다. 이 플래그는 브로커 간 연결의 오프라인 이벤트 실행을 중단합니다. 당신은/오프에 MQTT 브로커를 켜서 테스트를 할 경우

m:connect(BROKER, PORT, 0, function(client) 
0

을, 그리고 그것을 작동합니다

그 기본 0, autoreconnect을 설정하지 않고 그것을 시도. 하지만 당신의 와이파이 연결을 바꾸는 것이 아니라, 그것은 nodemcu의 mqtt 라이브러리 문제입니다.

wifi 연결 해제시 mqtt 오프라인/연결 끊기 이벤트가 없다고 생각합니다. 여기서는 연결의 워치 독을 추가하여 해결 방법을 제시합니다.

tmr.alarm(1, 3000, 1, function() 
    if wifi.sta.getip() == nil then 
    --mark as mqtt restart needed 
    restart = true 
else 
    -- wifi reconnect detected then restart mqtt connections 
    if restart == true then 
    --reset flag, clean object, init for a new one 
    restart = false 
    m = nil 
    mqtt_init() 
    connect() 
    end 
end 
end) 

Here it is the full code example