Android Question MQTT problem

DC1

Member
Licensed User
Longtime User
Hi
I have modified/created this B4A app - that will not connect to the esp8266, specifically the MQTT topic esp/temperature.

At first I wasn't sure if the esp was functioning correctly - so I tested it with MQTT.fx - it gave me the expected results (I'm using mosquitto on my Pi as the broker) - see attached

I'm using B4A 10.2 with the jMQTT v1.01 lib file

I'm at a bit of a loss - anyone have any thoughts ?


B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Private working As Boolean = True
    Public mqtt As MqttClient
    Private username As String = ""
    Private password As String = ""
    Public temperature As Float
End Sub

Sub Service_Create
    working = True
    'mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4A" & Rnd(0, 999999999))
    mqtt.Initialize("mqtt", "ssl://192.168.0.153:1883", "B4A" & Rnd(0, 999999999))
    ConnectAndReconnect
End Sub

Sub ConnectAndReconnect
    Do While working
        If mqtt.IsInitialized Then mqtt.Close
        'mqtt.Initialize("mqtt", "ssl://192.168.0.153:1883", "esp8266/temperature")
        mqtt.Initialize("mqtt", "ssl://192.168.0.153:1883", "B4A" & 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")
            AfterConnect
            Do While working And mqtt.Connected
                mqtt.Publish2($"${username}/f/ping"$, Array As Byte(0), 1, False)
                Sleep(5000)
            Loop
            Log("Disconnected")
        Else
            Log("Error connecting.")
        End If
        UpdateState
        Sleep(5000)
    Loop
End Sub

Private Sub AfterConnect
    mqtt.Subscribe($"esp8266/temperature"$, 0)
    UpdateState
End Sub

Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    If Topic.EndsWith("esp8266/temperature") Then
        Dim temp As String = BytesToString(Payload, 0, Payload.Length, "ascii")
        temperature = temp
        UpdateState
    End If
End Sub

Sub UpdateState
    CallSub(Main, "SetState")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Reviewing the Log file gives me
 

Attachments

  • logs.png
    logs.png
    30.5 KB · Views: 59
  • result with MQTT.fx.png
    result with MQTT.fx.png
    36.7 KB · Views: 60
Top