Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

MQTT_client disconnect when call MQTT_sunscribe

alien
2022-06-21
2023-10-10
  • alien - 2022-06-21

    Dear all,

    Hi, I am working with mqtt client SL library and I faced with an error but I couldn't find any solution.
    The problem is when I call mqtt client and following it I call mqtt publish it works fine and connection to broker stablished and I can received the published payload in the broker but when I call mqtt subscribe connection to broker is disconnected and I see this error :"Invalid Packet Bit Flags in first byte of Fixed Header"

    here you can see my code:

    PROGRAM mqtt_client
    VAR
    
        //----------------- Connection ------------------------
        Mqtt_Client     : MQTT.MQTTClient;
        sHostname       : STRING(255) := '';  // Hostname or ip address or URL (for WebSockets: ws:// or wss://)
        Port            : UINT := 12345;     // Port of the MQTT broker
        sUsername       : WSTRING(255):="";
        sPassword       : WSTRING(255):="QxDsfQ4hS-KcCZxia0W5cw";
        eCommunicationMode : MQTT.COMMUNICATION_MODE := MQTT.COMMUNICATION_MODE.TCP; // Communication mode: TCP/IP or WebSockets
        eMQTTVersion    : MQTT.MQTT_VERSION := MQTT.MQTT_VERSION.V3_1_1; // The MQTT protocol version
        sWillMessage    : STRING(1024);     // The last will message
        sWebSocketUrl   : STRING(255) ;
        // Example of connect properties (V5 only)
        mQTTConnectProperties : MQTT.MQTTConnectProperties := (
            udiSessionExpiryInterval := 10,
            udiMaximumPacketSize := MQTT.MQTTParam.g_udiMaxPacketSize,
            userProperties := 
                [
                    (wsKey := "key1", wsValue := "value1"),
                    (wsKey := "key2", wsValue := "value2"), 
                    (wsKey := "key3", wsValue := "value3")
                ]
        );
    
    
        DiagMsg_1       : STRING;
        xError_1        : BOOL;
        Connected       : BOOL;
    
        //----------------- Publish ------------------------
        Mqtt_Publish    : MQTT.MQTTPublish; 
        sTopicPublish   : WSTRING(1024) := "NeXo"; // Topic to publish a message
        pDone           : BOOL;
        pError          : BOOL;
        eTupe           : MQTT.MQTT_ERROR;
        eError: MQTT.MQTT_ERROR;
        length: INT;
        length1: UINT;
    
    
        message_1       : STRING:='';     // Message to publish in JSON format
        message_2       : STRING;     // Message to publish in JSON format
        message_3       : STRING;     // Message to publish in JSON format
        message_4       : STRING;     // Message to publish in JSON format
        message_5       : STRING;     // Message to publish in JSON format
        message_6       : STRING;     // Message to publish in JSON format
        message_7       : STRING;     // Message to publish in JSON format
        message_8       : STRING;     // Message to publish in JSON format
        message_9       : STRING;     // Message to publish in JSON format
        message_10      : STRING;     // Message to publish in JSON format
    
    
    
        sState          : BOOL;
        sState1         : BOOL;
        sState2         : BOOL;
        sState3         : BOOL;
        sState4         : BOOL;
        sState5         : BOOL;
        sState6         : BOOL;
        sState7         : BOOL;
        sState8         : BOOL;
        sState9         : BOOL;
        sState10        : BOOL;
    
        state           : WORD;
    
        //----------------- Subscribe -----------------------
        MQTT_Subscribe  : MQTT.MQTTSubscribe;
        wsSubcribeTopicFilter : WSTRING(1024) := "maxbess"; // Topic filter to subcribe a topic
        Subs_msg        : STRING;
        msg_size        : UDINT;
        sDone           : BOOL;
        sError          : BOOL;
        Error_num       : MQTT.MQTT_ERROR;
    
    END_VAR
    
    --------------------------------------------------------------------------------
    
    Mqtt_Client(xEnable:=TRUE,
                sHostname:=sHostname,
                sWebSocketUrl:=sHostname,
                uiPort:=Port,
                wsUsername:=sUsername,
                wsPassword:=sPassword,
                //pbWillMessage:=ADR(sWillMessage),
                //uiWillMessageSize:=DINT_TO_UINT(Stu.StrLenA(ADR(sWillMessage))),
                eCommunicationMode:=eCommunicationMode,
                eMQTTVersion:=eMQTTVersion,
                //mQTTConnectProperties:=mQTTConnectProperties,
                eMQTTError=>eError,
                xConnectedToBroker=>Connected,
                xError=>xError_1);
    
    Mqtt_Publish(xExecute:=PLC_PRG.CLK_500ms,
                 udiTimeOut:= 1000000,
                 pbPayload:= ADR(message_1),
                 udiPayloadSize:=INT_TO_UINT(LEN(message_1)),
                 mqttClient := Mqtt_Client,
                 wsTopicName:= sTopicPublish,
                 xDone=> pDone,
                 xError=>pError,
                 eMQTTError=>eTupe);
    
    
    MQTT_Subscribe(xEnable:= plc_prg.CLK_1s,
                  pbPayload:=ADR(Subs_msg),
                  udiMaxPayloadSize:= SIZEOF(Subs_msg),
                  mqttClient:= Mqtt_Client,
                  wsTopicFilter:=wsSubcribeTopicFilter,
                  udiPayloadSize => msg_size,
                  xDone => sDone,
                  xError=> sError,
                  eMQTTError=> Error_num);
    code
    

    I would appreciate it if you help me to find a solution.

     

    Last edit: alien 2022-06-21
  • damian177 - 2023-10-10

    Probably I have the same problem, When I connect to broker and try subscribe to much topics then the client return error, did you solve your problem ?

     

Log in to post a comment.