Difference between revisions of "Automation - MQTT"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
{| class="wikitable" style="float: right;" border="2" data-mce-style="float: right;"
{{{!}} class="wikitable" style="float: right; width: 322px;" border="2"
|+Proven on:
{{!}}+ Proven on:
|-
{{!}}- <!-- Debian -->
| style="text-align: center;" data-mce-style="text-align: center;"|[[File:Logo Mint.png|60px|link=https://linuxmint.com|center|middle|frameless]]
{{!}} style="text-align: center; width: 60px;" {{!}} [[File:Logo Debian.png{{!}}60px{{!}}link=https://www.debian.org/{{!}}center{{!}}middle{{!}}frameless]]
| style="text-align: center;" data-mce-style="text-align: center;"|[[File:Logo LMDE.png|60px|link=https://linuxmint.com/edition.php?id=279|center|middle|frameless]]
{{!}} style="text-align: center; width: 40px;" {{!}} 12 (bookworm)
| style="text-align: center;" data-mce-style="text-align: center;"|[[File:Logo Debian.png|60px|link=https://www.debian.org/|center|middle|frameless]]
{{!}} ([[#Debian / Version Caveat{{!}}caveat]])
| style="text-align: center;" data-mce-style="text-align: center;"|[[File:Logo_Ubuntu.png|60px|link=https://ubuntu.com/|center|middle|frameless]]
<br>
| style="text-align: center;" data-mce-style="text-align: center;"|[[File:Logo Sparky.png|60px|link=https://sparkylinux.org/|center|middle|frameless]]
{{!}}- <!-- LMDE -->
|-
{{!}} style="text-align: center; width: 65.675px;" {{!}} [[File:Logo LMDE.png{{!}}60px{{!}}link=https://linuxmint.com/edition.php?id=279{{!}}center{{!}}middle{{!}}frameless]]
|19.3 / 20.3
{{!}} 4
|4
{{!}} ([[#Debian / Version Caveat{{!}}caveat]])
|11 (bullseye)
<br>
|20.04.3
{{!}}- <!-- Linux Mint -->
|5.11
{{!}} style="text-align: center; width: 67.5125px;" {{!}} [[File:Logo Mint.png{{!}}60px{{!}}link=https://linuxmint.com{{!}}center{{!}}middle{{!}}frameless]]
|-
{{!}} 19.3 / 20.3
{{!}}
<br>
|([[#Debian Caveat{{!}}caveat]])
{{!}}- <!-- Ubuntu -->
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Ubuntu.png{{!}}60px{{!}}link=https://ubuntu.com/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 20.04.3
|}
{{!}}
<br>
{{!}}- <!-- Sparky Linux -->
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Sparky.png{{!}}60px{{!}}link=https://sparkylinux.org/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 5.11
{{!}} ([[#Debian / Version Caveat{{!}}caveat]])
<br>
{{!}}}


[https://mosquitto.org/ Mosquitto MQTT Broker]
[https://mosquitto.org/ Mosquitto MQTT Broker]
{{{!}} class="wikitable" style="border-collapse: collapse; width: 33%; left;"
{{!}}- style="text-align: center;" 
! style="width: 50%;" colspan="2" {{!}} As always...
{{!}}-
{{!}} {{!}}
Start with:
{{!}} {{!}}
*<code>sudo apt update</code>
*<code>sudo apt upgrade</code>
{{!}}}


= Installing Mosquitto =
= Installing Mosquitto =
Line 28: Line 46:
Pretty simple, eh?
Pretty simple, eh?


(Tho...  If you want the latest & gratest and you're feeling adventurous...  There's a [[Mosquitto from Source|CopyPasta page here]].)
== Ensure that Mosquitto broker is running ==
== Ensure that Mosquitto broker is running ==


Line 33: Line 52:
expected result is <u><code>Active: active (running)</code></u>
expected result is <u><code>Active: active (running)</code></u>


== Debian Caveat ==
== Note: Ubuntu repositories have an outdated version ==
 
If you want to know which version you've installed...
 
*<code>sudo mosquitto</code>
(Ignore the error message "Error: Address already in use". It's already running as a service.)
 
= 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 / Version Caveat =
(Ignore this if you built from source.  It's apparently a repo thing...)
 
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 42: Line 89:
But for now, It's a simple matter of editing the config file for Mosquitto.
But for now, It's a simple matter of editing the config file for Mosquitto.


* <code class="mwt-code">sudo vi /etc/mosquitto/mosquitto.conf</code>
* <code>sudo vi /etc/mosquitto/mosquitto.conf</code>


& add in:
& add in:
Line 51: Line 98:
Then,
Then,


* <code class="mwt-code">sudo service mosquitto restart</code>
* <code>sudo service mosquitto restart</code>


= Install client tools for testing etc =
= Securing the broker =
[https://mosquitto.org/documentation/authentication-methods/ Reference]


(Do this on any machine expected to manually use MQTT)
Mosquitto supports password authentication...


*<code>sudo apt install mosquitto-clients</code>
Simplest is:


== Testing ==
== password Authentication ==


=== In a terminal: ===
You can create a password file by:
* <code>sudo mosquitto_passwd -c /etc/mosquitto/SecretSquirrels '''USERNAME'''</code>
Then edit the configuration:
* <code>sudo vi /etc/mosquitto/mosquitto.conf</code>
and replace <code>allow_anonymous true</code> with <code>password_file /etc/mosquitto/SecretSquirrels</code>


*<code>mosquitto_sub -h localhost -t "mqtt" -v</code>
Then restart the broker:


=== In another terminal: ===
* <code>sudo service mosquitto restart</code>
From this point, you have to provide '''USERNAME''' & '''PASSWORD''' to access it.


*<code>mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"</code>
=== Note: ===
Now the message “<code>mqtt Hello MQTT</code>” will be displayed in the first terminal where the topic “'''mqtt'''” is subscribed.
The password file must be able to be read by whatever user Mosquitto is running as. On Linux/POSIX systems this will typically be the mosquitto user, and /etc/mosquitto/password_file is a good place for the file itself.


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:AutomationServers]]

Latest revision as of 17:53, 12 October 2025

Proven on:
Logo Debian.png
12 (bookworm) (caveat)


Logo LMDE.png
4 (caveat)


Logo Mint.png
19.3 / 20.3


Logo Ubuntu.png
20.04.3


Logo Sparky.png
5.11 (caveat)


Mosquitto MQTT Broker

As always...

Start with:

  • sudo apt update
  • sudo apt upgrade

Installing Mosquitto

  • sudo apt-get install mosquitto

Pretty simple, eh?

(Tho... If you want the latest & gratest and you're feeling adventurous... There's a CopyPasta page here.)

Ensure that Mosquitto broker is running

  • sudo service mosquitto status

expected result is Active: active (running)

Note: Ubuntu repositories have an outdated version

If you want to know which version you've installed...

  • sudo mosquitto

(Ignore the error message "Error: Address already in use". It's already running as a service.)

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 / Version Caveat

(Ignore this if you built from source. It's apparently a repo thing...)

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

Securing the broker

Reference

Mosquitto supports password authentication...

Simplest is:

password Authentication

You can create a password file by:

  • sudo mosquitto_passwd -c /etc/mosquitto/SecretSquirrels USERNAME

Then edit the configuration:

  • sudo vi /etc/mosquitto/mosquitto.conf

and replace allow_anonymous true with password_file /etc/mosquitto/SecretSquirrels

Then restart the broker:

  • sudo service mosquitto restart

From this point, you have to provide USERNAME & PASSWORD to access it.

Note:

The password file must be able to be read by whatever user Mosquitto is running as. On Linux/POSIX systems this will typically be the mosquitto user, and /etc/mosquitto/password_file is a good place for the file itself.