Coding Chase - Projects

My 3D Hub

Thank You For Donating:

Ikeeki (linux-sunxi@irc.freenode.net)
Computer Troubleshooters Brasil

Sunday 12 July 2015

IoT Projects - MQTT

As a starting point, I've been looking at a couple of message protocols in order to interact with my embedded devices.

One protocol in particular stood out - MQTT

Basically you'll need to set up a message server (or broker) which will then manage the message queues your clients will be using.

That in turn led me to Mosquitto. Installing Mosquitto on an Ubuntu machine is easy enough, just follow these steps and you're good to go.

Moving on to my embedded boards, my Cubietruck looked like it could handle it - already running Gitlab, Octoprint and a couple more, so why not?

I'm using Cubian, and unfortunately the repo available package for Mosquitto is too old (2012), so I had to install Mosquitto from source:

wget http://mosquitto.org/files/source/mosquitto-1.4.2.tar.gz
sudo apt-get install libc-ares-dev

sudo apt-get install uuid-dev

sudo useradd mosquitto
sudo mkdir /var/log/mosquitto
sudo chown mosquitto:root /var/log/mosquitto/


tar xvzpf mosquitto-1.4.2.tar.gz
cd mosquitto-1.4.2
make clean && make -j3
sudo make install



/etc/mosquitto/mosquitto.conf:


# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

 

/etc/init.d/mosquitto:

#! /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:/usr/local/sbin"

case "$1" in
  start)
    if init_is_upstart; then
        exit 1
    fi
    log_daemon_msg "Starting network daemon:" "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)
    if init_is_upstart; then
        exit 0
    fi
    log_daemon_msg "Stopping network daemon:" "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)
    if init_is_upstart; then
        exit 1
    fi
    log_daemon_msg "Reloading network daemon configuration:" "mosquitto"
        if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PIDFILE; then
            log_end_msg 0
        else
            log_end_msg 1
        fi   
    ;;

  restart)
    if init_is_upstart; then
        exit 1
    fi
    log_daemon_msg "Restarting network daemon:" "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)
    if init_is_upstart; then
        exit 1
    fi
    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)
    if init_is_upstart; then
        exit 1
    fi
    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


cubie@Cubian:~$ sudo service mosquitto status
[FAIL] mosquitto is not running ... failed!
cubie@Cubian:~$ sudo service mosquitto start
[ ok ] Starting network daemon:: mosquitto.
cubie@Cubian:~$ sudo service mosquitto status
[ ok ] mosquitto is running.
cubie@Cubian:~$

No comments :

Post a Comment