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 SL ❓❗

2020-11-19
2023-04-02
  • andras-mozes - 2020-11-19

    Hello,

    I would like to ask for some advice.

    Given a couple of sensors (lets say 10 pieces) which data I would like to send to an MQTT broker.
    So which one idea is more favorable?
    1. Create as many mqttPublisher instances as topics/sensors I have.
    2. Create only one mqttPublisher but altering wsTopicName attribute while publishing.

    Thanks for your help in advance!

     
    πŸ‘
    1
    • Morberis

      Morberis - 2020-11-19

      Reading up on it you alter the wsTopicName attribute.

      Source

      To accomplish this we can use the topic name and the message payload.
      

      Source 2

       
      πŸ‘
      1
  • i-campbell

    i-campbell - 2020-11-19

    Both are fine, so whichever makes your code easier to read. One Publisher per topic sounds easiest. One Publisher per topic takes more memory though.
    Pay attention to MQTT.MQTTParam.g_udiMaxPublishersAndSubscribers which has a default value of 20. You can change this through Library Manager.

     
    πŸ‘
    2
    • Morberis

      Morberis - 2020-11-19

      Thanks for giving a better answer and correcting me
      (β€’βŒ“β€’ )

       
    • aott33 - 2023-04-02

      Hello, I know this is an older thread, but I am running into issues trying to use one subscribe function block and altering the subscription topic.

      I want to subscribe to 31 topics. See topic examples below:

      building/1/vfd/setpointSpeedHz
      building/2/vfd/setpointSpeedHz
      ...
      building/31/vfd/setpointSpeedHz
      

      Since the only difference between the topics is the building number, I thought this would be a good use case for a "FOR LOOP". I put my subscribe function block inside the for loop and increment the building number. The issue is that I receive the payload for the last topic only.

      Below are my variables:

      VAR
          MQTTClient_0            : MQTT.MQTTClient;
          MQTTSubscribe_0         : MQTT.MQTTSubscribe;
      
          strSubTopicPrefix       : STRING := 'building/';
          iBuildingNum            : INT;
          strSubTopicSuffix       : STRING := '/vfd/setpointSpeedHz';
          strSubTopicConcat       : STRING;
          wsSubTopic              : WSTRING(1024);
          strPayload              : ARRAY[1..31] OF STRING(4096);
          wsWildCardTopic         : WSTRING(1024) := "building/+/vfd/setpointSpeedHz";
      
          FanRefSpeedJSON         : ARRAY[1..31] OF FanRefSpeedJson;
          ParseJSON               : JSON_TO_STRUCT;
      END_VAR
      

      Below is my for loop with the subscribe function block:

      FOR iBuildingNum := 1 TO 31 DO
          strSubTopicConcat := CONCAT(strSubTopicPrefix, TO_STRING(iBuildingNum));
          strSubTopicConcat := CONCAT(strSubTopicConcat, strSubTopicSuffix);
          wsSubTopic := TO_WSTRING(strSubTopicConcat);
      
          MQTTSubscribe_0 (xEnable := MQTTClient_0.xConnectedToBroker,
                          mqttClient := MQTTClient_0,
                          wsTopicFilter := wsSubTopic,
                          udiMaxPayloadSize := SIZEOF(strPayload),
                          pbPayload := ADR(strPayload));
      
          ParseJSON(Execute:= MQTTSubscribe_0.xReceived,
                  JSONString:= ADR(strPayload), 
                  JSONStringSize:= SIZEOF(strPayload),
                  JSONVars:= ADR(FanRefSpeedJSON[iBuildingNum]),
                  NumberOfVars:= SIZEOF(FanRefSpeedJSON[iBuildingNum]) / SIZEOF(JSONVAR));
      
          UserGVL.masterVfd[iBuildingNum].referenceFreq := TO_INT(FanRefSpeedJSON[iBuildingNum].value.Integer);
      END_FOR
      

      Any assistance to point me in the right direction would be appreciated!

       

      Last edit: aott33 2023-04-02

Log in to post a comment.