7.4: MQTT Publisher
Created by ppatierno on 1/3/2014 10:34:50 AM

MQTT Publisher

The publisher side of the application runs on a system based on Compact 7 or Compact 2013 real time operating system. To write managed code in C# on this platform we can use .Net Compact Framework (version 3.5 on Compact 7 and version 3.9 on Compact 2013) and Visual Studio 2008 (Compact 7) or Visual Studio 2012 (Compact 2013) as IDE.

If you want to use Compact 7 as O.S., you have to use Visual Studio 2008 to create a new Smart Device project and name it MqttLight for example; make sure you create a “Console Application” on Windows CE as “Target platform”.

Figure 7.4 : Visual Studio 2008, Smart Device Project based on .Net Compact Framework 3.5

If you want to use Compact 2013 as O.S., you have to use Visual Studio 2012 and need to install “Application Builder for Windows Embedded Compact 2013” (http://www.microsoft.com/en-us/download/details.aspx?id=38819) and the SDK for the OS image you use on your target device. In this way you can create a “Console Application” for “Windows Embedded Compact” platform (name it MqttLight for example).

Figure 7.5 : Visual Studio 2012, Windows Embedded Compact (.Net Compact Framework 3.9)

In the same solution (both VS2008 or VS2012) you have to add the M2Mqtt project from CodePlex (http://m2mqtt.codeplex.com); there are two different project files for .Net Compact Framework 3.5 (M2MqttNetCf35.csproj) and .Net Compact Framework 3.9 (M2MqttNetCf39.csproj).

All steps needed before write code are completed !!

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 publisher creates an InterfaceKit object and registers to events Attach, Detach and SensorChange; in this way it can detect sensor connection and disconnection to the device and it can handle connection and disconnection to the broker. The application gets the light value from the sensor using SensorChange event raised by InterfaceKit class and publishes it to the broker (using Publish() method of MqttClient class). Regarding MQTT client, all the default values are used like broker port 1883, QoS Level 1, no username and password, no “will” message and so on.

Regarding the subscriber side of the application, you will develop it in the next chapter when you will use Windows Embedded Standard as OS but the same library M2Mqtt to receive the messages; on the same machine you will configure the broker.

public class Program

{

// 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 Phidgets Interface Kit

private static InterfaceKit ifk;

// instance of MQTT client

private static MqttClient mqttClient;

 

static void Main(string args)

{

     ifk = new InterfaceKit();

     ifk.Attach += new Phidgets.Events.AttachEventHandler(ifk_Attach);

     ifk.Detach += new Phidgets.Events.DetachEventHandler(ifk_Detach);

     ifk.SensorChange += new

               Phidgets.Events.SensorChangeEventHandler(ifk_SensorChange);

     ifk.open();

     Console.ReadLine();

}

 

static void ifk_SensorChange(object sender, SensorChangeEventArgs e)

{

     // light sensor connected to port 0 on interface kit

     if (e.Index == 0)

     {

         mqttClient.Publish(MQTT_LUX_TOPIC,

                   Encoding.UTF8.GetBytes(e.Value.ToString()));

         }

    }

 

    static void ifk_Detach(object sender, Phidgets.Events.DetachEventArgs e)

    {

         // interface kit detached, disconnect from broker

         mqttClient.Disconnect();

    }

 

    static void ifk_Attach(object sender, Phidgets.Events.AttachEventArgs e)

    {

         // interface kit attached, connect to the broker

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

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

    }

}

print
  Comments


Turkish porno izle video site in rokettubeporno izle