IotMqttv5LastWillSample
Sample for the use of MQTTv5 UserProperties
The communication to an MQTT broker using MQTTv5 is shown in this sample. In particular, the LastWill 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.
- When a connection is established, a LastWill message is specified and some properties are set for the LastWill message. The LastWill is then transmitted to the message broker in the event of a Connect, and to interested clients in the event of a Disconnect.
The following code snippet once again shows the relevant part for specifying the LastWill message, as well as the associated properties. This code position is used for the initialization (only once) of the connection parameters.
fbMqttClient.stWill.sTopic := 'MyLastWillTopic';
fbMqttClient.stWill.sContentType := 'MyContentType';
fbMqttClient.stWill.eQoS := TcIotMqttQoS.ExactlyOnceDelivery;
fbMqttClient.stWill.fbPayload.SetData(ADR(sLastWillMsg), SIZEOF(sLastWillMsg));
fbMqttClient.stWill.fbUserProperties.AddUserProperty('MyFirst', 'UserProperty');
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), |