Search code examples
mqtttwincatstructured-text

MQTT using TC3_IoT_Communication; no connection with MQTT broker but there is no error


I had a mqtt broker running on my computer and I could connect to it with twincat. I moved the mqtt broker to a server on the same network.

I can connect with it by using Node-RED (so the hostname, hostport, username, and password must be correct), but TwinCAT does not connect to it. This is the code that I'm using to connect to my MQTT broker.

My variables:

fbMqttClient    : FB_IotMqttClient; // MQTT client
bSetParameter   : BOOL := TRUE; // set parameters once at start up
bConnect    : BOOL := TRUE; // if TRUE it will trigger MQTT client cyclically

My code:

// set parameters once when connecting to the mqtt broker
IF bSetParameter THEN
    bSetParameter               := FALSE;
    fbMqttClient.sHostName      := '172.16.1.51';
    fbMqttClient.nHostPort      := 1883;
    fbMqttClient.sTopicPrefix   := ''; 
    fbMqttClient.sUserName      := 'User';
    fbMqttClient.sUserPassword  := 'TopSecret'; 
    fbMqttClient.ipMessageQueue := fbMessageQueue;
END_IF

// MQTT client must be triggered cyclically
fbMqttClient.Execute(bConnect);

I expect a connection between my broker and TwinCAT. Instead I do not have a connection and I get this data from fbMqttClient:

bError = FALSE <br>
hrErrorCode = 16#00000001 <br>
eConnectionState = MQTT_ERR_NO_CONN

I hope someone can help me to find out where this is going wrong and how to solve this problem.


Solution

  • The problem was the firewall on the server and the problem is solved. I can still not connect with public MQTT brokers but I can connect with my own MQTT broker on the server, which is what i really wanted :).