Before starting to develop the software on subscriber side, you need to configure network settings for your Netduino Plus board; the necessary steps are the same as the publisher described in the previous paragraph.
Regarding the subscriber firmware, it uses an MqttClient instance to connect to the broker and to subscribe to the topic about temperature value in the room1 of building1. There is also a registered event handler for the MqttMsgPublishReceived event raised when a message is published on that topic. Inside the event handler, the firmware uses an OutputPort that wraps the onboard led of Netduino Plus; using Write() method on this port, the led is powered on or off if the temperature value received on topic is higher than a specified maximum value.
public class Program
{
// MQTT broker IP address
private const string MQTT_BROKER_ADDRESS = "192.168.1.6";
// MQTT topic to subscribe for temperature value
private const string MQTT_TEMP_TOPIC = "building1/room1/temp";
// temp alarm (°C)
private const double TEMP_MAX = 30;
// istance of MQTT client
private static MqttClient mqttClient;
// output port for onboard led
private static OutputPort outLed;
public static void Main()
outLed = new OutputPort(Pins.ONBOARD_LED, false);
// create MQTT client and connect to the broker
mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
mqttClient.Connect(Guid.NewGuid().ToString());
mqttClient.MqttMsgPublishReceived += mqttClient_MqttMsgPublishReceived;
// subscribe to temperature topic
string topics = new string { MQTT_TEMP_TOPIC };
byte qosLevels = new byte { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };
mqttClient.Subscribe(topics, qosLevels);
Thread.Sleep(Timeout.Infinite);
}
static void mqttClient_MqttMsgPublishReceived(object sender,
MqttMsgPublishEventArgs e)
double temp = Double.Parse(new String(Encoding.UTF8.GetChars(e.Message)));
outLed.Write(temp > TEMP_MAX);