Stap 9: Installeren MQTT makelaar
Je hebt een 'bericht makelaar' [de MQTT server], welke clients publiceren en abonneren op 'Onderwerpen'. In dit geval de Raspberry Pi loopt de bericht-makelaar, en zal ook abonneren op de onderwerpen voor elk van de uitbreiding-aansluitingen.
Pre-Requitsites
Voer de volgende commando's in uw console Raspberry Pi...
cd apt-get update #always a good idea apt-get install gcc g++ make #essentials apt-get install libc-ares-dev uuid-dev daemon xsltproc docbook-xsl #mosquitto
Installeren van MQTT
Voer de volgende commando's in uw console Raspberry Pi...
cd wget <a href="http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz" rel="nofollow"> http://mosquitto.org/files/source/mosquitto-1.4.2...</a> tar zxf mosquitto-1.4.2.tar.gz cd mosquitto-1.4.2 make install ldconfig mkdir /etc/mosquitto cp mosquitto.conf /etc/mosquitto nano /etc/mosquitto/mosquitto.conf
Uncomment en bijwerken van de volgende regels in 'mosquitto.conf'...
pid_file /var/run/mosquitto.pid user mosquitto port 1883 protocol mqtt listener 9001
.. .exit de editor en sla het bestand. Naast de 'mosquitto'-gebruiker toevoegen - u wellicht een wachtwoord op te geven. Druk op enter op andere gebruikersvelden...
adduser mosquitto nano /etc/init.d/mosquitto
.. .add de volgende in de lege 'mosquitto' init script code...
#!/bin/sh ### BEGIN INIT INFO # Provides: mosquitto # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: mosquitto MQTT v3.1 message broker # Description: # This is a message broker that supports version 3.1 of the MQ Telemetry # Transport (MQTT) protocol. # # MQTT provides a method of carrying out messaging using a publish/subscribe # model. It is lightweight, both in terms of bandwidth usage and ease of # implementation. This makes it particularly useful at the edge of the network # where a sensor or other simple device may be implemented using an arduino for # example. ### END INIT INFO set -e PIDFILE=/var/run/mosquitto.pid DAEMON=/usr/local/sbin/mosquitto # /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker test -x ${DAEMON} || exit 0 umask 022 . /lib/lsb/init-functions # Are we running from init? run_by_init() { ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ] } export PATH="${PATH:+$PATH:}/usr/sbin:/sbin" case "$1" in start) log_daemon_msg "Starting Mosquitto message broker" "mosquitto" if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then log_end_msg 0 else log_end_msg 1 fi ;; stop) log_daemon_msg "Stopping Mosquitto message broker" "mosquitto" if start-stop-daemon --stop --quiet --oknodo --pidfile ${PIDFILE}; then log_end_msg 0 rm -f ${PIDFILE} else log_end_msg 1 fi ;; reload|force-reload) log_daemon_msg "Reloading configuration not supported" "mosquitto" ;; restart) log_daemon_msg "Restarting Mosquitto message broker" "mosquitto" if start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile ${PIDFILE}; then rm -f ${PIDFILE} fi if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then log_end_msg 0 else log_end_msg 1 fi ;; try-restart) log_daemon_msg "Restarting Mosquitto message broker" "mosquitto" set +e start-stop-daemon --stop --quiet --retry 30 --pidfile ${PIDFILE} RET="$?" set -e case $RET in 0) # old daemon stopped rm -f ${PIDFILE} if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then log_end_msg 0 else log_end_msg 1 fi ;; 1) # daemon not running log_progress_msg "\(not running\)" log_end_msg 0 ;; *) # failed to stop log_progress_msg "\(failed to stop\)" log_end_msg 1 ;; esac ;; status) status_of_proc -p ${PIDFILE} ${DAEMON} mosquitto && exit 0 || exit $? ;; *) log_action_msg "Usage: /etc/init.d/mosquitto \{start|stop|reload|force-reload|restart|try-restart|status\}" exit 1 esac exit 0
.. .configure het init script wilt uitvoeren bij bootup...
chown root:root /etc/init.d/mosquitto chmod +x /etc/init.d/mosquitto update-rc.d mosquitto defaults update-rc.d mosquitto enable /etc/init.d/mosquitto start
.. .en ten slotte reboot de Raspberry Pi...