Skip to content

Latest commit

 

History

History
155 lines (131 loc) · 3.35 KB

raspberry-pi-mosquitto-websocket.md

File metadata and controls

155 lines (131 loc) · 3.35 KB

<- back

Mosquitto with websocket on Raspberry Pi

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).

Step by step installation

Step 1: dependencies installation

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

Step 2: libwebsockets compilation

git clone https://github.com/warmcat/libwebsockets.git
cd libwebsockets
mkdir build
cd build
cmake .. && sudo make install && sudo ldconfig

Step 3: download mosquitto

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

Step 4: change in config WITH_WEBSOCKETS:=yes

Proceed with:

sudo nano config.mk

Step 5: mosquitto compilation

make && sudo make install
sudo cp mosquitto.conf /etc/mosquitto

Step 6: add user “mosquitto”

useradd -r -m -d /var/lib/mosquitto -s /usr/sbin/nologin -g nogroup mosquitto

Step 7: setup mosquitto options

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

Step 8: create a link

sudo ln -s /usr/local/sbin/mosquitto /bin/mosquitto

Step 9: reboot

sudo reboot

Start mosquitto manually

mosquitto -c /etc/mosquitto/mosquitto.conf

Start mosquitto as a service on Raspberry boot

1. Edit “mosquitto.service” file

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.

2. Activate the service

sudo systemctl enable mosquitto
sudo systemctl start mosquitto

3. From now on, mosquitto will start at boot time

sudo reboot

4. Check eveything is okay

pidof mosquitto
mosquitto -v

Credits

This how-to has been written with the help of:

Author

Faure Systems (Oct 9th, 2019)