To add MQTT support to our actuator, we create a new UWP app project and name it
ActuatorMqtt. We copy the actuator logic from our
Actuator project, and the preceding procedures to create and maintain an MQTT connection to the broker. Even though it's an actuator, we want to publish sensor data for it, representing the current state of the actuator. If it changes state, we want the new state to be published. But since the value will not be updated using a regular interval, as in the sensor case, we will ask the broker to retain our latest value. We will also use a different Quality of Service level: At Least Once. This is to make sure that the most recent value is propagated correctly. We don't need to use the Exactly Once level, since the operation is idempotent:
this.mqttClient.PUBLISH( "Waher/MIOT/" + this.deviceId + "/On", MqttQualityOfService.AtLeastOnce, true, Encoding.UTF8.GetBytes(On.ToString())); StringBuilder Json = new StringBuilder...