As the title says... Chat on...

User avatar
By aterocana
#64901 Hi guys, I'm not able to make the esp8266 deals with mqtts brokers (not even the test.moquito.org ones).
I compiled my custom NodeMCU with TLS (and its dependancy NET) module included:
Code: Select all#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_CJSON
#define LUA_USE_MODULES_CRYPTO
#define LUA_USE_MODULES_DHT
#define LUA_USE_MODULES_ENCODER
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
#define LUA_USE_MODULES_HTTP
#define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_MQTT
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_NODE
#define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_RTCTIME
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TLS
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_WIFI


With vanilla MQTT I had no issue, I built a simple Node.js broker and everything is fine.
I don't get how to use MQTT on top of TSL, from docs isn't clear to me how to do it. Can someone help me?
This is the code I'm using so far (for sure it's not the sound way to do it, since it's not clear to me how to manage TLS):
Code: Select allfunction startMQTT()
    print("starting MQTT client")
    local _id = "nodemcu"
    local _user = "user"
    local _password = "password
    local _keep_alive = 60
    local _broker = "test.mosquito.org"
    local _port = 8883
    local _use_SSL = true
    local _auto_reconnect = true
    local _clean_session = false

    tls.cert.verify(true)

    _client = mqtt.Client(_id, _keep_alive, _user, _password, _clean_session)
    _client:lwt("/lwt", _id .. " went offline", 0, 0)

    _client:connect(_broker, _port, 1, 1,
        function(client)
            print("connected")
            local _topic = "/topic"
            local _qos = 0
            local _retain = false
            client:subscribe(_topic, _qos, function(conn)
                print("subscribed to" .. _topic)
            end)
        end
        ,
        function(client, reason)
            print("failed, reason: " .. reason)
        end
    )

    _client:on("offline", function(client)
        print("offline")
    end)

    _client:on("message", function(client, topic, data)
        print(topic .. ": ")
        if data ~= nil then
            print(data)
        end
    end)
end

I've got this:
Code: Select allPANIC: unprotected error in call to Lua API (mqtt_client.lua:13: attempt to index global 'tls)

Thanks for the help.
User avatar
By marcelstoer
#64952 As per https://nodemcu.readthedocs.io/en/lates ... sl-support you must also enable SSL in user_config.h

Code: Select all//#define CLIENT_SSL_ENABLE


I understand it's currently a bit confusing with this parameter and the TLS module but that setup is up for a change anyway, see https://github.com/nodemcu/nodemcu-firmware/issues/1700 for details.
User avatar
By pwm2vape
#64999 The TLS module is #ifdef'ed around the CLIENT_SSL_ENABLE define in user_config.h. If it's not also enabled (as described in docs) then the TLS module is not actually built even if it's def'ed in the user_modules.h.

However, I don't think looking at the code that if you want to do secure sockets over TLS you need to enable that module. It depends only on the espconn secure stuff and the SSL_ENABLE def.

BUT... I'm having horible difficulties getting this to work with SSL. I do not think dev branch or master branch with 2.0 sdk work with SSL in any way. I can debug code to point where it goes into SDK from trampoline and then it never calls user_main but resets. I think it may be a stack problem. I'm going to try to raise an issue on github, just want to make sure I'm not missing something obvious first.