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

User avatar
By aterocana
#65044 Thanks for the help. Update:
Running this code:
Code: Select allfunction startMQTT()
    print("starting MQTT client")
    local _id = "nodemcu"
    local _keep_alive = 60
    local _broker = "broker_address"
    local _port = 8443
    local _use_SSL = true
    local _auto_reconnect = true
    local _clean_session = false

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

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

function verify()
    print("verifying")
    tls.cert.verify([[-----BEGIN CERTIFICATE-----
    MIIDyDCCArACCQCFvizh52kCMDANBgkqhkiG9w0BAQsFADCBpTELMAkGA1UEBhMC
    SVQxDjAMBgNVBAgMBUN1bmVvMRAwDgYDVQQHDAdGb3NzYW5vMRUwEwYDVQQKDAxE
    b21vdGljYUxhYnMxETAPBgNVBAsMCFJlc2VhcmNoMR4wHAYDVQQDDBVsb2NhbGhv
    c3QubG9jYWxkb21haW4xKjAoBgkqhkiG9w0BCQEWG20uZG9taW5pY2lAZG9tb3Rp
    Y2FsYWJzLmNvbTAeFw0xNzA0MTMxMzE2MzdaFw0xODA0MTMxMzE2MzdaMIGlMQsw
    CQYDVQQGEwJJVDEOMAwGA1UECAwFQ3VuZW8xEDAOBgNVBAcMB0Zvc3Nhbm8xFTAT
    BgNVBAoMDERvbW90aWNhTGFiczERMA8GA1UECwwIUmVzZWFyY2gxHjAcBgNVBAMM
    FWxvY2FsaG9zdC5sb2NhbGRvbWFpbjEqMCgGCSqGSIb3DQEJARYbbS5kb21pbmlj
    aUBkb21vdGljYWxhYnMuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
    AQEAwM/cLcKR/DIYXdvWEOq+C4drOfPMoWus19kKNR8GuafPbhPRC1I+YixhLT/U
    X8GZfuuxYiJ0iUJo04ApmvA1xSw+QSRDZv9K+1JEyWD0Dj0ZdGJBiHX/dzqb7wrT
    ua6Yel/9QhUGmCA36mGycvdA/TlKsW8Oopa544eE2Jlf9dBBnECTKVhCJ8bO6Okm
    kHkNXqu16hj4xByjbaE29q+YYUb9DH/u0a9BNIsjF0PYREAOUJ8Q0NaJX2OIvO2p
    elmD8WKOOf54Ks7AVDAZ9vyMSe412UDYRnoJ86cNDZJvWa7SzZy6TrlgfZejUBcZ
    ZfvtyAEQH+cClgTwwBQkD0CdrwIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCvEwEd
    BZ0hpm91oBtUgGhl9cNJ7LtHpUPN6thZzYOCt+s8sJJhl8+B3boPFt20FFA7ckUd
    kpjEIlSbTpyRCyeNANkMvvuNyy9gTp8AgMCJ+pQ3aNrrT1SF6pHLCyakzA2VM/md
    afcBMWnv/pGkK9qy4J+bc5krvrwsko8+J06RxxClChi7F6n6bKZ7r8SXsMIWae45
    QJUViHCLl/uaj973kZ3UVwrcUHCABb3tbqNfkEOOOkGUT6DnrOhtaCwx4s8/lnUU
    LeJsMS82OPfsv+CM+vw3ejEf5rCypDwCR6SILqDUG0aSAULt1HFyj83FsaInqV2E
    0d+V9JHQxhg5dgUa
    -----END CERTIFICATE-----]])
    print("verification done")
end


The broker is working since I have success with other devices and the esp8266 is able to connect to it using 1883 port and disabling TLS in the broker.
What I get is:
Code: Select allstarting MQTT client
verifying
verification done
failed, reason: -5


