B4R Question MQTT Non Blocking Thread

fbritop

Active Member
Licensed User
Longtime User
Is it possible for the MQTT library, not to block the main thread (ex: GPIO State changes).

I do connect to a broker, in case the broker looses connectivty, then B4R resets with:

Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.

It tries to recconect, but until there is no succesful connection tothe broker, none of the INPUT gpio can detect a change state.

I know ESP is single thread, but is there anyway we can still receive the gpio state changes, while the MQTT client is trying to reconnect?

Board is an ESP WROMM 32

Thanks
FBP
 

fbritop

Active Member
Licensed User
Longtime User
The part thats matters:
tmrWifi is a timer every 10 secs
B4X:
Private Sub AppStart
   mqtt.Initialize(wifiClient.Stream, serverIp, serverPort, idDevice, "mqtt_MessageArrived", "mqtt_Disconnected")
End Sub
Sub tmrWifi
    If Not(wifi.IsConnected) Then
        Log("tmrWIfiRecconect")
        wifiConnected=wifiConnect
    Else
        Log("CNX OK")
        ConnectWS(0)
    End If
End Sub
Private Sub wifiConnect As Boolean
    If wifi.Connect2(ssid, wifiPwd) Then
        Log("WIFI CONNECTED")
        setLed(True, False, False,0)
        'ConnectWS
        Return True
    Else
        Log("CANT CONNECT")   
        setLed(True, False, False,0)
        Return False
    End If
End Sub

Sub ConnectWS(unused As Byte)
    If mqttState=False Then
    
        If mqtt.Connect Then 'ws.ConnectIp(wifiClient.Stream, serverIp, serverPort, "/?idDevice=10150") Then
            Log("Connected...")
            setLed(False, True, False,0)
            mqttState=True
            mqtt.Subscribe(JoinStrings(Array As String(idDevice, "/CLIENT")), 0)
        Else
            setLed(True, False, False,0)
            Log("Connection failed")
            mqttState=False
        End If
    End If
End Sub

Sub mqtt_Disconnected
    Log("Disconnected")
    mqtt.Close
    mqttState=False
End Sub
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Seems MQTT blocks, but WebSocket do not. But now I have another problem. New thread anyway.
 
Upvote 0
Top