To add MQTT support to our sensor, we will use the MqttClient
class defined in the Clayster.Library.Internet.MQTT
namespace. We start by adding the following namespace to our using
section in the code:
using Clayster.Library.Internet.MQTT;
Communication with the MQTT server will be done from a separate thread in our example. This is to assure that we avoid timing problems with the measurement logic. When new values are available, we flag this fact to the MQTT thread using auto-reset event objects. So, we need the following static variables:
private static Thread mqttThread = null; private static AutoResetEvent mqttNewTemp = new AutoResetEvent(false); private static AutoResetEvent mqttNewLight = new AutoResetEvent(false); private static AutoResetEvent mqttNewMotion = new AutoResetEvent(false);
The application will then publish values to MQTT topics if a significant change has occurred, or when the given time has passed since its last publication. So, we can create...