mqttCredentialsSet (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

All

Firmware version:

1.70.00


This function will change the credentials used to connect to the MQTT server.

 

Some MQTT servers require that the credentials used to authenticate the client must be changed every time the client connects.

By calling qmttCredentialsSet, this can be done without having to close the connection and then opening a new connection with the changed credentials.

 

 

Input:

handle : INT

The handle to the MQTT connection.

 

username : STRING

The new user name to use when connecting to the server.

The username will not be changed if empty

 

password : STRING

The new password to use when connecting to the server.

Setting the password requires that a username is present, either in this function or in mqttOpen.

The password will not be changed if empty

 

tls_certificate : STRING

The new client certificate to use when connecting to the server.

The tls_certificate will not be changed if empty

 

tls_password : STRING

The new password for the client certificate.

The tls_password will not be changed if empty

 

reconnect : BOOL (Default FALSE)

TRUE:

The connection to the MQTT server will be disconnected and then reconnected immediately.

FALSE:

The new credentials will take effect the next time the MQTT server connection is lost.

 

 

Returns: INT

0

- Subscription is registered.

1

- Invalid handle.

2

- Invalid parameter.

 

Declaration:

FUNCTION mqttCredentialsSet : INT;
VAR_INPUT
  handle          : INT;
  username        : STRING;
  password        : STRING;
  tls_certificate : STRING;
  tls_password    : STRING;
  reconnect       : BOOL := FALSE;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  mqtt : INT;
  text : STRING;
  rxd  : mqttReceive;
  rc   : INT;
  msg  : ARRAY [1..50] OF SINT;
  time : TON;
END_VAR;
 
PROGRAM example;
 
  DebugMsg(message := "Initialize network...");
  netOpen(iface := 2);
  WHILE NOT netConnected() DO
    DebugMsg(message := "Waiting for network connection...");
    Sleep(delay := 2500);
  END_WHILE;
  DebugMsg(message := "Initialize MQTT...");
  text := strFormat(format := "RTCU_\4", v4 := boardSerialNumber());
  mqtt := mqttOpen(
                  ip       := "test.mosquitto.org",
                  port     := 1883,
                  iface    := 2,
                  clientid := text,
                  username := "user",
                  password := "Password 1"
                  );
  DebugFmt(message := "MQTT handle= \1", v1 := mqtt);
 
  // Update timer
  time(trig := ON, pt := 3600000);
 
BEGIN
  ...
  time(trig := ON);
  IF time.q THEN
    // Reconnect
    rc := mqttCredentialsSet(handle := mqtt, password := "Password 2", reconnect := TRUE);
    DebugFmt(message := "mqttCredentialsSet= \1", v1 := rc);
    time(trig := OFF);
  END_IF;
  ...
END;
END_PROGRAM;