Subscribing to topics

Subscribing to topics is easy. You call the SUBSCRIBE method with one or more topics you're interested in. For each topic, you can also provide the maximum Quality of Service level you want to support on your end. Anything published with a higher Quality of Service will be downgraded on your connection. You can also use the wildcard characters, + (node) and # (branch), in your subscription request. In our example, we choose to subscribe to the Waher/MIOT/[DEVICE_ID]/Set/+ topic. Through it, publishers can execute generic set commands on actuator properties. The name of the actual property is defined by the sender, in the last subtopic node. We update the OnStateChanged event handler as follows:

this.mqttClient.OnStateChanged += (sender, state) => 
{ 
   Log.Informational("MQTT client state changed: " + 
         state.ToString()); 
 
   if (state == MqttState.Connected) 
         this.mqttClient.SUBSCRIBE( 
               "Waher/MIOT/" + this.deviceId + "/Set/+", 
               MqttQualityOfService.AtLeastOnce); 
}; 
You can use different wildcards on different levels of the topic tree to achieve interesting effects. To subscribe to a specific parameter named On, you subscribe to Waher/MIOT/[DEVICE_ID]/Set/On. To subscribe to any of the parameters on a given device you subscribe to Waher/MIOT/[DEVICE_ID]/Set/+. To subscribe to the On parameter on any device, subscribe to Waher/MIOT/+/Set/On. To subscribe to anything that is sent to a device, subscribe to Waher/MIOT/[DEVICE_ID]/#, and so on.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.123.120