I have my mosquitto server running with authentfication and TLS on.
The server is working well with a windows and android client so i guess the server is fine.
The ESP client does connect but it wont pub or sub. I have enabeled all logs for this matter wich tells me the connection is ok but the pub/sub messages are not received by the server. Im struggeling with this for a wile and im posting here now becouse im stuck. If anyone could look at my code and point me in the right direction, that would be appreciated.
I am using a Lolin board for this with this firmware:
NodeMCU custom build by frightanic.com
branch: master
SSL: true
modules: enduser_setup,file,gpio,http,mdns,mqtt,net,node,tmr,uart,wifi,tls
powered by Lua 5.1.4 on SDK 2.2.1(cfd48f3)
Here is my code existing out 4 files:
Init.lua
connect = require("connect")
config = require("config")
espmqtt = require("espmqtt")
connect.start()
-- Wait untill we get a ip number
tmr.alarm(1,1000, 1, function()
if wifi.sta.getip()==nil then
print("Connecting...")
else
print("Connected to wifi as: " .. wifi.sta.getip())
espmqtt.start()
tmr.stop(1)
end
end)
connect.lua
local module = {}
function module.start()
enduser_setup.start(
function()
-- Execute when connected
end,
function(err, str)
print("enduser_setup: Err #" .. err .. ": " .. str)
end
);
end
return module
config.lua
local module = {}
module.SSID = {}
-- MQTT setup
-- IP address and port of MQTT server
module.HOST = "mydomain.com"
module.PORT = 8883
-- TLS connection
module.SECURE = 1
-- MQTT user and password
module.USER = "myuser"
module.PASSWORD = "mypass"
-- MQTT topic
module.ENDPOINT = "thuis"
module.ID = node.chipid()
return module
espmqtt.lua
local module = {}
local function mqtt_start()
-- init mqtt client with logins, keepalive timer 120sec
m = mqtt.Client(config.ID, 120, config.USER, config.PASSWORD)
-- on publish message receive event
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect(config.HOST, config.PORT, config.SECURE, function(client)
print("connected to MQTT broker")
-- subscribe topic with qos = 0
client:subscribe("thuis", 0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
client:publish(config.ENDPOINT, "hello", 0, 0, function(client) print("sent") end)
end,
function(client, reason)
print("failed reason: " .. reason)
end)
--m:close();
end
function module.start()
mqtt_start()
end
return module