MQTT: Unterschied zwischen den Versionen
Stefan (Diskussion | Beiträge) (→Leer) |
Stefan (Diskussion | Beiträge) (→Installation auf Docker) |
||
Zeile 7: | Zeile 7: | ||
=== Installation auf Docker === | === Installation auf Docker === | ||
+ | Gerne! Hier ist eine **ausführliche und gut strukturierte Anleitung im WIKI-Format** zur **Installation von Mosquitto MQTT in Docker (Variante: `docker run`)**, inklusive: | ||
+ | |||
+ | * Mehreren Benutzern (`root`, `user1` bis `user5`) | ||
+ | * Mehreren Ports: 1883 (Standard MQTT), 1884 (zweiter Broker-Port), 9001 (WebSockets) | ||
+ | * Wichtigsten Konfigurationsoptionen für produktive Umgebungen | ||
+ | |||
+ | --- | ||
+ | |||
+ | # 🐳 MQTT Broker mit Mosquitto im Docker-Container installieren (`docker run` Variante) | ||
+ | |||
+ | ## 🔧 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 | ||
+ | |||
+ | ```bash | ||
+ | mkdir -p mosquitto/config | ||
+ | mkdir -p mosquitto/data | ||
+ | mkdir -p mosquitto/log | ||
+ | ``` | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## 📝 `mosquitto.conf` erstellen | ||
+ | |||
+ | Pfad: `mosquitto/config/mosquitto.conf` | ||
+ | |||
+ | ```conf | ||
+ | # Speicherung aktivieren | ||
+ | persistence true | ||
+ | persistence_location /mosquitto/data/ | ||
+ | |||
+ | # Logging konfigurieren | ||
+ | log_dest file /mosquitto/log/mosquitto.log | ||
+ | log_type error | ||
+ | log_type warning | ||
+ | log_type notice | ||
+ | log_type information | ||
+ | |||
+ | # Standard MQTT Port | ||
+ | listener 1883 | ||
+ | allow_anonymous false | ||
+ | password_file /mosquitto/config/passwd | ||
+ | |||
+ | # Zusätzlicher MQTT Port | ||
+ | listener 1884 | ||
+ | allow_anonymous false | ||
+ | password_file /mosquitto/config/passwd | ||
+ | |||
+ | # WebSocket-Port | ||
+ | listener 9001 | ||
+ | protocol websockets | ||
+ | allow_anonymous false | ||
+ | password_file /mosquitto/config/passwd | ||
+ | ``` | ||
+ | |||
+ | > ⚠️ `allow_anonymous false` sorgt dafür, dass nur authentifizierte Benutzer zugelassen werden. | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## 🔐 Benutzer erstellen | ||
+ | |||
+ | Verwende das Tool `mosquitto_passwd`, um eine Passwortdatei anzulegen: | ||
+ | |||
+ | ```bash | ||
+ | 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 | ||
+ | ``` | ||
+ | |||
+ | > Du wirst jeweils nach einem Passwort gefragt. Die Datei `passwd` wird im Format mit SHA256-Hashes gespeichert. | ||
+ | |||
+ | Falls Du `mosquitto_passwd` **nicht lokal installiert** hast: | ||
+ | |||
+ | ```bash | ||
+ | docker run --rm -it -v $(pwd)/mosquitto/config:/mosquitto eclipse-mosquitto \ | ||
+ | mosquitto_passwd -c /mosquitto/passwd root | ||
+ | ``` | ||
+ | |||
+ | Dann wiederhole den Befehl ohne `-c` für weitere Benutzer. | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## ▶️ Container starten (docker run) | ||
+ | |||
+ | ```bash | ||
+ | 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_pub` und `mosquitto_sub`: | ||
+ | |||
+ | ```bash | ||
+ | # Empfänger (Subscriber) | ||
+ | mosquitto_sub -h localhost -p 1883 -t test/topic -u root -P <passwort> | ||
+ | |||
+ | # Sender (Publisher) | ||
+ | mosquitto_pub -h localhost -p 1883 -t test/topic -m "Hallo MQTT" -u user1 -P <passwort> | ||
+ | ``` | ||
+ | |||
+ | ### WebSocket-Test (optional) | ||
+ | |||
+ | Über ein MQTT WebSocket Client (z. B. MQTT.fx, MQTT Explorer, JavaScript Client): | ||
+ | |||
+ | * **Host:** `localhost` | ||
+ | * **Port:** `9001` | ||
+ | * **Pfad:** `/` (Standard bei Mosquitto) | ||
+ | * **Protokoll:** `ws://` | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## 🧠 Wichtige Hinweise zur Konfiguration | ||
+ | |||
+ | | Option | Bedeutung | | ||
+ | | ----------------------- | ------------------------------------------------------------------ | | ||
+ | | `persistence true` | Sichert MQTT-Nachrichten bei QoS > 0 dauerhaft | | ||
+ | | `allow_anonymous false` | Verhindert unautorisierte Verbindungen | | ||
+ | | `password_file` | Gibt den Pfad zur Benutzer-Passwortdatei an | | ||
+ | | `listener` | Ermöglicht parallele Ports für unterschiedliche Clients oder Netze | | ||
+ | | `protocol websockets` | Schaltet auf WebSocket-Kommunikation um | | ||
+ | | `log_dest`, `log_type` | Erlaubt gezieltes Logging für Fehleranalyse | | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## 🔁 Container stoppen und entfernen | ||
+ | |||
+ | ```bash | ||
+ | docker stop mosquitto | ||
+ | docker rm mosquitto | ||
+ | ``` | ||
+ | |||
+ | --- | ||
+ | |||
+ | ## 🧪 Optional: ACL-Dateien für Benutzerrechte | ||
+ | |||
+ | Falls Du Benutzern unterschiedliche Rechte geben willst (z. B. Lese-/Schreibrechte pro Topic), füge in der `mosquitto.conf` hinzu: | ||
+ | |||
+ | ```conf | ||
+ | acl_file /mosquitto/config/acl | ||
+ | ``` | ||
+ | |||
+ | Beispiel für `acl`: | ||
+ | |||
+ | ```conf | ||
+ | user root | ||
+ | topic readwrite # | ||
+ | |||
+ | user user1 | ||
+ | topic readwrite sensor/temperature | ||
+ | |||
+ | user user2 | ||
+ | topic read sensor/temperature | ||
+ | ``` | ||
+ | |||
+ | --- | ||
+ | |||
+ | Wenn Du möchtest, kann ich Dir eine ZIP-Datei mit diesem Setup generieren. Sag einfach Bescheid. | ||
== FMEM == | == FMEM == |
Version vom 13. Juni 2025, 12:14 Uhr
Inhaltsverzeichnis
MQTT.org
http://mqtt.org/
Mosquitto
MQTT Broker Mosquitto installieren
http://www.neuendorf-online.de/blog/heimautomatisierung/mqtt-broker-mosquitto-installieren/
Installation auf Docker
Gerne! Hier ist eine **ausführliche und gut strukturierte Anleitung im WIKI-Format** zur **Installation von Mosquitto MQTT in Docker (Variante: `docker run`)**, inklusive:
- Mehreren Benutzern (`root`, `user1` bis `user5`)
- Mehreren Ports: 1883 (Standard MQTT), 1884 (zweiter Broker-Port), 9001 (WebSockets)
- Wichtigsten Konfigurationsoptionen für produktive Umgebungen
---
- 🐳 MQTT Broker mit Mosquitto im Docker-Container installieren (`docker run` Variante)
- 🔧 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
```bash mkdir -p mosquitto/config mkdir -p mosquitto/data mkdir -p mosquitto/log ```
---
- 📝 `mosquitto.conf` erstellen
Pfad: `mosquitto/config/mosquitto.conf`
```conf
- Speicherung aktivieren
persistence true persistence_location /mosquitto/data/
- Logging konfigurieren
log_dest file /mosquitto/log/mosquitto.log log_type error log_type warning log_type notice log_type information
- Standard MQTT Port
listener 1883 allow_anonymous false password_file /mosquitto/config/passwd
- Zusätzlicher MQTT Port
listener 1884 allow_anonymous false password_file /mosquitto/config/passwd
- WebSocket-Port
listener 9001 protocol websockets allow_anonymous false password_file /mosquitto/config/passwd ```
> ⚠️ `allow_anonymous false` sorgt dafür, dass nur authentifizierte Benutzer zugelassen werden.
---
- 🔐 Benutzer erstellen
Verwende das Tool `mosquitto_passwd`, um eine Passwortdatei anzulegen:
```bash 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 ```
> Du wirst jeweils nach einem Passwort gefragt. Die Datei `passwd` wird im Format mit SHA256-Hashes gespeichert.
Falls Du `mosquitto_passwd` **nicht lokal installiert** hast:
```bash docker run --rm -it -v $(pwd)/mosquitto/config:/mosquitto eclipse-mosquitto \
mosquitto_passwd -c /mosquitto/passwd root
```
Dann wiederhole den Befehl ohne `-c` für weitere Benutzer.
---
- ▶️ Container starten (docker run)
```bash 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_pub` und `mosquitto_sub`:
```bash
- Empfänger (Subscriber)
mosquitto_sub -h localhost -p 1883 -t test/topic -u root -P <passwort>
- Sender (Publisher)
mosquitto_pub -h localhost -p 1883 -t test/topic -m "Hallo MQTT" -u user1 -P <passwort> ```
- WebSocket-Test (optional)
Über ein MQTT WebSocket Client (z. B. MQTT.fx, MQTT Explorer, JavaScript Client):
- **Host:** `localhost`
- **Port:** `9001`
- **Pfad:** `/` (Standard bei Mosquitto)
- **Protokoll:** `ws://`
---
- 🧠 Wichtige Hinweise zur Konfiguration
| Option | Bedeutung | | ----------------------- | ------------------------------------------------------------------ | | `persistence true` | Sichert MQTT-Nachrichten bei QoS > 0 dauerhaft | | `allow_anonymous false` | Verhindert unautorisierte Verbindungen | | `password_file` | Gibt den Pfad zur Benutzer-Passwortdatei an | | `listener` | Ermöglicht parallele Ports für unterschiedliche Clients oder Netze | | `protocol websockets` | Schaltet auf WebSocket-Kommunikation um | | `log_dest`, `log_type` | Erlaubt gezieltes Logging für Fehleranalyse |
---
- 🔁 Container stoppen und entfernen
```bash docker stop mosquitto docker rm mosquitto ```
---
- 🧪 Optional: ACL-Dateien für Benutzerrechte
Falls Du Benutzern unterschiedliche Rechte geben willst (z. B. Lese-/Schreibrechte pro Topic), füge in der `mosquitto.conf` hinzu:
```conf acl_file /mosquitto/config/acl ```
Beispiel für `acl`:
```conf user root topic readwrite #
user user1 topic readwrite sensor/temperature
user user2 topic read sensor/temperature ```
---
Wenn Du möchtest, kann ich Dir eine ZIP-Datei mit diesem Setup generieren. Sag einfach Bescheid.
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/