Publishing sensor data
Now that we have a live connection, we're ready to publish sampled sensor data. We will first need some member variables. These will store last values, last-published values, and the timestamps of last-published values:
private double? lastLight = null; private bool? lastMotion = null; private double? lastPublishedLight = null; private bool? lastPublishedMotion = null; private DateTimelastLightPublishTime = DateTime.MinValue; private DateTimelastMotionPublishTime = DateTime.MinValue;
From our sampling and event methods where we receive and calculate our most recent sensor data, we call two new methods: PublishLight()
and PublishMotion()
. To illustrate different ways of publishing data, these two methods will publish the corresponding sensor data fields on individual topics, as strings. They will then individually call PublishLastJson()
, which will publish both fields on one topic, as a JSON object string.
Choosing relevant values
Before we publish the data, we need...