Sub ConnectAndReconnect
Do While working
If mqtt.IsInitialized Then mqtt.Close
mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4X" & Rnd(0, 999999999))
Dim mo As MqttConnectOptions
mo.Initialize(username, password)
Log("Trying to connect")
mqtt.Connect2(mo)
Wait For Mqtt_Connected (Success As Boolean)
If Success Then
Log("Mqtt connected")
Do While working And mqtt.Connected
mqtt.Publish2("ping", Array As Byte(0), 1, False) 'change the ping topic as needed
Sleep(5000)
Loop
Log("Disconnected")
Else
Log("Error connecting.")
End If
Sleep(5000)
Loop
End Sub