Come installare MQTT su Raspberry con Debian Stretch

Il broker MQTT utilizzato su Linux e' mosquitto. In questo note si mostra come installarlo dai repository Debian invece che da repository specializzati come suggerito in rete da parecchi tutorial obsoleti.

Installazione

Come prima cosa conviene aggiornare il sistema apt:

sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

I due packages necessari per un uso generale sono mosquitto e mosquitto-clients, se si necessita solo del broker non installare mosquitto-clients.

sudo apt-get -y install mosquitto mosquitto-clients
Reading package lists... Done
Building dependency tree        
Reading state information... Done
The following additional packages will be installed:
  libc-ares2 libev4 libmosquitto1 libuv1 libwebsockets8
Suggested packages:
  apparmor
The following NEW packages will be installed:
  libc-ares2 libev4 libmosquitto1 libuv1 libwebsockets8 mosquitto mosquitto-clients
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 485 kB of archives.
After this operation, 1,055 kB of additional disk space will be used.
Get:1 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf libev4 armhf 1:4.22-1 [34.0 kB]
Get:2 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf libc-ares2 armhf 1.12.0-1+deb9u1 [76.0 kB]
Get:3 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf libmosquitto1 armhf 1.4.10-3+deb9u1 [46.4 kB]
Get:4 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf libuv1 armhf 1.9.1-3 [73.2 kB]
Get:5 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf libwebsockets8 armhf 2.0.3-2 [85.1 kB]
Get:6 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf mosquitto armhf 1.4.10-3+deb9u1 [120 kB]
Get:7 http://raspbian.mirror.garr.it/mirrors/raspbian/raspbian stretch/main armhf mosquitto-clients armhf 1.4.10-3+deb9u1 [51.3 kB]
Fetched 485 kB in 15s (31.3 kB/s)       
Selecting previously unselected package libev4.
(Reading database ... 34533 files and directories currently installed.)
Preparing to unpack .../0-libev4_1%3a4.22-1_armhf.deb ...
Unpacking libev4 (1:4.22-1) ...
Selecting previously unselected package libc-ares2:armhf.
Preparing to unpack .../1-libc-ares2_1.12.0-1+deb9u1_armhf.deb ...
Unpacking libc-ares2:armhf (1.12.0-1+deb9u1) ...
Selecting previously unselected package libmosquitto1:armhf.
Preparing to unpack .../2-libmosquitto1_1.4.10-3+deb9u1_armhf.deb ...
Unpacking libmosquitto1:armhf (1.4.10-3+deb9u1) ...
Selecting previously unselected package libuv1:armhf.
Preparing to unpack .../3-libuv1_1.9.1-3_armhf.deb ...
Unpacking libuv1:armhf (1.9.1-3) ...
Selecting previously unselected package libwebsockets8:armhf.
Preparing to unpack .../4-libwebsockets8_2.0.3-2_armhf.deb ...
Unpacking libwebsockets8:armhf (2.0.3-2) ...
Selecting previously unselected package mosquitto.
Preparing to unpack .../5-mosquitto_1.4.10-3+deb9u1_armhf.deb ...
Unpacking mosquitto (1.4.10-3+deb9u1) ...
Selecting previously unselected package mosquitto-clients.
Preparing to unpack .../6-mosquitto-clients_1.4.10-3+deb9u1_armhf.deb ...
Unpacking mosquitto-clients (1.4.10-3+deb9u1) ...
Setting up libev4 (1:4.22-1) ...
Setting up libuv1:armhf (1.9.1-3) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for systemd (232-25+deb9u2) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up libc-ares2:armhf (1.12.0-1+deb9u1) ...
Setting up libwebsockets8:armhf (2.0.3-2) ...
Setting up mosquitto (1.4.10-3+deb9u1) ...
Setting up libmosquitto1:armhf (1.4.10-3+deb9u1) ...
Setting up mosquitto-clients (1.4.10-3+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for systemd (232-25+deb9u2) ...
pi@raspberrypi:~ $ which mosquitto_pub
/usr/bin/mosquitto_pub

Debian Stretch contiene diversi packages per mosquitto.

  • mosquitto
  • mosquitto-clients

mosquitto contiene il solo broker (cioe' il processo daemon), quindi almeno questo pacchetto deve essere installato.

dpkg -L mosquitto
/.
/etc
/etc/init
/etc/init/mosquitto.conf
/etc/init.d
/etc/init.d/mosquitto
/etc/logrotate.d
/etc/logrotate.d/mosquitto
/etc/mosquitto
/etc/mosquitto/ca_certificates
/etc/mosquitto/ca_certificates/README
/etc/mosquitto/certs
/etc/mosquitto/certs/README
/etc/mosquitto/conf.d
/etc/mosquitto/conf.d/README
/etc/mosquitto/mosquitto.conf
/usr
/usr/bin
/usr/bin/mosquitto_passwd
/usr/sbin
/usr/sbin/mosquitto
/usr/share
/usr/share/doc
/usr/share/doc/mosquitto
/usr/share/doc/mosquitto/README.Debian
/usr/share/doc/mosquitto/changelog.Debian.gz
/usr/share/doc/mosquitto/changelog.gz
/usr/share/doc/mosquitto/copyright
/usr/share/doc/mosquitto/examples
/usr/share/doc/mosquitto/examples/aclfile.example
/usr/share/doc/mosquitto/examples/mosquitto.conf.gz
/usr/share/doc/mosquitto/examples/pskfile.example
/usr/share/doc/mosquitto/examples/pwfile.example
/usr/share/doc/mosquitto/readme.md
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/mosquitto_passwd.1.gz
/usr/share/man/man5
/usr/share/man/man5/mosquitto.conf.5.gz
/usr/share/man/man7
/usr/share/man/man7/mosquitto-tls.7.gz
/usr/share/man/man7/mqtt.7.gz
/usr/share/man/man8
/usr/share/man/man8/mosquitto.8.gz
/var
/var/lib
/var/lib/mosquitto
/var/log
/var/log/mosquitto

/etc/init.d/mosquitto contiene lo script di lancio (vecchia maniera, SystemV). Nei sistemi moderni che utilizzano systemd (come Debian Stretch) viene automaticamente generato un service che, usando lo script SystemV, gestisce il daemon.

Il package mosquitto-clients contiene i programmi di utilita'

pi@raspberrypi:~ $ dpkg -L mosquitto-clients
/.
/usr
/usr/bin
/usr/bin/mosquitto_pub
/usr/bin/mosquitto_sub
/usr/share
/usr/share/doc
/usr/share/doc/mosquitto-clients
/usr/share/doc/mosquitto-clients/changelog.Debian.gz
/usr/share/doc/mosquitto-clients/changelog.gz
/usr/share/doc/mosquitto-clients/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/mosquitto-clients
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/mosquitto_pub.1.gz
/usr/share/man/man1/mosquitto_sub.1.gz

Tutti i pacchetti relativi a mosquittoin Debian Stretch sono elencati qui.

libmosquitto-dev
MQTT version 3.1/3.1.1 client library, development files
libmosquitto1
MQTT version 3.1/3.1.1 client library
libmosquitto1-dbg
debugging symbols for libmosquitto binaries
libmosquittopp-dev
MQTT version 3.1 client C++ library, development files
libmosquittopp1
MQTT version 3.1/3.1.1 client C++ library
libmosquittopp1-dbg
debugging symbols for libmosquittopp binaries
mosquitto
MQTT version 3.1/3.1.1 compatible message broker
mosquitto-clients
Mosquitto command line MQTT clients
mosquitto-dbg
debugging symbols for mosquitto binaries
mosquitto-dev
Development files for Mosquitto 

Sistema operativo

Come al solito un file vuoto con nome ssh e' stato creato nella partizione di boot per abilitare il server SSH , necessario nel funzionamento headless.

La riga

enable_uart=1

e' stata aggiunta alla fine del file /boot/config.txt per abilitare la seriale della console.

Network config

Per attivare l'interfaccia wireless modificare come superutente il file /etc/network/interfaces nel modo seguente:

auto lo
iface lo inet loopback

iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Per configurare la propria rete wireless configurare il file /etc/wpa_supplicant/wpa_supplicant.conf come da esempio qui sotto, sostituendo i valori appropriati:

country=IT
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SSID_NAME"
    scan_ssid=1
    psk="KEY"
    key_mgmt=WPA-PSK
    }

Per attivare le modifiche sopra riportate i comandi sono i seguenti:

sudo systemctl enable wpa_supplicant.service
sudo systemctl start wpa_supplicant.service
sudo systemctl stop networking
sudo systemctl start networking

Come viene attivato il daemon mosquitto

Guardando alla lista dei files del package mosquitto non si trova il file di service necessario per l'integrazione in systemd.

C'e' invece il classico file per il sistema rc (System V):

/etc/init.d/mosquitto

questo e' il solito script linkato nelle diverse directory sotto /etc/rc.d. In effetti anche su Stretch il sottosistema rc sembra essere presente, almeno a giudicare dalla presenza dei files di configurazione.

Il service mosquitto funziona ma ha come status "generated":

pi@raspberrypi:~ $ systemctl list-unit-files --type=service | grep mosqu mosquitto.service generated

In realtà i files sono presenti ma sono utilizzati dal systemd per generare automaticamente dei files .service

cat  /run/systemd/generator.late/mosquitto.service 
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/mosquitto
Description=LSB: mosquitto MQTT v3.1 message broker
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
After=remote-fs.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mosquitto start
ExecStop=/etc/init.d/mosquitto stop
ExecReload=/etc/init.d/mosquitto reload
pi@raspberrypi:~ $ systemctl status mosquitto
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
   Loaded: loaded (/etc/init.d/mosquitto; generated; vendor preset: enabled)
   Active: active (running) since Wed 2018-04-18 21:01:34 CEST; 1 day 1h ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/mosquitto.service
           └─408 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Apr 18 21:01:33 raspberrypi systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
Apr 18 21:01:34 raspberrypi mosquitto[328]: Starting network daemon:: mosquitto.
Apr 18 21:01:34 raspberrypi systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.

Links

Andrea Montefusco
Currently employed as network architect, always Internet working man, real C/C++ programmer in the past, network and Unix system engineer as needed, HAM Radio enthusiast (former IW0RDI, now IW0HDV), aeromodeller (a person who builds and flies model airplanes) since 1976 (ex FAI10655).
http://www.montefusco.com - https://github.com/amontefusco - https://github.com/IW0HDV - andrew@montefusco.com