Git Good @ Tech

Installing MQTT Broker(Mosquitto) on Raspberry Pi

Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for “Internet of Things” messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino. And I will show you how to install Mosquitto to RaspberryPi

  1. SSH into your Raspberry Pi. Now, you need to add the Mosquitto repository. Enter the following commands:
    sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
    sudo apt-key add mosquitto-repo.gpg.key
    sudo rm mosquitto-repo.gpg.key
  2. Next, make the repository available to apt-get. First we need to change to the apt sources list directory:
    cd /etc/apt/sources.list.d/
  3. Now install the packages list for your Raspbian.
    sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
  4. Now update apt information and install Mosquitto.
    sudo apt-get update
    sudo apt-get install mosquitto mosquitto
  5. Next we can install the three parts of Mosquitto proper.
    • mosquitto – the MQTT broker (or in other words, a server)
    • mosquitto-clients – command line clients, very useful in debugging
    • python-mosquitto – the Python language bindings
    sudo apt-get install mosquitto mosquitto-clients python-mosquitto
  6. Now the broker is immediately started.  Since we have to configure it first, stop it by command:
    sudo /etc/init.d/mosquitto stop
  7. We need to set up the configuration file. The configuration files is located at /etc/mosquitto. Open the config file:
    sudo nano /etc/mosquitto/mosquitto.conf

     
    File should look like this:

    # 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
    
    include_dir /etc/mosquitto/conf.d

    Now replace “log_dest file /v…” line with “log_dest topic”. And add following 6 lines:

    log_type error
    log_type warning
    log_type notice
    log_type information
    
    connection_messages true
    log_timestamp true

    Now the file should look like this:

    # 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 topic
    
    log_type error
    log_type warning
    log_type notice
    log_type information
    
    connection_messages true
    log_timestamp true
    
    include_dir /etc/mosquitto/conf.d
  8. Start the mosquito server:
    sudo /etc/init.d/mosquitto start
  9. Test your installation by opening 2 two terminal windows to the first insert:
    mosquitto_sub -d -t hello/world

     

  10. And to the second window insert:
    mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"

     

  11. When you have done the second statement you should see this in the Terminal 1 window:
    ~ $ sudo mosquitto_sub -d -t hello/world
    Client mosqsub/3014-LightSwarm sending CONNECT
    Client mosqsub/3014-LightSwarm received CONNACK
    Client mosqsub/3014-LightSwarm sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
    Client mosqsub/3014-LightSwarm received SUBACK
    Subscribed (mid: 1): 0
    Client mosqsub/3014-LightSwarm received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (32 bytes))
    Greetings from Terminal window 2

    On Linux version 4.4.50/ Raspbian v. 8 (jessie) and mosquitto MQTT v3.1 the mosquitto will start on boot automatically.



4 Comments

  1. Mihai

    At step 4 I am getting:

    pi@raspberrypi:/etc/apt/sources.list.d $ sudo apt-get install mosquitto mosquitto
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    mosquitto : Depends: libssl1.0.0 (>= 1.0.1) but it is not installable
    Depends: libwebsockets3 (>= 1.2) but it is not installable
    E: Unable to correct problems, you have held broken packages.

  2. Doug

    Raspbian Jessie
    mosquitto 1.6.8

    wget http://ftp.us.debian.org/debian/pool/main/libw/libwebsockets/libwebsockets3_1.2.2-1_armhf.deb
    sudo apt-get install mosquitto

    worked

Leave a Reply to BigJay Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 JakeMakes

Theme by Anders NorenUp ↑