IOT with Meteor and MQTT

What is this all about?

I want to create an meteor application which represents the status of up to 100 beaglebones. To archive that I digged into mqtt/mosquitto. There’s a nice talk about mqtt and iot devices by Michael Karliner:

The Setup

I’m having an old laptop(x200) which I don’t use any more, so it has to be the host for this test setup. The X200 will host the mosquitto broker and the actual meteor application. The beaglebones will use a java library to publish their data via mqtt.

Getting Started

Setting up the broker

First of all I had to install a broker to communicate too. There are a gazillion of options, but it seems like mosquitto(open source, written in C) has all the features one could ever need - with an exclusion of clustering, but I don’t intend to scale to different servers and stuff like that so it’s useless for me anyways.

Installing mosquitto could be as easy as typing:

sudo apt-get install mosquitto

…but: The mosquitto package in the debian repositories is outdated, so i had to install it via an alternative repository to get the most current version (needed for node mqtt if you don’t want to waste time configuring stuff).

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-pp
sudo apt-get update
sudo apt-get install mosquitto

After doing this you should have mosquitto up and running and the mosquitto server-side is ready!?

Setting up the meteor application

There are a few mqtt packages on atmosphere. Sadly non of them seems to have a lot of users/stars, but i found mqtt-collection to be pretty stable and fast.

All you have to do now is connecting the mqtt streams to your meteor application. The syntax is really straight forward:

//shared collection
Messages = new Meteor.Collection("messages");
//serverside mqtt in-stream
Messages.mqttConnect(server, ["topic"], {insert: bool});

For a local mosquitto server you would use “mqtt://localhost:1883” as server configuration. Then you have to specify a topic you want to subscribe to. This topic can also be a wild-card. So when having a bunch of beaglebones publishing at [“beagle/1”, “beagle/2”,…arguments] you can simply subscribe to [“beagle/+”] and all the topics are unified in one single collection. The last parameter accepts an options object - there are a bunch of options supported but i’ll only explain “insert” as it seems to be the most important one for most usecases.

When for example having status publications like this and {insert: true}

mosquittopub -h localhost -p 1883 -t "beaglebone/1" -m "idle"
mosquittopub
-h localhost -p 1883 -t "beaglebone/2" -m "reset"
mosquittopub -h localhost -p 1883 -t "beaglebone/1" -m "alive"
mosquittopub
-h localhost -p 1883 -t "beaglebone/1" -m "dead"

The resulting collection would hold all 4 entries. This is nice if you want to observe state transitions, but when spamming the status once a second or in an even higher frequency(10 hz in our setup) the database will be bloated pretty fast.

When instead setting {insert: false} the resulting database would only hold two entries which are updated via mongodb upsert commands. This is pretty handy when using reactivity to publish updates.

topic: “beaglebone/1”, message: ”dead”
topic: “beaglebone/2”, message: ”reset”

So i conclusion insert: true could be used when for example streaming error logs when I don’t want to lose information, but in most cases it probably kills the database.

The end

Thanks for reading!, i hope this helps a few people getting started with iot and meteor. This is the first post of a series - the next one will probably tackle high frequency updates ;) - stay tuned.

Thanks for your input @meteor-mentor group #meteor-mentor

Here’s the current repository if someone is interested: