B4R Question MQTT Non Blocking Thread

Discussion in 'B4R Questions' started by fbritop, May 28, 2019.

  1. fbritop

    fbritop Active Member Licensed 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
     
  2. thetahsk

    thetahsk Active Member Licensed User

    Post your code
     
  3. fbritop

    fbritop Active Member Licensed User

    The part thats matters:
    tmrWifi is a timer every 10 secs
    Code:
    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(
    TrueFalseFalse,0)
            
    'ConnectWS
            Return True
        
    Else
            
    Log("CANT CONNECT")   
            setLed(
    TrueFalseFalse,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(
    FalseTrueFalse,0)
                mqttState=
    True
                mqtt.Subscribe(JoinStrings(
    Array As String(idDevice, "/CLIENT")), 0)
            
    Else
                setLed(
    TrueFalseFalse,0)
                
    Log("Connection failed")
                mqttState=
    False
            
    End If
        
    End If
    End Sub

    Sub mqtt_Disconnected
        
    Log("Disconnected")
        mqtt.Close
        mqttState=
    False
    End Sub
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The network connection will block the program.
     
  5. fbritop

    fbritop Active Member Licensed User

    Thanks...
     
  6. fbritop

    fbritop Active Member Licensed User

    Seems MQTT blocks, but WebSocket do not. But now I have another problem. New thread anyway.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice