This function will block the thread while waiting for a message to be received, with an optional timeout.
Once a message is received, mqttWaitEvent will continue to report this until it has been read using the mqttReceive function block.
Input:
timeout : INT
Timeout period in seconds to wait.
0
|
- Disable timeout. The function will return immediately.
|
-1
|
- Wait forever. The function will only return when a message is received.
|
Returns: INT
1
|
- A message is received.
|
0
|
- Timeout.
|
-1
|
- No MQTT connections.
|
Declaration:
FUNCTION mqttWaitEvent : INT;
VAR_INPUT
timeout : INT;
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;
|