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:
11: mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS), MQTT_BROKER_PORT);
12: mqttClient.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(mqttClient_MqttMsgPublishReceived);
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 !!");
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#.
Hi Paulo, I have not progressed much with MQTT, I have managed to get my standard netduino 1 networked now. I used a really cheap £2.60 ENC28J60 module and the mIp TCP/IP network stack http://mip.codeplex.com/. This also means I could use my netduino GO! over ethernet as well, although I haven't tried it yet. I also have some GPIO expansion, I am controlling my entire house and have around 60 relays split across 2 distribution boards. Its some what industrial! But at the moment I trying to get something together to control all the relays over the network. I really want to get MQTT working as this is definitely the best thing for home automation and I would love to use node-red as well at some point. I guess this issue I am going to face is the networking element of your libraries which will probably not work as is with the mIp stack? The other issue which goes back to my previous question, I now have my own virtualized server infrastructure at home. Consisting of a physical with 3 virtuals (SQL, App, Web). I've got MQTT installed on the webserver, however I really can't figure out how to configure it so that I can connect to it from the rest of my network? Again my network is quite business like with network points all over the split across switches in 2 cabs. The aim is to have a netduino running the relays in each of the distribution boards, then other distributed intelligence and sensor arrays around the house all providing data back to MQTT which can be logged and managed centrally. I would really appreciate if you can give me a little guidance on how best to configure and secure my MQTT server, I read your post about SSL, but I think I'm getting a little ahead of myself and could with getting Mosquitto up and running first before I try complicating things further with SSL right? Also if you could give me some direction as to the feasibility of me adapting your library or whether it will just work with mIP as is that would be great? Also great work, I am really interested to see how the windows 8 mobile support develops! Cheers, Andy
Hi Andy, I read your comment only now. Have you solved your problems ? Paolo.
Hi, I have been playing with mqtt for a few days just using command line at the moment. I plan to use it with a bunch of netduinos to automate my house, having the netduino as "dumb" clients and a central service controlling everything. Messages will be sent and received using mqtt to and from the netduinos however I had a few questions... Starting with the mqtt server, I have found very little information about how people configure this to run on say windows server or just standard windows and used as a server. I plan on having mqtt running on a virtual machine with my home controller service running on a separate vm, a separate database server and a web server. So far I have had mqtt running on my laptop but I haven't seen anything on how to set it up on a server a connect in to it from another computer? Additionally I wondered how you deal with message priorities in your netduino applications? Have you used any wills etc? Also can you buffer queues of messages and process them as services become available and prioritise how the messages are picked up from the queue and do you have any examples of how this can be achieved? Many thanks, Andy
View this page in another language: