Showing posts with label pub/sub. Show all posts
Showing posts with label pub/sub. Show all posts

Monday, March 4, 2019

Talking MQTT

This post will be about using MQTT from the command line. Don't be afraid, next post will be instructions on how to do the same thing through the GUI. This post will allow someone to do some troubleshooting if the GUI isn't working, as well as some familiarity with some of the concepts of MQTT.

Before getting started, ensure Mosquitto is installed and running on an agent. For now, it doesn't need to be on the final server, it can be on any supported platform. There is no need even for another node. If running on a Linux system including Raspberry PI be sure to install both the server/agent as well as the clients:

sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

Once mosquitto is running, then it will be necessary to have two windows connected to the server, where commands can be entered. In one window, enter the command "mosquitto_sub" to subscribe to a topic using the local agent:

pi@raspberrypi:~ $ mosquitto_sub -h localhost -t test

In the other window, enter "mosquitto_pub" to publish a message to that topic:

pi@raspberrypi:~ $ mosquitto_pub -h localhost -t test -m "hello world"

The "-t" option to both "mosquitto_sub" and "mosquitto_pub" indicates the topic the message will be published to. The "-h" option to both is the agent that will handle the messages for the topic, and can be the host name, or the ip address. The mosquitto_pub has a "-m" option for setting the payload of the message that will be published. The mosquitto_sub will just output the payload to stdout. The two windows look something like:


Multiple windows can be opened to be subscribers. When the pub message is sent, they all will receive identical copies of the payload:


If there are nodes, payloads can be sent to them as well. The server/agent host (-h option) must be the same server/agent that the node is attached to. An example, the alarm sounder from the last post can be turned on using the mostuitto_pub application. The topics it is subscribed to are "alarm/light" for the LED, and "home/alarm" for the tone generator.

 pi@raspberrypi:~ $ mosquitto_pub -h localhost -t alarm/light -m "on"

or 

pi@raspberrypi:~ $ mosquitto_pub -h localhost -t home/alarm -m "off"

Using a simple topic/payload combination many items can be controlled. The next post will be the beginning of using Node-Red, a GUI for these commands. 


Stay tuned...

Friday, February 22, 2019

Intro to MQTT

In the previous post, I talked about how MQTT came about. In this post I will talk a little about what it is but mostly how to use it.



As a publish/subscribe (pub/sub) architecture, the nodes collect data or do actions, using a network connection. In the middle is a server or agent that will allow the nodes to only have a single connection. All the nodes connect to the server, and send or receive data with that one server. The server contains the smarts about who will receive any messages sent to it. Nodes are clients in the TCP/IP nomenclature. The MQTT agent is the server.

There is a normal TCP/IP connection protocol, where a server is listening for clients to connect. The Server will accept connections, and handle multiple clients communication. The details I won't go into, but you can read about it.

As a node connects, it may register certain topics that it will listen to. When the node connects, it must inform the server what message topics it will subscribe to. The server will make a list of these devices that are subscribed to the various topics.

Nodes may send messages on any topic, but only to the server. The server will repeat the message to each node subscribed to the topic. An example would be if a node was to send a message on the topic "outside/temperature" with a payload of 52 to the server, and there were 3 nodes subscribed to this topic, then the server would send a unique message with the topic "outside/temperature" and a payload of 52 to each of the three nodes subscribed.

A single node may subscribe to any number of topics, and send to multiple other topics. The payload of the messages sent is 65535 bytes. MQTT payloads are mostly free format, and can be single values or json formatted. Messages can be commands to set, or status of a node.

 The free format nature of the MQTT Nodes, message and topics can be a challenge for interoperability. All my nodes work the way they do for me, but your nodes may work differently. If you have a node that says it is a temperature sensor, how do I know what it is measuring the temperature of? There is a proposed standard called "Homie" to address most of the confusion.

Typically an MQTT setup will have a machine dedicated to be the MQTT agent. This machine must be running all the time, and ready to act on MQTT messages. There is no connection necessary to the cloud or any outside systems. Connecting to the cloud may be something you want to do for a particular application, or to relay some information out to a remote monitoring and control system.

Dedicating a whole Windows system is probably overkill, unless there is a great deal of processing needed on the received messages. Normally the MQTT server will just pass the messages along to the required recipients. For my home control system I am using a RaspberryPi 3B running Raspian, and I am using the mosquito MQTT server. Using apt-get, from the normal Raspian repository, MQTT can be installed.



For most of the nodes, I am using ESP8266 or ESP32 boards. These boards can be purchased locally for about $15 or mail order for $3-10. I typically need to add sensors and output to these boards so I do a little customization before deploying them. The MQTT libraries are in the normal Arduino library UI. I do all the programming in the Arduino IDE, I am not pushing the limits of anything, and just trying to make a simple reliable system.

The ESP32 and ESP8266 come in various formats. The various formats make adding peripherals convenient and mounting the devices as easy or complex as the box I put it in. I have a 3D printer, so I am able to package up the nodes in pleasant looking boxes.

In future posts, I start building nodes.