Since I loaded my small MQTT client. Net Framework (Desktop, Compact and Micro) on CodePlex, I have never posted a simple example of the use of time for questions.

In this post, we shall see how it is possible to realize a "simple" alarm system consists of two boards Netduino Plus both connected in the network, through the first board we are able to detect a movement through the use of a PIR (Passive InfraRed) sensor while the second is capable of receiving the notification of the detection (in this case does is to write it on the console Debug). Obviously, the second board may be trivially replaced by a smartphone through which we could be alerted in real time of the intrusion.

Regarding the broker, it is the simple RSMB (Really Small Message Broker) of IBM which can be used in a totally free for non-commercial purposes and downloaded here. You do not need to install it but, in the case of Windows, is a simple executable to be launched in order to have a broker and MQTT listening on the default port (1883). In our example, it is currently running on my PC that acts in a sense as a server.

The trivial application running on the board that serves as part detector is the following:

 

   1: public class Program
   2: {
   3:    private const string MQTT_PIR_TOPIC = "alarm";
   4:    private const string MQTT_BROKER_ADDRESS = "192.168.1.5";
   5:    private const int MQTT_BROKER_PORT = 1883;
   6:  
   7:    private static MqttClient mqttClient;
   8:    
   9:    public static void Main()
  10:    {
  11:        Pir pir = new Pir(Pins.GPIO_PIN_D0);
  12:        pir.Motion += new Pir.MotionEventHandler(pir_Motion);
  13:  
  14:        mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
  15:        mqttClient.MqttMsgPublished += new MqttClient.MqttMsgPublishedEventHandler(mqttClient_MqttMsgPublished);
  16:        mqttClient.Connect("MQTTNetduinoPir");
  17:  
  18:        Thread.Sleep(Timeout.Infinite);
  19:    }
  20:  
  21:    static void mqttClient_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
  22:    {
  23:        Debug.Print("Message published ");
  24:    }
  25:  
  26:    static void pir_Motion(object sender, PirEventArgs e)
  27:    {
  28:        Debug.Print("Motion " + e.Motion + " Time " + e.Time);
  29:        if (e.Motion)
  30:            mqttClient.Publish(MQTT_PIR_TOPIC, new byte { 0x01 }, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE);
  31:    }
  32:  
  33: }

Pir class is used for the sensor (see uPLibrary on CodePlex), which raises an event at the detection of a movement. Through the class MqttClient connection is made to the broker, and a message is published on the topic "alarm" to report the detection of the sensor.

The application that receives the reports is the following:

 

   1: public class Program
   2: {
   3:     private const string MQTT_PIR_TOPIC = "alarm";
   4:     private const string MQTT_BROKER_ADDRESS = "192.168.1.5";
   5:     private const int MQTT_BROKER_PORT = 1883;
   6:  
   7:     private static MqttClient mqttClient;
   8:     
   9:     public static void Main()
  10:     {
  11:         mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS), MQTT_BROKER_PORT);
  12:         mqttClient.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(mqttClient_MqttMsgPublishReceived);
  13:         
  14:         mqttClient.Connect("MQTTNetduinoReceiver");
  15:         mqttClient.Subscribe(new string { MQTT_PIR_TOPIC }, new byte { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
  16:  
  17:         Thread.Sleep(Timeout.Infinite);
  18:     }
  19:  
  20:     static void mqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
  21:     {
  22:         if (e.Message[0] == 0x01)
  23:             Debug.Print("Alarm !!");
  24:     }
  25:  
  26: }

It through the MqttClient, does nothing more than to register topic "alarm" through which receives notifications via a message from the sender shot through the broker.

Basically, we have developed a simple Push Notification system that no issue could be exploited through the Internet, perhaps using a Windows Azure Worker Role to perform the broker, since there is no broker written entirely in C#.