Android Question MQTT with CA file

August Jimenez

Member
Licensed User
HI,

I'm developing a MQTT client and I would like to know how to connect with a broker using a certificate file.

The connection must use mqtts/tls protocol.

Thanks
 

udg

Expert
Licensed User
Longtime User
In my Starter service, I use something like this:
B4X:
Sub Process_Globals
  Private mqtt As MqttClient 
  Private ServerAndPort As String = "ssl://<your MQTT broker IP address>:<port number>"
  ...
End Sub

Public Sub Connect(ThisUser As TUser)
   CurrentUser = ThisUser    'this is a type used in my program, you can use a simple integer
   If mqtt.Connected Then mqtt.Close
   mqtt.Initialize("mqtt", ServerAndPort, CurrentUser.Name)
   Dim mo As MqttConnectOptions
   mo.Initialize(ThisUser.Name,ThisUser.Pwd)
   'this message will be sent if the client is disconnected unexpectedly.
   mo.SetLastWill("servizio/disconnect", CurrentUser.Name.GetBytes("UTF8"), 0, False)
   mqtt.Connect2(mo)
End Sub
Main calls Connect when needed.

My MQTT broker server resides on a cheap VPS server where I installed a free Let'sEncrypt certificate which I have to renew every three months.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Sorry, I missed the "file" part of your question. Time for a coffe..
 
Upvote 0

August Jimenez

Member
Licensed User
Thanks, Erel. I have used the second option you have mentioned, and it works, without specified the CA file.

I will open another thread about the publish method.

Regards
 
Upvote 0
Top