Introduction to MQTTFor my IoT devices, I will be using MQTT to communicate sensor data to clients.
Here's a quick diagram showing how this works.
- Publisher can be a xBee + temperature sensor, a nodeMCU + humidity sensor, sending topic and data.
- The broker is the server part receiving the publication from the publishers (sensors) and serving the subscribers, reading the data.
- Subscribers are the clients subscribing to the topics and getting the data for display, analysis etc.
Installing a brokerIn this quick post, I am using the MQTT broker called mosquitto on fedora server 23 x64bit. Here are the steps to install and test it.
- dnf install mosquitto.x86_64
- ssh1: start mosquitto
1456270790: mosquitto version 1.4.5 (build date 2015-11-27 15:20:56+0000) starting
1456270790: Using default config.
1456270790: Opening ipv4 listen socket on port 1883.
1456270790: Opening ipv6 listen socket on port 1883.
1456270827: New connection from ::1 on port 1883.
1456270827: New client connected from ::1 as mosqsub/26144-localhost (c1, k60).
1456270870: New connection from ::1 on port 1883.
1456270870: New client connected from ::1 as mosqpub/26149-localhost (c1, k60).
1456270870: Client mosqpub/26149-localhost disconnected.
- ssh2: Starts a subscription to the topic "hello/world".
- mosquitto_sub -d -t hello/world
Client mosqsub/26499-localhost sending CONNECT
Client mosqsub/26499-localhost received CONNACK
Client mosqsub/26499-localhost sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/26499-localhost received SUBACK
Subscribed (mid: 1): 0
- ssh3: Publish a first message on the topic "hello/world"
- mosquitto_pub -d -t hello/world -m "This is my first message"
Client mosqpub/26511-localhost sending CONNECT
Client mosqpub/26511-localhost received CONNACK
Client mosqpub/26511-localhost sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (24 bytes))
Client mosqpub/26511-localhost sending DISCONNECT
1456274285: New connection from ::1 on port 1883.
1456274285: New client connected from ::1 as mosqpub/26549-localhost (c1, k60).
1456274285: Client mosqpub/26549-localhost disconnected.
In the window that started the subscription (ssh2), you should see this:
Client mosqsub/26499-localhost received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (24 bytes))and from time to time those ping request:
This is my first message
Client mosqsub/26499-localhost sending PINGREQFor the above, I used this video for reference.
Client mosqsub/26499-localhost received PINGRESP
Windows MQTT client:
Now let's try a MQTT client on Windows. I found this Chrome extension:
firewall-cmd --add-port=1883/tcpThen from the MQTTlens, you can now create a new connection like this:
After a save, you should get a connection.
Then you can try the subscribe and publish
We see here that the publish we pushed from the windows client, appeared in the subscription windows of the server. Then we sent from the server "this is my first message" and saw in the on both the server subscription and the client.
Next step... getting my IoT device talking to the broker. Hardware ordered!