Setup MQTT Portal
curl -sSL https://get.docker.com | sh
sudo usermod -aG docker $USER
sudo mkdir -p /opt/mqtt
cd /opt/mqtt
sudo vim compose.yaml
services:
mosquitto:
image: eclipse-mosquitto
container_name: mosquitto
volumes:
- ./config:/mosquitto/config
- ./data:/mosquitto/data
- ./log:/mosquitto/log
ports:
- 1883:1883
- 9001:9001 # websocket
restart: unless-stopped
stdin_open: true
tty: true
mkdir ./config
sudo nano ./config/mosquitto.conf
listener 1883
listener 9001
protocol websockets
persistence true
persistence_file mosquitto.db
persistence_location /mosquitto/data/
# log_dest file /mosquitto/log/mosquitto.log
#Authentication
allow_anonymous false
password_file /mosquitto/config/pwfile
#ADD LOGGING
sudo touch ./config/pwfile
sudo chmod 0700 ./config/pwfile
docker compose up -d
docker compose exec mosquitto sh
mosquitto_passwd -c /mosquitto/config/pwfile <USERNAME>
exit
docker compose restart mosquitto
# Without authentication
mosquitto_pub -t 'hello/topic' -m 'hello MQTT'
# With authentication
mosquitto_pub -t 'hello/topic' -m 'hello MQTT' -u user1 -P <password>
# Alternate way in url format
# Format => mqtt(s)://[username[:password]@]host[:port]/topic
mosquitto_pub -L mqtt://user1:abc123@localhost/test/topic -m 'hello MQTT'
Check main.cpp for the mosquitto client code.
Read more about it here => https://mqttx.app/
sudo docker run -d --name mqttx-web -p 80:80 emqx/mqttx-web
#https://manpages.debian.org/testing/systemd/systemd.service.5.en.html
ExecStart =mosquitto_sub -v -t "topic/name" >> mylog.mqtt
#and enable it with e.g.
sudo systemctl enable path/to/mylogmqtt.service
docker pull eclipse-mosquitto
docker run -it --entrypoint /bin/sh eclipse-mosquitto
``