#18 Raspberry Pi: Install and Test Mosquitto MQTT Broker on Raspberry Pi 3 (Raspbian Stretch OS)

IoT Tutorial #18 [ Raspberry Pi Tutorials #6 ]
In previous post,
we have used https://www.pubnub.com as a Broker for Publish -Subscribe IoT Architecture.

Now we will be use Raspberry Pi 3 as a broker.

For Publish-Subscribe IoT Architecture, we need :
  • Client (Publisher and Subscriber)
  • Broker (Server)


    MQTT (MQ Telemetry Transport) is one of the widely used protocol for Publish-Subscribe IoT Architecture.

    In order to use Raspberry Pi 3 as Broker (Server), we need to install (MQTT) moquitto server
    and To use Raspberry Pi 3 as client (Publisher or Subscriber), we need to istall (MQTT) mosquitto client on RPi3.

    Follow these steps sequentially, to Install (MQTT) moquitto server and client on RPi3 (Raspbian Stretch OS):

    STEPS:

      1.      Installing MQTT Broker on Raspberry Pi:       

      Open terminal on RPi3 and Run following commands sequentially:
    #1.  To change directory to home directory
    cd ~

    #2.  To Download/Fetch the the mosquitto repository key (file)
    sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

    #3.  Set this key
    sudo apt-key add mosquitto-repo.gpg.key

    #4.  Change directory to below path
    cd /etc/apt/sources.list.d/




    #5.  To Download/Fetch mosquitto strech list
    sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
    sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list

    #6.  To update all
    sudo apt-get update

    #7.  To change directory to home directory
    cd ~

    #8.  To Dwonload/Fetch openssl
    sudo wget http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u7_armhf.deb
    Note: If you get any "404 not found" error in wget command, then put the link given in the command in the browser and check for the updated/latest version for the same package and use that (update) link in wget command.

    #9.  To install downloaded bebian package
    sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u7_armhf.deb

    #10.  To Download/Fetch web socket library
    sudo wget http://ftp.nz.debian.org/debian/pool/main/libw/libwebsockets/libwebsockets3_1.2.2-1_armhf.deb
    Note: If you get any "404 not found" error in wget command, then put the link given in the command in the browser and check for the updated/latest version for the same package and use that (update) link in wget command.

    #11.  To install downloaded debian package
    sudo dpkg -i libwebsockets3_1.2.2-1_armhf.deb

    #12.  To install mosquito
    sudo apt-get install mosquitto

    #13.  To install mosquitto clients
    sudo apt-get install mosquitto mosquitto-clients


    For better understanding, Watch the video below:

    Demonstration:




         

             2.      Test installed mosquitto (MQTT broker):

    #1.  Start mosquitto (MQTT broker) as a service
    sudo /etc/init.d/mosquitto start

    #Expected Response:
    [ ok ] Starting mosquitto (via systemctl): mosquitto.service.

    #2.  Start Subscriber for topic_name "iot"
    mosquitto_sub -d -t "iot"
    Note: -d : debug mode, -t : topic_name

    #Expected Response:
    Client mosqsub|2917-raspberryp sending CONNECT
    Client mosqsub|2917-raspberryp received CONNACK
    Client mosqsub|2917-raspberryp sending SUBSCRIBE (Mid: 1, Topic: iot, QoS: 0)
    Client mosqsub|2917-raspberryp received SUBACK
    Subscribed (mid: 1): 0

    #3.  Start Publisher for topic_name "iot" and send message:
    #Open new terminal window (keep previous terminal window open):
    mosquitto_pub -d -t "iot" -m "Hi, This is my msg..."
    Note: -d : debug mode, -t : topic_name

    #Expected Response: (On Publisher Window)
    Client mosqpub|2933-raspberryp sending CONNECT
    Client mosqpub|2933-raspberryp received CONNACK
    Client mosqpub|2933-raspberryp sending PUBLISH (d0, q0, r0, m1, 'iot', ... (21 bytes))
    Client mosqpub|2933-raspberryp sending DISCONNECT

    #Expected Response: (On Subscriber Window)
    Client mosqsub|2917-raspberryp received PUBLISH (d0, q0, r0, m0, 'iot', ... (21 bytes))
    Hi, This is my msg...

    Here, Message we published using PUBLISHER is received by SUBSCRIBER.
    That means, Testing of the mosquitto MQTT broker completed Successfully.





      3.      Installing mosquitto MQTT Client on Computer: 

    #1.  To install mosquitto MQTT client on laptop (Windows / Mac / Linux etc): 

    For MacBook:Open terminal on macOS and paste these commands sequentially:
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    brew install wget

    For Others: To install mosquitto MQTT client on laptop (Windows / Linux , etc)Visit here: https://mosquitto.org/download/

    #2.  Start Subscriber for topic_name "iot" and to connect to MQTT Broker (RPi3):

    For Macbook:

    mosquitto_sub -h 192.168.0.35 -t "iot"
    Note:
    -h : host = <ip_address_of_RPi3>
     -t : topic name


    Now whatever message is sent to the RPi3 (MQTT Broker) with topic name "iot" by any publisher will be received by your computer (MQTT Subscriber)

      4.      Stop the MQTT Broker on Raspberry Pi:      

      Open terminal on RPi3 and Run following commands sequentially:
    #1.  Stop mosquitto (MQTT broker) service
    sudo /etc/init.d/mosquitto stop

    #Expected Response:
    [ ok ] Stopping mosquitto (via systemctl): mosquitto.service.


    --------------------------------------------------------------------------------

    Click here to see more codes for Raspberry Pi 3 and similar Family.
    &
    Click here to see more codes for NodeMCU ESP8266 and similar Family.
    &
    Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

    Feel free to ask doubts in the comment section. I will try my best to solve it.
    If you find this helpful by any mean like, comment and share the post.
    This is the simplest way to encourage me to keep doing such work.

    Thanks and Regards,
    -Akshay P. Daga

    9 Comments

    1. I get stuck on steps 10, 11 an obviously 12.

      After my research for 10 I did:
      sudo wget http://ftp.nz.debian.org/debian/pool/main/libw/libwebsockets/libwebsockets12_2.4.2-1_armhf.deb

      and RPI seems to be happy.

      Then for the install part:
      sudo dpkg -i libwebsockets12_2.4.2-1_armhf.deb

      I then get a warning:

      dpkg: dependency problems prevent configuration of libwebsockets12:armhf:
      libwebsockets12:armhf depends on libev4 (>= 1:4.04); however:
      Package libev4 is not installed.

      dpkg: error processing package libwebsockets12:armhf (--install):
      dependency problems - leaving unconfigured
      Processing triggers for libc-bin (2.24-11+deb9u3) ...


      What should I do differently?

      ReplyDelete
      Replies
      1. follow the below steps --

        a) restart pi
        b) sudo apt-get upgrade
        c) sudo apt --fix-broken install
        d) Then install mosquito by going to #12 and #13
        e) use ‘mosquitto --version’ to check mqtt version
        note: ignore errors such –“ N: Ignoring file 'mosquitto-stretch.list.1' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension”

        Delete
    2. best tutorial I've seen!

      ReplyDelete
      Replies
      1. Thank you very much.
        Please checkout other Free IOT tutorials here : https://www.apdaga.com/search/label/IoT%20%28Internet%20of%20Things%29

        Delete
    3. This comment has been removed by a blog administrator.

      ReplyDelete
      Replies
      1. @Zaved khan what is the point of sharing movies related link here. Please stop spamming.

        Delete
    4. This comment has been removed by a blog administrator.

      ReplyDelete
    5. Thanks for your help, finally got it to work.

      ReplyDelete
    Post a Comment
    Previous Post Next Post