Off-The-Shelf Hacker: Build a Dedicated Messaging Broker
The sprinkler controller and yard light project both use MQTT messaging for data communications. Up until now, my Linux notebook served as the broker during initial development efforts. I use my notebook for daily consulting work too, so it makes sense to transition over to a dedicated machine for the broker as the projects move forward.
Ultimately, I’ll have an array of yard sensors around my house, as well as the sprinkler controller silently awaiting commands, out in the garage. MQTT is exceedingly easy to use and I can certainly see using it for local network communication among devices, in many yet-to-be-dreamed-up future projects.
Today we’ll look at setting up a very basic MQTT broker on a Raspberry Pi. The Pi is reliable, fairly inexpensive and has built-in analysis and interfacing capabilities through various programming languages and applications. In future articles, we’ll examine adding security features, data logging through scripts and graphical user interface applications.
MQTT is a machine-to-machine protocol that simplifies communications between computing devices. It uses a client-server model and is available on Linux, Windows and other operating systems. I’ve had good luck with the Mosquitto version of the client and broker, on both Ubuntu and Raspbian Linux. You can also use libraries to pass messages with Arduino firmware and Python or Processing programs. You can even do messages directly to and from the command line, which is great for prototyping.
All of this magic happens through an MQTT broker. The broker coordinates the messages and is a software program. The client subscribes to a topic on the broker and when a message is published it appears at the client. Likewise, if the client publishes a message, to a topic on the broker, the message is seen by all the clients subscribed to that topic. You can have many clients and many topics all shipping messages back and forth through the broker. You can even have the broker and clients talking together on the same machine.
You won’t believe how easy it is to put Mosquitto on the Raspberry Pi.
I started with a relatively fresh installation of Raspbian Linux on a Raspberry Pi model B+. MQTT is very lightweight and you don’t need a lot of horsepower to run the broker. I also connected a monitor via an HDMI cable and a wireless Logitech keyboard/mousepad. Connectivity to the local network was handled with a Cat 5 cable from my router. You can just as easily run the broker over WiFi, such as in the case of a Raspberry Pi 3. A standard 2.0 Amp cell phone charger and cable were used for power. Someday, I’ll probably convert over to a small 5-volt dedicated power supply. When everything is stable I’ll run the broker on the Pi without a monitor or keyboard.
It is always a good idea to upgrade Linux to the latest version. From the command line enter the following and enter your password if required.
pi% sudo apt-get update
pi% sudo apt-get upgrade
Next, install the Mosquitto MQTT broker package using the following.
pi% sudo apt-get install mosquitto
This command installs the Mosquitto broker. It will also set up the broker to start automatically, whenever the Raspberry Pi is rebooted.
At the same time, you can also install the mosquitto clients. This way, you can subscribe and publish messages directly from the command line. It is convenient for testing.
pi% sudo apt-get install mosquitto-clients
You’ll need to know the IP address of the broker in order to send/receive messages. Find it using this command in a terminal.
I used the wired Ethernet interface and not wireless, so the address appeared under the eth0 device name.
Now we can test the setup.
First, start the mosquitto client in a terminal to watch for messages. I chose the “mqtt” topic at random.
pi% mosquitto_sub -h 192.168.1.118 -t mqtt
In a separate terminal window, I issued a mosquitto command to publish a message to the “mqtt” topic.
pi% mosquitto_pub -h 192.168.1.118 -t mqtt -m hello
The IP for my broker was 192.168.1.118. You’ll need to use your own particular IP for your installation. The word “hello” should immediately show up on your mosquitto_sub terminal screen.
That’s all it takes to get a Raspberry Pi ready to exchange messages with networked MQTT clients.
A dedicated MQTT broker, on the Pi moves my WiFi-based sensor projects ever closer to actually using the things to gather data, watch for intruders and turn on lights and sprinklers.
I’ve covered the MQTT programs for the yard sensor over the last few weeks. Obviously, the firmware for the NodeMCU board will need to reflect the new IP address of the Raspberry Pi-powered MQTT broker.
The real gem in all of this is that MQTT abstracts away the details of the communication process. Put a broker on your network, then subscribe and publish messages to do things. If a sensor detects movement just publish the data and other clients will see it. The client could be hooked up to a light, a buzzer or a text file for logging activity. The client might also be a user interface on a screen that displays status or publishes a command to turn on a light, at the push of an onscreen button. Or… it could be a hardware button on a little box.
With MQTT and a Raspberry Pi, you can do a lot of remote control stuff with just a little effort.
The post Off-The-Shelf Hacker: Build a Dedicated Messaging Broker appeared first on The New Stack.