I am running my broker on a Raspberry Pi and recently added TLS security/encryption to it for some IOT devices. It is not a self signed cert but one that updates periodically. All the IOT devices connect. I run an app (HomeRemote) on my phone which acts is my MQTT client. This has worked for years without TLS. See my plugin below, I have added a settings parameter “TLS” and set it to “true” and then change the port in settings to be directed to 8883 and use MQTTS in my url. While the HR app worked in simulator mode (on a Windows 11 PC), it will not connect when running the app from my phone. Can you look at my “onConnect” function in my plugin and recommend needed changes so that my client can connect to broker with TLS?
plugin.Name = “Tasmota_MQTT”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = { “Host”: “my.mqtt.host”, “Port”: “8883”, “Username”: “user”, “Password”: “pass” ,“TLS”:“false”};
var mqtt = new MQTTClient();
var host = plugin.Settings[“Host”];
var port = plugin.Settings[“Port”];
var username = plugin.Settings[“Username”];
var password = plugin.Settings[“Password”];
var tls = plugin.Settings[“TLS”];
var subscribed = false;
function onConnect() {
if(tls) {
mqtt.connect(“mqtts://” + username + “:” + password + “@” + host + “:” + port);
} else {
mqtt.connect(“mqtt://” + username + “:” + password + “@” + host + “:” + port);
}
subscribed = false;
//console.log(“connected”);
}
function onChangeRequest(device, attribute, value) {
var deviceIdParts = device.Id.split(“:”);
switch (attribute) {
case “Switch”:
mqtt.publish(“cmnd/” + deviceIdParts[0] + “/” + deviceIdParts[1], ((value == “On”) ? “ON” : “OFF”), {retain: false, qos: 1});
break;
default:
break;
}
}
function onDisconnect() {
mqtt.disconnect();
//console.log(“disconnected”);
}
function onPoll() {
if (!subscribed) {
var subscribeTopics = [
“stat/+/TEMP1”,
];
mqtt.subscribe(subscribeTopics);
subscribed = true;
}
while (true) {
var message = mqtt.readMessage();
var topicParts = message.topic.split(“/”);
if (topicParts.length == 3) {
var payloadString = message.payload.toString();
switch (topicParts[2]) {
case “TEMP1”:
var payloadJson = JSON.parse(payloadString);
for (var t in payloadJson) {
var tempDeviceId = topicParts[1] + “:” + topicParts[2] + “:” + t;
var tempDevice = plugin.Devices[tempDeviceId];
if (tempDevice) {
tempDevice.Temperature = payloadJson[t].Temperature;
}
}
break;
default:
break;
}
}
}
}
function onSynchronizeDevices() {
throw “this is optional”;
}