NetduinoMQTT

NetduinoMQTT is an implementation of the MQTT protocol for the Netduino platform.

It's functionality right now is limited to QoS 0 publishing and subscribing.

Disclaimer:  This blog does not reflect the opinions of my employers, past, present or future.  Any nonsense, errors or omissions are mine alone.  This software is a personal project, it is not officially supported by me and certainly not by my employer.  You use it at your own risk.

Which isn't to say that I don't welcome bug reports, testing results, feature requests, patches, etc.

Where to get it?
  https://github.com/danielan/NetduinoMQTT


What is MQTT?

MQTT stands for "Message Queue Telemetry Transport". It is a lightweight protocol for sharing messages.  It uses a publish/subscriber model that has a broker and a client.  The client connects to the broker and sends a message associated with a topic which is then sent to all clients that have subscribed to receive the messages associated with that topic.

There is also more to it - things like broker to broker communications, queuing, various levels of quality of service (QoS), etc.  More information is available at the links in the "more information" section below.

I thought it would be a fun and appropriate choice as a protocol to communicate temperature readings from my Netduino Plus.

How do you use/test this?

Before you start with this, you should probably have a networked Netduino microcontroller like the Netduino Plus (http://netduino.com/netduinoplus/) and you should have some basic experience/understanding of how to develop for and deploy to this platform.  In other words, blink a few LED's before you start this project. :)

Download the code from above.  (I'll start posting downloads - for now you can cut/paste from the browse feature unless you want to set up GIT.)

Once you have that, to use this, create a new Netduino Application in Visual Studio that includes NetduinoMQTT.cs.  There is also a Program.cs that you can use, or write your own.  The rest of this page is going to assume that you used the Program.cs I provided.

Next, just deploy this to your Ethernet attached Netduino. 

To test this out, you will need a broker.  I've used two different brokers on this during development; Really Small Message Broker (RSMB) and Mosquitto.

To test this with RSMB on Windows:

Download RSMB (see "More Information" below) and unzip it.

Next, open 4 "cmd" or powershell (maybe a little less annoying for UTF8 testing) windows and make your working directory in the folder "windows" under the rsmb directory you just extracted.

In one cmd window run: broker.exe
In the next cmd window run: stdinpub.exe test
In another cmd window run: stdoutsub.exe test --clientid one
In the last cmd window run: stdoutsub.exe test --clientid two

"test" is the name of our topic (this is good to remember).

Now, if you type something into the stdinpub.exe window and press enter - you should see it show up in the two stdoutsub.exe windows.

If you have NetduinoMQTT deployed to your Netduino, if you press the onboard button, you should also see a message show up in the two stdoutsub.exe windows.

You'll also see some messages about connections, etc in the broker.exe window.

To test this with Mosquitto on Windows:

Note: If you did the RSMB tests - remember to kill at least broker.exe before proceeding - so that Mosquitto can bind to the default port.


Download Mosquitto for Windows (see "More Information" below) and install it.

Next, open 4 "cmd" or powershell (maybe a little less annoying for UTF8 testing) windows and make your working directory in the folder where Mosquitto was installed (C:\Program Files (x86)\mosquitto).

In one cmd window run: mosquitto.exe
In the next cmd window run: mosquitto_pub.exe -t test -l
In another cmd window run: mosquitto_sub.exe -t test
In the last cmd window run: mosquitto_sub.exe -t test

"test" is the name of our topic (this is good to remember).

Now, if you type something into the mosquitto_pub.exe window and press enter - you should see it show up in the two mosquitto_sub.exe windows.

If you have NetduinoMQTT deployed to your Netduino, if you press the on-board button, you should also see a message show up in the two mosquitto_sub.exe windows.

You won't see messages about connections, etc in the mosquitto.exe window by default, but you can add this by editing and using the config file (sample provided in the installation).


Is this production ready?

Not really.  Try to keep in mind that this is code for a hobby microcontroller.  If you are planning to use this to track the temperature of your nuclear reactor or operate some sort of DIY pacemaker - this is not a good plan.  :)  But if you want to track the temperature of your basement for fun - this might be a fun project.

To this point, it has not been particularly well tested.  I've done some basic functional testing, but various conditions are not yet accounted for and will often lead to unhandled exceptions, etc.  Given the limitations of the system and a lack of time I haven't completely tested some things (i.e., 256MB messages, etc).


More information?

MQTT:  http://mqtt.org
A series on MQTT on ReadWriteWeb:
http://www.readwriteweb.com/hack/2011/11/ibms-andy-piper-negotiating-th.php
http://www.readwriteweb.com/hack/2011/11/ibms-andy-piper-part-2-how-mqt.php
http://www.readwriteweb.com/hack/2011/11/ibms-andy-piper-part-3-the-tax.php
Here's a really high-level video; MQTT & Internet of Things:
http://www.youtube.com/watch?v=WVWAXnbnV3w


Paho: http://eclipse.org/proposals/technology.paho/
MQTT 3.1 Specification: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
RSMB: https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=d5bedadd-e46f-4c97-af89-22d65ffee070
Mosquitto: http://mosquitto.org/
Netduino: http://netduino.com/

License:


Copyright 2011-2012 Dan Anderson. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY DAN ANDERSON ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL DAN ANDERSON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

1 comment:

  1. This is awesome. I was looking for a way to get a message about captured information from a Netduino to a PC, and this "just worked"! Thanks for putting in the work on this.

    ReplyDelete