Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MQTT Essentials - A Lightweight IoT Protocol

You're reading from   MQTT Essentials - A Lightweight IoT Protocol Send and receive messages with the MQTT protocol for your IoT solutions.

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781787287815
Length 280 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

Preface 1. Lightweight Messaging with MQTT 3.1.1 and Mosquitto FREE CHAPTER 2. Securing an MQTT Mosquitto Server 3. Controlling a Drone with Python 4. Interacting with Sensors in Java 5. Controlling Home Automation in JavaScript 6. Interacting with Actuators in Swift A. Exersise Answers

Installing a Mosquitto broker on Ubuntu Linux

Now, we will learn the necessary steps to install a Mosquitto broker, also known as Mosquitto MQTT server on the most popular operating systems: Linux, macOS and Windows. First, we will start with Linux; specifically, we will work with Ubuntu Linux. In case you want to work with a different Linux distribution, you can find details about the installation procedure in the Mosquitto downloads section: http://mosquitto.org/download.

Follow these steps to install a Mosquitto broker on Ubuntu Linux. Take into account that you will require root privileges:

  1. Open a Terminal window and run the following command to add the Mosquitto repository:
          sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
    

    After you enter the password, you will see an output similar to the next lines (the temporary file names will be different).

          gpg: keyring `/tmp/tmpj3a50p1p/secring.gpg' created
          gpg: keyring `/tmp/tmpj3a50p1p/pubring.gpg' created
          gpg: requesting key 262C4500 from hkp server keyserver.ubuntu.com
          gpg: /tmp/tmpj3a50p1p/trustdb.gpg: trustdb created
          gpg: key 262C4500: public key "Launchpad mosquitto" imported
          gpg: Total number processed: 1
          gpg:               imported: 1  (RSA: 1)
          OK
    
  2. Run the following command to update the packages with the recently added Mosquitto repository.
          sudo apt-get update
    

    You will see an output similar to the next lines.

          Get:1 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/      ubuntu xenial InRelease [23,8 kB]
          Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
          Hit:3 http://ar.archive.ubuntu.com/ubuntu xenial InRelease
          Hit:4 http://ar.archive.ubuntu.com/ubuntu xenial-updates InRelease
          Get:5 http://ppa.launchpad.net/mosquitto-dev/      mosquitto-ppa/ubuntu xenial/main amd64 Packages [2.048 B]
          Hit:6 http://ar.archive.ubuntu.com/ubuntu xenial-backports InRelease
          Get:7 http://ppa.launchpad.net/mosquitto-dev/      mosquitto-ppa/ubuntu xenial/main i386 Packages [2.048 B]
          Get:8 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/      ubuntu xenial/main Translation-en [1.292 B]
          Fetched 29,2 kB in 1s (21,5 kB/s)
          Reading package lists... Done
    
  3. Now, run the following command to install the package for the Mosquitto server.
          sudo apt-get install mosquitto
    

    You will see an output similar to the next lines. Enter Y and press Enter to answer the question and complete the installation process.

          Reading package lists... Done
          Building dependency tree
          Reading state information... Done
          The following additional packages will be installed:
            libev4 libuv1 libwebsockets7
          The following NEW packages will be installed:
            libev4 libuv1 libwebsockets7 mosquitto
          0 upgraded, 4 newly installed, 0 to remove and 299 not upgraded.
          8 not fully installed or removed.
          Need to get 274 kB of archives.
          After this operation, 714 kB of additional disk space will be used.
          Do you want to continue? [Y/n]
    

    The last lines should include a line that says Setting up mosquitto, as shown in the following lines:

          Setting up mosquitto (1.4.10-0mosquitto3) ...
          Processing triggers for libc-bin (2.23-0ubuntu3) ...
          Processing triggers for systemd (229-4ubuntu7) ...
          Processing triggers for ureadahead (0.100.0-19) ...
    
  4. Run the following command to install the Mosquitto client packages that will allow us to run commands to publish messages to topics and subscribe to topic filters.
          sudo apt-get install mosquitto-clients
    

    You will see an output similar to the next lines. Enter Y and press Enter to answer the question and complete the installation process.

          Reading package lists... Done
          Building dependency tree
          Reading state information... Done
          The following additional packages will be installed:
            libc-ares2 libmosquitto1
          The following NEW packages will be installed:
            libc-ares2 libmosquitto1 mosquitto-clients
          0 upgraded, 3 newly installed, 0 to remove and 299 not upgraded.
          Need to get 138 kB of archives.
          After this operation, 332 kB of additional disk space will be used.
          Do you want to continue? [Y/n]
    

    The last lines should include a line that says Setting up mosquitto-clients, as shown in the following lines:

          Setting up libmosquitto1:amd64 (1.4.10-0mosquitto3) ...
          Setting up mosquitto-clients (1.4.10-0mosquitto3) ...
          Processing triggers for libc-bin (2.23-0ubuntu3) ...
    
  5. Run the following command to check the status for the mosquitto service that was recently installed.
          sudo service mosquitto status
    

    The first lines of the output should be similar to the following lines with an active (running) status displayed. The details after CGroup indicate the command-line that started the service. The -c option followed by /etc/mosquitto/mosquitto.conf specifies that Mosquitto is using this configuration file.

          mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
             Loaded: loaded (/etc/init.d/mosquitto; bad;       vendor preset: enabled)
             Active: active (running)
               Docs: man:systemd-sysv-generator(8)
             CGroup: /system.slice/mosquitto.service
                     └─17560 /usr/sbin/mosquitto -c       /etc/mosquitto/mosquitto.conf
    

    You can also run the following command to check whether the Mosquitto MQTT server is listening at the default port: 1883.

          netstat -an | grep 1883
    

    The following lines show the results of the previous command that indicate the Mosquitto MQTT server has opened an IPv4 and an IPv6 listen socket on port 1883:

          tcp        0      0 0.0.0.0:1883    0.0.0.0:*    LISTEN
          tcp6       0      0 :::1883         :::*         LISTEN
    
You have been reading a chapter from
MQTT Essentials - A Lightweight IoT Protocol
Published in: Apr 2017
Publisher: Packt
ISBN-13: 9781787287815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image