To access an MQTT broker direct from the browser, you need websockets support. On a Raspberry Pi, this used to require a custom build from source. That is no longer required. You can now install direct from the mosquitto.org repository and add a simple config change. This article explains the details.
MQTT is a messaging protocol for the “Internet of Things” (IoT). It allows devices to communicate easily with minimal overheads. The Raspberry Pi of course makes an excellent low cost platform for managing IoT. Not only is it cheap to buy, it is also cheap to keep running.
To use MQTT, you need a “broker” which is simply a service running in the background. Mosquitto is one of the more popular brokers, partly because it is pretty small and therefore ideal for running on a Pi.
If you want to use MQTT from the browser however, you also need the broker to support something called “websockets” since browsers cannot directly talk using the MQTT protocol. In the past, Mosquitto didn’t have websockets compiled in by default and compiling your own version on a Pi is painful to say the least.
Thankfully, this is no longer a problem since the authors of Mosquitto now provide a repository containing ARM versions which work fine on the Pi. See mosquitto.org for the details.
Once installed from the repository, you will need to add a suitable configuration since websockets are not part of the default configuration.
To turn it on, you should add a new file to
/etc/mosquitto/conf.d/ containing something like:
# See: http://mm011106.github.io/reference/mosquitto_conf.html
# Standard Listener
# Websockets Listener
Set the protocol to accept for this listener. Can be mqtt (the default), or websockets.
Note that you can create more than one listener of the same type. You might, for example, want to create another listener that is restricted to a subset of topics so that you can allow access to that over the Internet. Perhaps you would restrict it to listing only sensor data but not control messages for your home automation project for example.
Once you have enabled websockets access to Mosquitto, you can do some interesting things directly from web pages. Get hold of MQTT.js for the browser and you can both listen to and send messages from/to the broker. Combined with a framework such as REACT allows you to, with only a few lines of code, display all the messages that arrive at your broker.
The Internet of Things (IoT) comes alive with the help of Node-Red, a Flow-Based Programming (FBP) tool designed to help link together sensors, switches, logic and displays. With hardware magic from Arduino’s, Raspberry Pi’s, low cost sensors and wireless switches. We can easily build a bespoke home automation and monitoring hub.
Having got into some electronics with my son over the last couple of years, I’ve found a new enthusiasm for doing some home automation and monitoring. In the process joining a much overhyped band of people creating the so-called “Internet of Things” (IoT)
Between Arduino’s and Raspbery Pi’s, some simple low cost sensors and a fair bit of patience, it is amazing what can be achieved.
One of the big tasks though is trying to link everything together. Generally, this involves either using a big pre-build tool such as the popular Domoticz or programming your own monitoring and control hub from the bottom up. Personally, I find the former approach too restrictive with the packages rarely doing what I need so that I am always fighting with them. But I find the latter approach too time consuming to ever get very far.
Thankfully, some great folks at IBM have come to the rescue by creating a tool called “Node-Red“. This uses an approach called “Flow-Based Programming” (FBP). Indeed, FBP was invented at IBM in the 1970’s so it seems only logical that the IBM Emerging Technology team should carry on the good work.
Why not give Node-Red a try? It is getting more and more attention with lots of new node types being contributed by the community. There is a site for reviewing new nodes and examples at flows.nodered.org. There is a Slack site and a lively Google news group. There is also some good documentation at the docs site.
You may also find Node-Red useful for a lot more than just IoT as it makes it easy to work through computing tasks, reducing coding to a minimum.