I really need the MQTT over TLS feature. Should I use something else other than Nodemcu for that, at this point? Thank you.
User avatar
By aterocana
#65320 Hi, I'm keep trying to make it work. I found there's a function called auth in tlc.c module with this signature:
Code: Select allstatic int tls_cert_auth(lua_State *L)
. I haven't found any doc about it but reading the code it seems it should takes client certificate and key to authenticate to server through mbedTLS.
So far my code is this:
Code: Select allfunction startMQTT()
    print("starting MQTT client")
    local _id = "nodemcu" .. node.chipid()
    local _user = "username"
    local _password = "password"
    local _keep_alive = 60
    local _broker = "broker"
    -- local _broker = "iot.eclipse.org"
    local _port = 8883
    local _use_SSL = 1
    local _auto_reconnect = 0
    -- local _auto_reconnect = 1
    local _clean_session = false

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

    print("connecting to " .. _broker)
    print(verify())
    print(auth())
    _client:connect(_broker, _port, _use_SSL, _auto_reconnect,
        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

function verify()
    print("starting verify, heap: "..node.heap())
    local verified = tls.cert.verify([[
    -----BEGIN CERTIFICATE-----
    SERVER CERTIFICATE STRING (PEM file)
    -----END CERTIFICATE-----
    ]])
    print("finished verify, heap: "..node.heap())
    return verified
end

function auth()
    print("starting auth, heap: "..node.heap())
    local authenticated = tls.cert.auth(
    [[
    -----BEGIN CERTIFICATE-----
    CLIENT CERTIFICATE String (PEM file)
    -----END CERTIFICATE-----
    ]]
    ,
    [[
    -----BEGIN RSA PRIVATE KEY-----
    CLIENT PRIVATE KEY String (PEM file)
    -----END RSA PRIVATE KEY-----
    ]]
    )
    print("finished auth, heap: "..node.heap())
    return authenticated
end


I also added some debug print in the firmware before compiling it, here's the execution:
Code: Select allstarting MQTT client                                                                                                                                                                         
enter mqtt_socket_client.                                                                                                                                                                     
NodeMCU_135627                                                                                                                                                                               
length username: 25                                                                                                                                                                           
length password: 8                                                                                                                                                                           
MQTT: Init info: nodemcu1267239, USERNAME, PASSWORD                                                                                                                         
leave mqtt_socket_client.                                                                                                                                                                     
enter mqtt_socket_lwt.                                                                                                                                                                       
mqtt_socket_lwt.                                                                                                                                                                             
mqtt_socket_lwt: topic: /lwt, message: nodemcu1267239 went offline, qos: 0, retain: 0                                                                                                         
leave mqtt_socket_lwt.                                                                                                                                                                       
connecting to BROKER                                                                                                                                                       
starting verify, heap: 28384                                                                                                                                                                 
fill_page_with_pem entered                                                                                                                                                                   
finished verify, heap: 28480                                                                                                                                                                 
true                                                                                                                                                                                         
starting auth, heap: 28448                                                                                                                                                                   
fill_page_with_pem entered                                                                                                                                                                   
ENABLE: trying espconn_secure_cert_req_enable()                                                                                                                                               
INSIDE espconn_secure_cert_req_enable                                                                                                                                                                                                                                                                                                                                             
ENABLE: finish espconn_secure_cert_req_enable()                                                                                                                                               
finished auth, heap: 28480                                                                                                                                                                   
true                                                                                                                                                                                         
enter mqtt_socket_connect.                                                                                                                                                                   
TCP ip is set: 255.255.255.255                                                                                                                                                               
TCP port is set: 8883.                                                                                                                                                                       
leave mqtt_socket_connect.                                                                                                                                                                   
enter mqtt_socket_on.                                                                                                                                                                         
leave mqtt_socket_on.                                                                                                                                                                         
enter mqtt_socket_on.                                                                                                                                                                         
leave mqtt_socket_on.                                                                                                                                                                         
enter socket_dns_found.                                                                                                                                                                       
TCP ip is set: 212.35.199.35                                                                                                                                                                 
enter socket_connect.                                                                                                                                                                         
leave socket_connect, heap = 25208.                                                                                                                                                           
leave socket_dns_found.                                                                                                                                                                       
client handshake start.                                                                                                                                                                       
espconn_mbedtls.c 652, type[certificate],length[705]                                                                                                                                         
espconn_mbedtls.c 652, type[certificate],length[705]                                                                                                                                         
espconn_mbedtls.c 652, type[private_key],length[609]                                                                                                                                         
espconn_mbedtls.c 652, type[certificate],length[856]                                                                                                                                         
please start sntp first !                                                                                                                                                                     
please start sntp first !
please start sntp first !
please start sntp first !
E:M 272
please start sntp first !
please start sntp first !
E:M 272
client handshake failed!
Reason:[-0x2700]
enter mqtt_socket_reconnected.
enter mqtt_socket_disconnected.
leave mqtt_socket_disconnected.
leave mqtt_socket_reconnected.


I have a client written in node.js which is connecting to the broker. Any idea? Thanks.