8.3: MQTT Subscriber
Created by ppatierno on 1/3/2014 10:50:34 AM

MQTT Subscriber

The subscriber side of the application runs on a system based on Windows Embedded Standard (7 or 8). In this case, we can use full desktop .Net Framework (any version) and any Visual Studio as IDE to develop a simple Windows Forms or WPF (Windows Presentation Foundation) application that acts as monitor (subscriber) for receiving light values and shows them to the user.

To be simple you can create a Windows Forms application using Visual Studio 2012 and name it MqttLightWinForms. In the same solution you have to add the M2Mqtt project from CodePlex (http://m2mqtt.codeplex.com) and using M2Mqtt.csproj project file for .Net Framework 4.0 (or change the target version).

Fig8.1

Figure 8.1 : Visual Studio 2012, Windows Forms project creation

As you remember from previous chapter, in this example your device is located in the room1 of the building1 you are monitoring, so the topic to publish light values will be building1/room1/lux. The subscriber needs to subscribe to the same topic to receive these values and show them to the user. On Load event of the Form class, the application creates an MqttClient instance and connects to the broker that in this case is running on the same machine; after this, it subscribes to the building1/room1/lux topic. Furthermore, it registers to the MqttMsgPublishReceived event of MqttClient class to receive notification when a published message arrives on the topic. In the event handler for receiving message from MQTT client object, the application updates the UI, a simple textbox to show light value received.

 

public partial class Form1 : Form

{

// MQTT broker IP address

private const string MQTT_BROKER_ADDRESS = "192.168.1.5";

// MQTT topic to publish light/lux value

private const string MQTT_LUX_TOPIC = "building1/room1/lux";

 

// instance of MQTT client

private static MqttClient mqttClient;

 

public Form1()

{

   InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

   // create MQTT client instance

   mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));

  

   // register for published message received

   mqttClient.MqttMsgPublishReceived += mqttClient_MqttMsgPublishReceived;

 

   // connect to the broker

   mqttClient.Connect(Guid.NewGuid().ToString());

 

   // subscribe to light topic

   string topics = new string { MQTT_LUX_TOPIC };

   byte qosLevels = new byte { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };

 

   mqttClient.Subscribe(topics, qosLevels);

}

 

void mqttClient_MqttMsgPublishReceived(object sender,

                                       MqttMsgPublishEventArgs e)

{

   if (this.InvokeRequired)

   {

this.Invoke(new Action(() => this.txtLight.Text =

                             Encoding.UTF8.GetString(e.Message)));

   }

}

}

As you can see in the following figures, the broker and the subscriber client can run both on Windows Embedded Standard 7 and Windows Embedded 8 Standard.

Fig8.2

Figure 8.2 : Windows Embedded Standard 7

Fig8.3

Figure 8.3 : Windows Embedded 8 Standard

print
  Comments


Turkish porno izle video site in rokettubeporno izle