Mosquitto

/MQTT

Mosquitto/MQTT


I use Mosquitto for my MQTT broker. A MQTT broker is like a server that is responsible for relaying MQTT messages to any device on my MQTT network.

I have Pi's, PC's and ESP's that all talk MQTT to each other. Fairly simple interface, once you figure out how it works, and very robust.


docker exec mosquitto mosquitto_passwd -b /mosquitto/pwfile/pwfile hello world

Installation

I installed in Docker with Docker compose


Installed per https://www.diyhobi.com/install-mqtt-and-openhab-3-in-docker-raspberry-pi-4/

Installation directory is ~/IOTstack/smarthome/mqtt


cd ~/IOTstack

mkdir smarthome

mkdir smarthome/mqtt

mkdir smarthome/mqtt/config

cd smarthome/mqtt/config

Make config file

nano mosquitto.conf

paste this into the file

# Config file for mosquitto

listener 1883

persistence true

persistence_location /mosquitto/data/

log_dest file /mosquitto/log/mosquitto.log

allow_anonymous false


Make the docker compose file

cd ~/IOTstack/smarthome


sudo nano docker-compose.yaml

Paste:

version: '3'


services:

#mqtt

mosquitto:

container_name: mqtt

#hostname: mosquitto

restart: always

image: eclipse-mosquitto

ports:

- "1883:1883"

- "9001:9001"

volumes:

- ./mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf

- ./mqtt/data:/mosquitto/data

- ./mqtt/log:/mosquitto/log

networks:

- default


Run docker compose

docker-compose up -d

To check if it is running:

docker ps


Run a new command in the running MQTT container to setup username and password

docker exec -it mqtt sh

Now that we are the MQTT container command, we can change the username and password of MQTT.

  • Username: mymqtt

  • Password: mypassword

mosquitto_passwd -c /mosquitto/data/pwfile mymqtt

You will be asked to enter a password twice.

exit the command:

exit

To finish configuration of Mosquitto, we have to stop the container first

docker stop mqtt

Edit Mosquitto config file:

sudo nano ~/docker/smarthome/mqtt/config/mosquitto.conf


Paste this at the bottom

password_file /mosquitto/data/pwfile



Start the container

docker start mqtt

docker ps

Should work now


Old stuff for history

run

cd ~/IOTstack

./menu.sh

Select "Build Stack", then Eclipse-Mosquitto

Exit


docker-compose up -d

cd ~/IOTstack/services/mosquitto

Type this command to start a shell to enter password for username

sh terminal.sh

After shell starts, use this to enter username/password

mosquitto_passwd -c /mosquitto/config/pwfile USERNAME


Enter password twice.

Restart and it worked



Sniffer

Highly recommend finding a MQTT sniffer to help troubleshoot problems when things don't work

One big advantage to MQTT is that you can use a sniffer, and see if data is getting transmitter. So you can isolate problem to either transmit or receive side.

I've been using MQTT Lens for chrome

But there are many others, for phones or PCs.

With any sniffer, you will need to setup to talk to you MQTT broker.

Hostname is ip address of the pi or whatever device Mosquitto is running on. User name and password are what you setup for Mosquitto. Should connect if you enter everything in correctly.

Then you can subscribe (receive) or publish (tx) to topics to see or send MQTT traffic