mqttPublish (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Firmware version:

3.12 / 1.00.00


This function will publish a message to a MQTT server.

The function will not block the MQTT interface while waiting for confirmation when using QoS 1 or 2.

 

The message will only be sent if connected to the MQTT server.

If the connection is lost before the function returns, the message will be sent when the connection is re-established, unless mqttPendingClear is called.

 

 

Input:

handle : INT

The handle to the MQTT connection.

 

topic : STRING

The topic of the message.

 

qos : SINT (0..2)

The QoS to publish the message with.

 

retained : BOOL

If TRUE the server should retain a copy of the message, if FALSE the message is not retained.

Retained messages will be transmitted to new subscribers of the topic by the server.

 

data : PTR

Address to the buffer with the contents of the message.

 

size : INT

The number of bytes to send.

Please note that the maximum size of the message depends on the architecture of the device:

oX32 architecture: 1024 bytes,

oNX32 architecture: 4096 bytes.

oNX32L architecture: 16384 bytes.

 

 

Returns: INT

0

- Message was sent.

1

- Invalid handle.

2

- Not connected to the MQTT server.

3

- Invalid parameter.

4

- A publish is already in progress.

5

- Message transmission interrupted due to connection loss. The message will be transmitted again when connection is reestablished.

 

Declaration:

FUNCTION mqttPublish : INT;
VAR_INPUT
   handle   : INT;
   topic    : STRING;
   qos      : SINT;
   retained : BOOL;
   data     : PTR;
   size     : INT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
   mqtt  : INT;
   text  : STRING;
   topic : STRING;
   time  : TON;
   temp  : DINT;
   rc    : INT;
   buf   : ARRAY [1..15] OF SINT;
END_VAR;
 
PROGRAM example;
   DebugMsg(message := "Initialize GPRS...");
   gsmPower(power := ON);
   gprsOpen();
   WHILE NOT gprsConnected() DO
      DebugMsg(message := "Waiting for cellular connection...");
      Sleep(delay := 2500);
   END_WHILE;
   DebugMsg(message := "Initialize MQTT...");
   text  := strFormat(format := "RTCU_\4", v4 := boardSerialNumber());
   topic := strFormat(format := "RTCU/\4/temperature", v4 := boardSerialNumber());
   mqtt  := mqttOpen(ip := "test.mosquitto.org", port := 1883, clientid := text);
   DebugFmt(message := "MQTT handle= \1", v1 := mqtt);
 
   IF NOT mqttConnected(handle := mqttTHEN
      rc := mqttStatus(handle := mqtt);
      DebugFmt(message := "Connection failed, Status=\1", v1 := rc);
   END_IF;
 
   time(trig := OFF, pt := 60000);
 
BEGIN
   time(trig := ON);
   IF time.THEN
      temp := boardTemperature();
      text := dintToStr(:= temp / 100) + "." + dintToStr(:= abs(:= temp MOD 100));
      strToMemory(dst := ADDR(buf), str := text, len := strLen(str := text));
      rc := mqttPublish(
                        handle   := mqtt,
                        topic    := topic,
                        qos      := 1,
                        retained := FALSE,
                        data     := ADDR(buf),
                        size     := strLen(str := text)
                       );
      DebugFmt(message := "Published= \1", v1 := rc);
      time(trig := OFF);
   END_IF;
END;
END_PROGRAM;