March 2018
Beginner to intermediate
410 pages
10h 40m
English
Before we publish the data, we need to consider what data is relevant to publish. We want to avoid spamming the broker, or any subscribers. We define relevant to mean values that show a significant change (more than one percentage point), or values after significant silence (at least 15 seconds):
private void PublishLight(double Light)
{
DateTime Now = DateTime.Now;
this.lastLight = Light;
if ((!this.lastPublishedLight.HasValue ||
Math.Abs(this.lastPublishedLight.Value-Light) >= 1.0 ||
(Now-this.lastLightPublishTime).TotalSeconds >= 15.0) &&
this.mqttClient != null &&
this.mqttClient.State == MqttState.Connected)
{
this.lastPublishedLight = Light;
this.lastLightPublishTime = Now;
Read now
Unlock full access