IotMqttv5UserPropsSample
Sample for the use of MQTTv5 UserProperties
The communication to an MQTT broker using MQTTv5 is shown in this sample. In particular, the UserProperties mechanism of MQTTv5 is demonstrated. As a prerequisite, the message broker used must support MQTTv5. The basic flow of the sample is as follows:
- There is a PLC project that connects to a local message broker based on MQTTv5 and publishes messages to it. At the same time, the application subscribes to the same topic to receive the sent messages again.
- The structure of the program corresponds to the sample IotMqttv5Sample. In addition to the actual payload, the messages also contain UserProperties, the handling of which is demonstrated both when sending and receiving messages.
The following code snippet once again shows the relevant part for specifying the User Properties on a message to be sent.
fbPubProps.sContentType := sContentType;
fbPubProps.nMsgExpiryInterval := 7;
fbPubProps.bPayloadUtf8 := TRUE;
fbPubProps.ClearUserProperties();
FOR m:=1 TO 10 DO
hrPropSet := fbPubProps.AddUserProperty(aUserName[m], aUserValue[m]);
IF FAILED(hrPropSet) THEN
EXIT;
END_IF
END_FOR
In this sample, 10 User Properties whose key/value values are taken from the aUserName and aUserValue arrays are thus added to a message to be sent. The user properties are then passed to the message to be sent as input parameters to the Publish() method.
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v3.1.4026.0 | IPC or CX (x86, x64, ARM) | Tc3_IotBase (>= v3.4.2.0), |