Difference between revisions of "Automation - MQTT"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
Line 33: Line 33:
expected result is <u><code>Active: active (running)</code></u>
expected result is <u><code>Active: active (running)</code></u>


== Debian Caveat ==
= Install client tools for testing etc =
 
(Do this on any machine expected to manually use MQTT)
 
*<code>sudo apt install mosquitto-clients</code>
 
== Testing ==
 
=== In a terminal: ===
 
*<code>mosquitto_sub -h localhost -t "mqtt" -v</code>
 
=== In another terminal: ===
 
*<code>mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"</code>
Now the message “<code>mqtt Hello MQTT</code>” will be displayed in the first terminal where the topic “'''mqtt'''” is subscribed.
 
Subscribing to '''<code>#</code>''' gives you a subscription to everything except for topics that start with a '''<code>$</code>''' (these are normally control topics anyway).
 
= Debian Caveat =
During an install on a raw Debian system, I discovered that Mosquitto refused connection when I tried to access it with anything other than "localhost" as the hostname...
During an install on a raw Debian system, I discovered that Mosquitto refused connection when I tried to access it with anything other than "localhost" as the hostname...


Line 53: Line 72:
* <code class="mwt-code">sudo service mosquitto restart</code>
* <code class="mwt-code">sudo service mosquitto restart</code>


= Install client tools for testing etc =
(Do this on any machine expected to manually use MQTT)
*<code>sudo apt install mosquitto-clients</code>
== Testing ==
=== In a terminal: ===
*<code>mosquitto_sub -h localhost -t "mqtt" -v</code>
=== In another terminal: ===
*<code>mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"</code>
Now the message “<code>mqtt Hello MQTT</code>” will be displayed in the first terminal where the topic “'''mqtt'''” is subscribed.
Subscribing to '''<code>#</code>''' gives you a subscription to everything except for topics that start with a '''<code>$</code>''' (these are normally control topics anyway).


[[Category:ServerBuilding]]
[[Category:ServerBuilding]]
[[Category:AutomationServers]]
[[Category:AutomationServers]]

Revision as of 18:33, 12 January 2022

Proven on:
Logo Mint.png
Logo LMDE.png
Logo Debian.png
Logo Ubuntu.png
Logo Sparky.png
19.3 / 20.3 4 11 (bullseye) 20.04.3 5.11
(caveat)

Mosquitto MQTT Broker

Installing Mosquitto

  • sudo apt-get install mosquitto

Pretty simple, eh?

Ensure that Mosquitto broker is running

  • sudo service mosquitto status

expected result is Active: active (running)

Install client tools for testing etc

(Do this on any machine expected to manually use MQTT)

  • sudo apt install mosquitto-clients

Testing

In a terminal:

  • mosquitto_sub -h localhost -t "mqtt" -v

In another terminal:

  • mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"

Now the message “mqtt Hello MQTT” will be displayed in the first terminal where the topic “mqtt” is subscribed.

Subscribing to # gives you a subscription to everything except for topics that start with a $ (these are normally control topics anyway).

Debian Caveat

During an install on a raw Debian system, I discovered that Mosquitto refused connection when I tried to access it with anything other than "localhost" as the hostname...

Apparently, mosquitto 2.0 binds only to the loopback interface unless specifically told otherwise.

& Debian installs v2.0 or higher...

But for now, It's a simple matter of editing the config file for Mosquitto.

  • sudo vi /etc/mosquitto/mosquitto.conf

& add in:

listener 1883
allow_anonymous true

Then,

  • sudo service mosquitto restart