MQTT/ESP8266/NodeMCU/Lua代码未发布 [英] MQTT / ESP8266 / NodeMCU / Lua code not publishing

查看:192
本文介绍了MQTT/ESP8266/NodeMCU/Lua代码未发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ESP8266上的以下Lua代码有问题...

I have a problem with the following Lua code on an ESP8266...

function sendData(humidity,temperature)
    -- Setup MQTT client and events
    print("sendData() entered")
    print("Setting up mqtt.Client...")
    m = mqtt.Client(mqtt_client_id, 120, username, password)
    print("Attempting client connect...")
    m:connect(mqtt_broker_ip , mqtt_broker_port, 0, function(conn)
        print("Connected to MQTT")
        print("  IP: " .. mqtt_broker_ip)
        print("  Port: " .. mqtt_broker_port)
        print("  Client ID: " .. mqtt_client_id)
        print("  Username: " .. mqtt_username)

        payload = "Temp: " .. temperature .. " Hmdy: " .. humidity
        m:publish("pt/env",payload, 0, 0, function(conn)
            print("Going to deep sleep for " .. (DSLEEPTIME/1000) .. " seconds")
            node.dsleep(DSLEEPTIME*1000,4)             
        end)
    end)
end

正在成功通过以下代码调用代码...

The code is being successfully called with the following...

-- Connect to network
wifi.setmode(wifi.STATION)
wifi.setphymode(wifi_signal_mode)
wifi.sta.config(wifi_SSID, wifi_password) 
wifi.sta.connect()

print("Attempting to connect...")
ip = wifi.sta.getip()
if ip ~= nil then
    print("Got IP: " .. ip)
    print("About to call sendData()...")
    sendData(humidity, temperature)
    print("Returned from sendData()...")
end

使用ESPlorer,我看到以下内容...

Using ESPlorer I see the following...

Attempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Attempting to connect...
Got IP: 192.168.0.39
About to call sendData()...
sendData() entered
Setting up mqtt.Client...
Attempting client connect...
Returned from sendData()...

因此它基本上输入了sendData(...),我看到了该行的输出...

So it basically enters sendData(...) and I see the output from the line...

print("Attempting client connect...")

...但是我从没看到m:connect(...)块中的日志记录,例如...

...but I never see the logging in the m:connect(...) block such as...

print("Connected to MQTT")

...似乎只是立即返回.

...it seems it simply returns immediately.

MQTT经纪人是运行Mosquitto的Raspberry Pi,我已经在Android手机和平板电脑上的应用程序中对其进行了测试.我可以在手机和平​​板电脑之间双向成功发布/订阅.

The MQTT broker is a Raspberry Pi running Mosquitto and I've tested it with apps on my Android phone and tablet. I get successful publishing / subscription between phone and tablet in both directions.

我是Lua的新手,只了解MQTT的基础知识,如果有人可以帮助,m:connect(...)块有什么问题我很茫然.

I'm a Lua novice and only understand the basics of MQTT and I'm at a loss for what's wrong with the m:connect(...) block if anyone can help it would be appreciated.

更新:已解决问题-很抱歉不尽快返回该线程.问题仅归结于我在RPi上运行的Mosquitto版本(符合MQTT v3.1). NodeMCU MQTT库支持MQTT v3.1.1,并且不向后兼容.从本质上讲,尽管我做了一些更改,但我的代码并没有什么大错-仅仅是由于MQTT版本不兼容.

UPDATE: PROBLEM SOLVED - Apologies for not getting back to this thread sooner. The problem was simply down to the version of Mosquitto I was running on my RPi (which complied to MQTT v3.1). The NodeMCU MQTT library supports MQTT v3.1.1 and is NOT backward compatible. In essence there wasn't very much wrong with my code although I did make some changes - it was simply down to MQTT versions being incompatible.

推荐答案

您没有告诉我们您使用的NodeMCU版本.警告:请勿使用 https://github上可用的任何预构建的0.9.x二进制文件. com/nodemcu/nodemcu-firmware/releases .按照 http://nodemcu.readthedocs.io/en/dev/构建自己的固件zh/build/.

You didn't tell us what NodeMCU version you use. Warning: do NOT use any of the pre-built 0.9.x binaries available at https://github.com/nodemcu/nodemcu-firmware/releases. Build your own firmware as per http://nodemcu.readthedocs.io/en/dev/en/build/.

我总是帮助剥离一个失败的函数并利用所有可用的回调函数.我可以从将数据发送到cloudmqtt.com的dev分支近两个月的固件中确认以下工作:

I always helps to strip a failing function and to make use of all available callback functions. I can confirm the following works on a nearly 2 months old firmware from the dev branch sending data to cloudmqtt.com:

function sendData(humidity, temperature)
    print("Setting up mqtt.Client...")
    m = mqtt.Client("SO-36667049", 120, "user", "password")
    print("Attempting client connect...")
    m:connect("m20.cloudmqtt.com", 12703, 0, 0,
        function(conn)
            print("Connected to MQTT")
            payload = "Temp: " .. temperature .. " Hmdy: " .. humidity
            m:publish("topic", payload, 0, 0, 
                function(client) 
                    print("Message sent") 
                end)
        end,
        function(client, reason)
            print("Connection failed, reason: " .. reason)
        end)
end

差异:

  • m:connect显式定义安全y/n和自动重新连接y/n.如果仅设置所有可选参数的子集,这总是让我感到困惑.您在m:connect中的0被解释为secure还是autoreconnect?我对Lua不太了解,无法告诉我为什么要对其进行明确编码.
  • 为失败的连接尝试使用额外的回调函数.请参见 http://nodemcu.readthedocs .org/en/dev/en/modules/mqtt/#connection-failure-callback-reason-codes 以获得失败原因代码.
  • 对于回调函数中的变量,请不要使用与父"函数中相同的名称.请注意如何使用m:connect(..., function(conn),然后在该函数内部再次使用m:publish(..., function(conn).您不会在代码中与conn对象进行交互,因此不会造成任何危害.但是,这可能会在其他项目中给您带来困扰.
  • m:connect defines secure y/n and autoreconnect y/n explicitly. It always confuses me if only a subset of all optional parameters is set. Is your 0 in m:connect interpreted as secure or as autoreconnect? I don't know Lua well enough to tell why I code it explicitly.
  • Make use of the extra callback for function for failed connection attempts. See http://nodemcu.readthedocs.org/en/dev/en/modules/mqtt/#connection-failure-callback-reason-codes for failure reason codes.
  • Do NOT use the same name for variables in callback functions as used in "parent" functions. Note how you use m:connect(..., function(conn) and then inside that function you use m:publish(..., function(conn) again. You don't interact with the conn object in your code, so no harm done. However, this may bite you in other projects.

这篇关于MQTT/ESP8266/NodeMCU/Lua代码未发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