MQTT: Unterschied zwischen den Versionen

Aus
Wechseln zu: Navigation, Suche
(Beispiele)
(Installation auf Docker)
 
(10 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 6: Zeile 6:
 
  http://www.neuendorf-online.de/blog/heimautomatisierung/mqtt-broker-mosquitto-installieren/
 
  http://www.neuendorf-online.de/blog/heimautomatisierung/mqtt-broker-mosquitto-installieren/
  
=== MQTT: Broker Mosquitto installieren ===  
+
=== Leer ===
http://www.kriwanek.de/index.php/de/homeautomation/zentrale/mqtt-mosquito/252-mqtt-broker-mosquitto-installieren
+
 
 +
== MQTT Broker mit Mosquitto im Docker-Container installieren (Variante: docker run) ==
 +
 
 +
=== Ziel ===
 +
Diese Anleitung beschreibt die Einrichtung eines Mosquitto MQTT Brokers in Docker mit folgenden Eigenschaften:
 +
 
 +
* Benutzer-Authentifizierung für 6 Benutzer (root, user1–user5)
 +
* Mehrere Listener:
 +
** Port 1883 (MQTT Standard)
 +
** Port 1884 (z. B. für interne Kommunikation)
 +
** Port 9001 (WebSocket)
 +
* Sicherheitskonfigurationen
 +
* Persistenz & Logging
 +
 
 +
=== Verzeichnisstruktur vorbereiten ===
 +
Erzeuge folgende Verzeichnisse im gewünschten Projektordner:
 +
 
 +
<pre>
 +
mkdir -p mosquitto/config
 +
mkdir -p mosquitto/data
 +
mkdir -p mosquitto/log
 +
</pre>
 +
 
 +
=== Konfigurationsdatei: mosquitto.conf ===
 +
Pfad: <code>mosquitto/config/mosquitto.conf</code>
 +
 
 +
<pre>
 +
persistence true
 +
persistence_location /mosquitto/data/
 +
 
 +
log_dest file /mosquitto/log/mosquitto.log
 +
log_type error
 +
log_type warning
 +
log_type notice
 +
log_type information
 +
 
 +
listener 1883
 +
allow_anonymous false
 +
password_file /mosquitto/config/passwd
 +
 
 +
listener 1884
 +
allow_anonymous false
 +
password_file /mosquitto/config/passwd
 +
 
 +
listener 9001
 +
protocol websockets
 +
allow_anonymous false
 +
password_file /mosquitto/config/passwd
 +
</pre>
 +
 
 +
'''Hinweis''': <code>allow_anonymous false</code> erzwingt Authentifizierung.
 +
 
 +
=== Benutzer erstellen ===
 +
Verwende das Tool <code>mosquitto_passwd</code>, um die Passwortdatei anzulegen:
 +
 
 +
<pre>
 +
mosquitto_passwd -c mosquitto/config/passwd root
 +
mosquitto_passwd mosquitto/config/passwd user1
 +
mosquitto_passwd mosquitto/config/passwd user2
 +
mosquitto_passwd mosquitto/config/passwd user3
 +
mosquitto_passwd mosquitto/config/passwd user4
 +
mosquitto_passwd mosquitto/config/passwd user5
 +
</pre>
 +
 
 +
Falls <code>mosquitto_passwd</code> nicht installiert ist:
 +
 
 +
<pre>
 +
docker run --rm -it -v $(pwd)/mosquitto/config:/mosquitto eclipse-mosquitto \
 +
  mosquitto_passwd -c /mosquitto/passwd root
 +
</pre>
 +
 
 +
=== Docker-Container starten ===
 +
Führe folgenden Befehl aus:
 +
 
 +
<pre>
 +
docker run -d \
 +
  --name mosquitto \
 +
  -p 1883:1883 \
 +
  -p 1884:1884 \
 +
  -p 9001:9001 \
 +
  -v $(pwd)/mosquitto/config:/mosquitto/config \
 +
  -v $(pwd)/mosquitto/data:/mosquitto/data \
 +
  -v $(pwd)/mosquitto/log:/mosquitto/log \
 +
  eclipse-mosquitto
 +
</pre>
 +
 
 +
=== Verbindung testen ===
 +
==== Mit mosquitto_sub / mosquitto_pub ====
 +
 
 +
<pre>
 +
mosquitto_sub -h localhost -p 1883 -t test/topic -u root -P <passwort>
 +
mosquitto_pub -h localhost -p 1883 -t test/topic -m "Hallo MQTT" -u user1 -P <passwort>
 +
</pre>
 +
 
 +
==== WebSocket-Test ====
 +
 
 +
Verbindung über WebSocket (z. B. mit MQTT Explorer, MQTT.fx):
 +
 
 +
* Host: <code>localhost</code>
 +
* Port: <code>9001</code>
 +
* Protokoll: <code>ws://</code>
 +
* Pfad: <code>/</code> (Standard)
 +
 
 +
=== Wichtige mosquitto.conf Optionen ===
 +
 
 +
{| class="wikitable"
 +
! Option !! Bedeutung
 +
|-
 +
| <code>persistence true</code> || speichert MQTT-Nachrichten (bei QoS > 0) dauerhaft
 +
|-
 +
| <code>allow_anonymous false</code> || erzwingt Benutzeranmeldung
 +
|-
 +
| <code>password_file</code> || Pfad zur Passwortdatei
 +
|-
 +
| <code>listener</code> || konfiguriert verschiedene Ports
 +
|-
 +
| <code>protocol websockets</code> || aktiviert WebSocket-Kommunikation
 +
|-
 +
| <code>log_dest</code> / <code>log_type</code> || Logging für Fehler und Diagnosen
 +
|}
 +
 
 +
=== Container stoppen und löschen ===
 +
 
 +
<pre>
 +
docker stop mosquitto
 +
docker rm mosquitto
 +
</pre>
 +
 
 +
=== Optional: ACL für Benutzerrechte ===
 +
In <code>mosquitto.conf</code> hinzufügen:
 +
 
 +
<pre>
 +
acl_file /mosquitto/config/acl
 +
</pre>
 +
 
 +
Beispiel für Datei <code>acl</code>:
 +
 
 +
<pre>
 +
user root
 +
topic readwrite #
 +
 
 +
user user1
 +
topic readwrite sensor/temperature
 +
 
 +
user user2
 +
topic read sensor/temperature
 +
</pre>
  
 
== FMEM ==
 
== FMEM ==
Zeile 25: Zeile 171:
 
== Arduino ==
 
== Arduino ==
 
=== Arduino-MQTT ===
 
=== Arduino-MQTT ===
https://github.com/256dpi/arduino-mqtt
+
https://github.com/256dpi/arduino-mqtt
 +
 
 +
https://www.smarthome-tricks.de/allgemein/nodemcu-esp8266-sensoren-an-iobroker-ueber-mqtt/
  
 
== .Net ==
 
== .Net ==
Zeile 51: Zeile 199:
  
 
  /ESPEASY-LoLin-110_110/GET/Relais/State
 
  /ESPEASY-LoLin-110_110/GET/Relais/State
 +
 +
== Python MQTT Einführungstutorial ==
 +
https://smarthome-blogger.de/blog/tutorial/python-mqtt-tutorial
 +
 +
== MQTTX ==
 +
  docker pull emqx/emqx:latest
 +
  docker run -d --name emqx  -p 1883:1883 -p 8083:8083  -p 8084:8084 -p 8883:8883  -p 18083:18083  -v $PWD/data:/opt/emqx/data  -v $PWD/log:/opt/emqx/log  emqx/emqx:latest
 +
 +
  docker pull emqx/mqttx-web:latest
 +
 +
== Mosquitto 2.0 MQTT Broker mit Management Dashboard ==
 +
https://wiki.instar.com/de/Frequently_Asked_Question/Mosquitto_2.0_with_Management_Dashboard/

Aktuelle Version vom 13. Juni 2025, 12:19 Uhr

MQTT.org

http://mqtt.org/

Mosquitto

MQTT Broker Mosquitto installieren

http://www.neuendorf-online.de/blog/heimautomatisierung/mqtt-broker-mosquitto-installieren/

Leer

MQTT Broker mit Mosquitto im Docker-Container installieren (Variante: docker run)

Ziel

Diese Anleitung beschreibt die Einrichtung eines Mosquitto MQTT Brokers in Docker mit folgenden Eigenschaften:

  • Benutzer-Authentifizierung für 6 Benutzer (root, user1–user5)
  • Mehrere Listener:
    • Port 1883 (MQTT Standard)
    • Port 1884 (z. B. für interne Kommunikation)
    • Port 9001 (WebSocket)
  • Sicherheitskonfigurationen
  • Persistenz & Logging

Verzeichnisstruktur vorbereiten

Erzeuge folgende Verzeichnisse im gewünschten Projektordner:

mkdir -p mosquitto/config
mkdir -p mosquitto/data
mkdir -p mosquitto/log

Konfigurationsdatei: mosquitto.conf

Pfad: mosquitto/config/mosquitto.conf

persistence true
persistence_location /mosquitto/data/

log_dest file /mosquitto/log/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information

listener 1883
allow_anonymous false
password_file /mosquitto/config/passwd

listener 1884
allow_anonymous false
password_file /mosquitto/config/passwd

listener 9001
protocol websockets
allow_anonymous false
password_file /mosquitto/config/passwd

Hinweis: allow_anonymous false erzwingt Authentifizierung.

Benutzer erstellen

Verwende das Tool mosquitto_passwd, um die Passwortdatei anzulegen:

mosquitto_passwd -c mosquitto/config/passwd root
mosquitto_passwd mosquitto/config/passwd user1
mosquitto_passwd mosquitto/config/passwd user2
mosquitto_passwd mosquitto/config/passwd user3
mosquitto_passwd mosquitto/config/passwd user4
mosquitto_passwd mosquitto/config/passwd user5

Falls mosquitto_passwd nicht installiert ist:

docker run --rm -it -v $(pwd)/mosquitto/config:/mosquitto eclipse-mosquitto \
  mosquitto_passwd -c /mosquitto/passwd root

Docker-Container starten

Führe folgenden Befehl aus:

docker run -d \
  --name mosquitto \
  -p 1883:1883 \
  -p 1884:1884 \
  -p 9001:9001 \
  -v $(pwd)/mosquitto/config:/mosquitto/config \
  -v $(pwd)/mosquitto/data:/mosquitto/data \
  -v $(pwd)/mosquitto/log:/mosquitto/log \
  eclipse-mosquitto

Verbindung testen

Mit mosquitto_sub / mosquitto_pub

mosquitto_sub -h localhost -p 1883 -t test/topic -u root -P <passwort>
mosquitto_pub -h localhost -p 1883 -t test/topic -m "Hallo MQTT" -u user1 -P <passwort>

WebSocket-Test

Verbindung über WebSocket (z. B. mit MQTT Explorer, MQTT.fx):

  • Host: localhost
  • Port: 9001
  • Protokoll: ws://
  • Pfad: / (Standard)

Wichtige mosquitto.conf Optionen

Option Bedeutung
persistence true speichert MQTT-Nachrichten (bei QoS > 0) dauerhaft
allow_anonymous false erzwingt Benutzeranmeldung
password_file Pfad zur Passwortdatei
listener konfiguriert verschiedene Ports
protocol websockets aktiviert WebSocket-Kommunikation
log_dest / log_type Logging für Fehler und Diagnosen

Container stoppen und löschen

docker stop mosquitto
docker rm mosquitto

Optional: ACL für Benutzerrechte

In mosquitto.conf hinzufügen:

acl_file /mosquitto/config/acl

Beispiel für Datei acl:

user root
topic readwrite #

user user1
topic readwrite sensor/temperature

user user2
topic read sensor/temperature

FMEM

WIKI Mosquitto

https://wiki.fhem.de/wiki/MQTT_Einf%C3%BChrung

MQTT in FHEM einrichten und verwenden

http://www.kriwanek.de/index.php/de/homeautomation/zentrale/fhem/244-mqtt-in-fhem-einrichten-und-verwenden
Raspberry Pi: MQTT Server mosquitto installieren und Daten in Fhem über MQTT empfangen oder “Wie werte ich Haustür klingeln aus?” 
http://blog.wenzlaff.de/?p=6487
https://github.com/georgehahn/charlotte
https://wiki.fhem.de/wiki/MQTT_Einf%C3%BChrung

Arduino

Arduino-MQTT

https://github.com/256dpi/arduino-mqtt
https://www.smarthome-tricks.de/allgemein/nodemcu-esp8266-sensoren-an-iobroker-ueber-mqtt/

.Net

https://code.msdn.microsoft.com/M2Mqtt-MQTT-client-library-ac6d3858/view/Discussions#content
http://www.firewing.info/pmwiki.php?n=FirewingUser.MQTT
http://www.firewing.info/forum/

MQTT OpenHab

https://onesmarthome.de/smart-home-openhab-2-mqtt-mosquitto/


== Beispiele ==
mosquitto_pub -h 192.168.0.40 -p 1883 -u admin -p axxxx -t Modul/3/Test -m 32
mosquitto_pub -h 192.168.0.40 -p 1883 -u admin -p axxxx -t Modul/2/In/R/1 -m ON
mosquitto_pub -h 192.168.0.40 -p 1883 -u admin -p axxxx -t Modul/2/In/R/1 -m OFF
/ESPEASY-LoLin-110_110/SET/GPIO/4   0
/ESPEASY-LoLin-110_110/SET/GPIO/4   1

/ESPEASY-LoLin-110_110/SET/GPIO/15  0
/ESPEASY-LoLin-110_110/SET/GPIO/15  1
/ESPEASY-LoLin-110_110/GET/LED-G/State
/ESPEASY-LoLin-110_110/GET/Relais/State

Python MQTT Einführungstutorial

https://smarthome-blogger.de/blog/tutorial/python-mqtt-tutorial

MQTTX

 docker pull emqx/emqx:latest
 docker run -d --name emqx   -p 1883:1883 -p 8083:8083   -p 8084:8084 -p 8883:8883   -p 18083:18083   -v $PWD/data:/opt/emqx/data   -v $PWD/log:/opt/emqx/log   emqx/emqx:latest
 docker pull emqx/mqttx-web:latest

Mosquitto 2.0 MQTT Broker mit Management Dashboard

https://wiki.instar.com/de/Frequently_Asked_Question/Mosquitto_2.0_with_Management_Dashboard/