#Come installare MQTT su Raspberry con Debian Stretch <abstract> Il broker MQTT utilizzato su Linux e' [mosquitto](https://mosquitto.org/). In questo note si mostra come installarlo dai repository Debian invece che da repository specializzati come suggerito in rete da parecchi tutorial obsoleti. </abstract> ##Installazione Come prima cosa conviene aggiornare il sistema apt: <pre class="terminal"> <b>sudo apt-get upgrade</b> 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. </pre> I due packages necessari per un uso generale sono mosquitto e mosquitto-clients, se si necessita solo del broker non installare mosquitto-clients. <pre class="terminal"> <b>sudo apt-get -y install mosquitto mosquitto-clients</b> 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) ... </pre> <pre class="terminal"> pi@raspberrypi:~ $ which mosquitto_pub /usr/bin/mosquitto_pub </pre> 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. <pre class="terminal"> dpkg -L mosquitto /. /etc /etc/init /etc/init/mosquitto.conf /etc/init.d <b>/etc/init.d/mosquitto</b> /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 <b>/usr/sbin/mosquitto</b> /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 </pre> /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' <pre class="terminal"> pi@raspberrypi:~ $ dpkg -L mosquitto-clients /. /usr /usr/bin <b>/usr/bin/mosquitto_pub</b> <b>/usr/bin/mosquitto_sub</b> /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 </pre> Tutti i pacchetti relativi a mosquittoin Debian Stretch sono elencati [qui](https://packages.debian.org/source/stretch/mosquitto). 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 <pre> enable_uart=1 </pre> 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: <pre> 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 </pre> Per configurare la propria rete wireless configurare il file /etc/wpa_supplicant/wpa_supplicant.conf come da esempio qui sotto, sostituendo i valori appropriati: <pre> 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 } </pre> Per attivare le modifiche sopra riportate i comandi sono i seguenti: <pre> sudo systemctl enable wpa_supplicant.service sudo systemctl start wpa_supplicant.service sudo systemctl stop networking sudo systemctl start networking </pre> ##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): <b>/etc/init.d/mosquitto</b> 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 <pre> 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=<b>/etc/init.d/mosquitto start</b> ExecStop=/etc/init.d/mosquitto stop ExecReload=/etc/init.d/mosquitto reload </pre> <pre> 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. </pre> ## Links * [How To Use Systemctl to Manage Systemd Services and Units](https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units) * [How does systemd use /etc/init.d scripts?](https://unix.stackexchange.com/questions/233468/how-does-systemd-use-etc-init-d-scripts) * [systemd sysv init compatibility mode: how it works and troubleshooting when it breaks](https://www.turnkeylinux.org/blog/debugging-systemd-sysv-init-compat) @include='bio_andrea_montefusco'
2018 Ⓒ TanzoLab