mqttReceive (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

3.12 / 1.00.00


This function will return a received message.

 

Note that the message is only valid after a "message received" event is raised; the event will be removed when this function block is called.

The RTCU will buffer all valid incoming messages in an internal buffer with room for 10 messages - further messages will be lost.

 

 

Input:

data : PTR

Address of the buffer where the data of the message is stored in.

 

maxsize : INT

The maximum number of bytes that can be stored in the buffer. Any message data after this limit will be lost.

Please note that the firmware is only capable of handling messages with up to a device specific limit, and will truncate the data if a message is received with more data.

The limit depends on the architecture of the device:

oX32 architecture: 1024 bytes,

oNX32 architecture: 4096 bytes.

oNX32L architecture: 16384 bytes.

 

Output:

ready : BOOL

True if a message has been received.

 

handle : INT

The handle of the MQTT connection the message is received from.

 

topic : STRING

The topic of the received message.

 

size : INT

The actual number of bytes stored in the buffer.

 

retained : BOOL

If TRUE the message is not a new one, but one that has been retained by the MQTT server, if FALSE the message is a new one.

 

duplicate : BOOL

If TRUE the message is a duplicate, if FALSE is is not a duplicate.

 

Declaration:

FUNCTION_BLOCK mqttReceive;
VAR_INPUT
  data     : PTR;
  maxsize   : INT;
END_VAR;
VAR_OUTPUT
  ready     : BOOL;
  handle   : INT;
  topic     : STRING;
  size     : INT;
  retained : BOOL;
  duplicate : BOOL;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  mqtt : INT;
  text : STRING;
  rxd : mqttReceive;
  rc   : INT;
  msg : ARRAY [1..50] 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());
  mqtt := mqttOpen(ip := "test.mosquitto.org", port := 1883, clientid := text);
  DebugFmt(message := "MQTT handle= \1", v1 := mqtt);
 
  IF NOT mqttConnected(handle := mqtt) THEN
    rc := mqttStatus(handle := mqtt);
    DebugFmt(message := "Connection failed, Status=\1", v1 := rc);
  END_IF;
 
  rc := mqttSubscribe(handle := mqtt, qos := 2, topic := "RTCU/+/temperature");
  DebugFmt(message := "Subscribed= \1", v1 := rc);
  rxd(data := ADDR(msg), maxsize := SIZEOF(msg));
 
BEGIN
  rc := mqttWaitEvent(timeout := 5);
  IF rc > 0 THEN
    DebugMsg(message := "Message received");
     rxd();
    DebugMsg(message := "-Topic= " + rxd.topic);
    DebugFmt(message := "-Handle= \1", v1 := rxd.handle);
    DebugFmt(message := "-Size= \1", v1 := rxd.size);
    DebugMsg(message := "-Temperature= " + strFromMemory(src := ADDR(msg), len := rxd.size));
  END_IF;
END;
END_PROGRAM;