0

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.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Louise
  • 3
  • 2

5 Answers5

0

Make sure you have an activated mqtt license and configuration on your Beckhoff runtime.

Filippo Boido
  • 1,136
  • 7
  • 11
0

Try to connect to the public broker. If it works, it must be your server.

Filippo Boido
  • 1,136
  • 7
  • 11
  • I change my code to this: ``` // set parameters once when connecting to the mqtt broker IF bSetParameter THEN bSetParameter := FALSE; //fbMqttClient.sHostName := '172.16.1.51'; fbMqttClient.sHostName := 'broker.hivemq.com'; 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); ``` Same result :( – Louise May 24 '19 at 14:21
0

It's working for me and I'm using your same code. Double check your mqtt license and make sure that it is not expired.

Screenshot of my connection to public server with your code

Filippo Boido
  • 1,136
  • 7
  • 11
  • How do I check my mqtt license? – Louise May 27 '19 at 06:49
  • In your solution explorer: Check System -> License -> Manage Licenses -> TF6701 Tc3IoT Communication and activate the configuration. – Filippo Boido May 27 '19 at 07:13
  • I have these licenses: TC3 IoT MQTT Protocol, TC3 ADS, TC3 IO, TC3 PLC, TC3 IoT Communication (MQTT), TC3 IoT Communication (I/O), TC3 IoT Functions, and TC3 IoT Communicator – Louise May 27 '19 at 07:17
  • Check the status under System -> License -> Order Information. If the current status is 'missing' reactivate the configuration. – Filippo Boido May 27 '19 at 07:22
  • there is nothing missing, all the licenses are activated – Louise May 27 '19 at 12:11
  • but i think it might not be a problem with TwinCAT, I can also not connect with broker.hivemq.com or test.mosquitto.org when I'm using mosquitto_sub. I can connect with the mqtt server that is running on localhost. – Louise May 27 '19 at 12:13
0

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 :).

Louise
  • 3
  • 2
0

my problem was due to squid that have installed. I was installed for another project. And maybe firewall can cause that problem like this.

Mr.ram
  • 1