Standard installation of mosquitto MQTT broker on Raspberry Pi does not support websocket (necessary to receive messages for web browsers such as Chrome or Firefox).
sudo apt-get --assume-yes install \
build-essential \
python \
quilt \
devscripts \
python-setuptools \
python3 \
libssl-dev \
cmake \
libc-ares-dev \
uuid-dev \
daemon \
zlibc \
zlib1g \
zlib1g-dev
git clone https://github.com/warmcat/libwebsockets.git
cd libwebsockets
mkdir build
cd build
cmake .. && sudo make install && sudo ldconfig
mkdir ~/mosquitto
cd ~/mosquitto/
MOSQUITTO_VER=mosquitto-1.4.14
wget https://mosquitto.org/files/source/$MOSQUITTO_VER.tar.gz
tar zxvf $MOSQUITTO_VER.tar.gz
cd $MOSQUITTO_VER
Proceed with:
sudo nano config.mk
make && sudo make install
sudo cp mosquitto.conf /etc/mosquitto
useradd -r -m -d /var/lib/mosquitto -s /usr/sbin/nologin -g nogroup mosquitto
sudo nano /etc/mosquitto/mosquitto.conf
Find:
port 1883
listener 9001
protocol websockets
pid_file /var/run/mosquitto.pid
Optionally add a security layer creating a password file:
mosquitto_passwd -c /etc/mosquitto/passwd yourloginname
sudo nano /etc/mosquitto/mosquitto.conf
allow_anonymous false
sudo ln -s /usr/local/sbin/mosquitto /bin/mosquitto
sudo reboot
mosquitto -c /etc/mosquitto/mosquitto.conf
sudo nano /etc/systemd/system/mosquitto.service
Add:
[Unit]
Description=Mosquitto MQTT Broker daemon
After=network.target
Requires=network.target
[Service]
Type=forking
RemainAfterExit=no
StartLimitInterval=0
PIDFile=/var/run/mosquitto.pid
# Note: The following paths may be different on your system.
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=2
[Install]
WantedBy=multi-user.target
See https://goo.gl/wMCZFv for more.
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
sudo reboot
pidof mosquitto
mosquitto -v
This how-to has been written with the help of:
- Installer Mosquitto MQTT avec WebSocket sur un Raspberry Pi
- INSTALLING MOSQUITTO MQTT BROKER ON RASPBERRY PI (WITH WEBSOCKETS)
Faure Systems (Oct 9th, 2019)
- company: FAURE SYSTEMS SAS
- mail: dev at faure dot systems
- github: fauresystems
- web: Faure Systems