Ga naar inhoud

πŸ“‘ Mosquitto MQTT

Mosquitto is een lichtgewicht MQTT message broker voor IoT communicatie.

Algemene Info

Item Waarde
Type LXC Container op Proxmox
IP Adres 192.168.20.11
VLAN 20 (IoT)
Poort 1883
Status πŸ”§ Op te zetten

Wat is MQTT?

MQTT (Message Queuing Telemetry Transport) is een lightweight publish-subscribe protocol, ideaal voor IoT:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     publish     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     subscribe    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Sensor    β”‚ ──────────────► β”‚  Mosquitto  β”‚ ────────────────►│    Home     β”‚
β”‚  (temp)     β”‚   topic:        β”‚   Broker    β”‚   topic:         β”‚  Assistant  β”‚
β”‚             β”‚   home/temp     β”‚             β”‚   home/temp      β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Functies

  • βœ… Publish/Subscribe - Asynchroon berichtenverkeer
  • βœ… Topics - HiΓ«rarchische berichtorganisatie
  • βœ… QoS Levels - Betrouwbaarheid opties
  • βœ… Retained Messages - Laatst bekende waarde
  • βœ… Will Messages - Offline detectie
  • βœ… Authentication - Gebruikersnaam/wachtwoord

Configuratie

/etc/mosquitto/conf.d/default.conf

# Luister op alle interfaces
listener 1883

# Authenticatie vereist
allow_anonymous false
password_file /etc/mosquitto/passwd

# Logging
log_dest file /var/log/mosquitto/mosquitto.log
log_type all

# Persistence
persistence true
persistence_location /var/lib/mosquitto/

Gebruiker Aanmaken

# Eerste gebruiker (maakt bestand aan)
mosquitto_passwd -c /etc/mosquitto/passwd mqtt_user

# Extra gebruiker toevoegen
mosquitto_passwd /etc/mosquitto/passwd andere_user

# Service herstarten
systemctl restart mosquitto

Clients

Home Assistant

# configuration.yaml
mqtt:
  broker: 192.168.20.11
  port: 1883
  username: mqtt_user
  password: !secret mqtt_password

Frigate

# config.yml
mqtt:
  host: 192.168.20.11
  port: 1883
  user: mqtt_user
  password: mqtt_password

Zigbee2MQTT

# configuration.yaml
mqtt:
  server: mqtt://192.168.20.11:1883
  user: mqtt_user
  password: mqtt_password

Topics Structuur

Aanbevolen HiΓ«rarchie

home/
β”œβ”€β”€ livingroom/
β”‚   β”œβ”€β”€ temperature
β”‚   β”œβ”€β”€ humidity
β”‚   └── light/
β”‚       β”œβ”€β”€ state
β”‚       └── brightness
β”œβ”€β”€ kitchen/
β”‚   └── ...
└── frigate/
    β”œβ”€β”€ events
    └── available

Zigbee2MQTT Topics

zigbee2mqtt/
β”œβ”€β”€ bridge/
β”‚   β”œβ”€β”€ state
β”‚   β”œβ”€β”€ devices
β”‚   └── log
└── [device_name]/
    β”œβ”€β”€ state
    β”œβ”€β”€ brightness
    └── ...

Testing

Publish Bericht

# Vanuit container of client met mosquitto-clients
mosquitto_pub -h 192.168.20.11 -u mqtt_user -P wachtwoord \
  -t "home/test" -m "Hello World"

Subscribe op Topic

mosquitto_sub -h 192.168.20.11 -u mqtt_user -P wachtwoord \
  -t "home/#" -v

Alle Berichten Zien

mosquitto_sub -h 192.168.20.11 -u mqtt_user -P wachtwoord \
  -t "#" -v

Firewall Regels

MQTT moet bereikbaar zijn voor:

Client IP Poort Regel
Home Assistant 192.168.20.10 1883 Zelfde VLAN
Frigate 192.168.1.15 1883 Allow Management to IoT
Zigbee2MQTT 192.168.20.12 1883 Zelfde VLAN
Z-Wave JS 192.168.20.13 1883 Zelfde VLAN

Monitoring

Status Checken

systemctl status mosquitto

Logs Bekijken

tail -f /var/log/mosquitto/mosquitto.log

Actieve Connecties

# Als je SYS topics hebt ingeschakeld
mosquitto_sub -h localhost -t '$SYS/#' -v

Troubleshooting

Connection refused

Symptomen

  • Client kan niet verbinden
  • "Connection refused" error

Checklist:

  1. Service draait:

    systemctl status mosquitto
    

  2. Luistert op juiste poort:

    netstat -tlnp | grep 1883
    

  3. Credentials correct:

    # Test lokaal
    mosquitto_pub -h localhost -u mqtt_user -P wachtwoord -t test -m test
    

  4. Firewall:

    # Kan client bij poort 1883?
    nc -zv 192.168.20.11 1883
    

Authentication error

Symptomen

  • "Not authorized"
  • "Bad username or password"

Oplossing:

  1. Check password file:

    cat /etc/mosquitto/passwd
    

  2. Regenereer wachtwoord:

    mosquitto_passwd /etc/mosquitto/passwd mqtt_user
    systemctl restart mosquitto
    

Berichten komen niet aan

Debug stappen:

  1. Subscribe op alle topics:

    mosquitto_sub -h 192.168.20.11 -u mqtt_user -P wachtwoord -t "#" -v
    

  2. Check of publisher werkt:

    mosquitto_pub -h 192.168.20.11 -u mqtt_user -P wachtwoord -t test -m "debug"
    

  3. Check topic spelling (case-sensitive!)

Hoog geheugengebruik

Oorzaken: - Te veel retained messages - Grote payloads

Oplossing:

# Clear retained messages
mosquitto_pub -h localhost -t "topic" -n -r

# Of herstart met schone database
systemctl stop mosquitto
rm /var/lib/mosquitto/mosquitto.db
systemctl start mosquitto

LXC Container Setup

Container Aanmaken

# Op Proxmox
pct create 102 local:vztmpl/debian-12-standard_12.0-1_amd64.tar.zst \
  --hostname mosquitto \
  --memory 512 \
  --cores 1 \
  --net0 name=eth0,bridge=vmbr0,tag=20,ip=192.168.20.11/24,gw=192.168.20.1 \
  --storage local-lvm \
  --rootfs local-lvm:4

Installatie

pct start 102
pct enter 102

apt update && apt install -y mosquitto mosquitto-clients
systemctl enable mosquitto

# Configureren
nano /etc/mosquitto/conf.d/default.conf
mosquitto_passwd -c /etc/mosquitto/passwd mqtt_user
systemctl restart mosquitto

Gerelateerd