IoT Bytes

Bits and Bytes of IoT

Mosquitto MQTT Broker on Raspberry Pi

Pradeep Singh | 30th April 2016

mosquitto_mqtt (1)

MQTT is the protocol of choice for M2M and IoT Applications. However, when it comes to selecting the MQTT broker, most of the times we resort to Cloud based Brokers. Having a local MQTT Broker may have many advantages over Cloud based Brokers, like Security, Flexibility, Reliability, Low Latency, Cost Effectiveness, better QoS implementation etc.

Mosquitto MQTT Broker:

Mosquitto is an open source iot.eclipse.org project. It implements the MQTT protocol versions 3.1 and 3.1.1. For more details please refer to http://mosquitto.org/.

Raspberry Pi:

The Raspberry Pi is a Single Board Computer developed by Raspberry Pi Foundation. For more details you can refer to https://www.raspberrypi.org/

Mosquitto on Raspbery Pi:

Raspberry Pi has enough compute power to run Mosquitto and function as a personal MQTT Broker which can cater most of our personal MQTT needs. So let’s go ahead and explore Installation, Testing and Uninstallation process.

What do you need?

  • Raspberry Pi with Raspbian Operating System
  • Ethernet / WiFi Connection to Internet from Raspberry Pi

A. Install Mosquitto MQTT Broker:

1. SSH into Raspberry Pi and create a new directory for temp files –

mkdir mosquitto
cd mosquitto

2. Import the repository package signing key –

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key

3. Make the repository available to apt –

cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list

4. Install Mosquitto MQTT Broker –

apt-get install mosquitto

5. Check Mosquitto Service Status, Process and Default Port (1883) –

service mosquitto status
ps -ef | grep mosq
netstat -tln | grep 1883

If you see Mosquitto service running and listening to TCP Port 1883, you have a functional MQTT Broker.

B. Test Mosquitto MQTT Broker with MQTT Client:

For testing you can use any MQTT Client. However, if you have Python 2.7 Installed on your machine, you can test it with following sample Python scripts. To execute these Scripts, you must have Paho MQTT Client installed on your machine. You can install it with pip command –

pip install paho-mqtt

Once Paho Client Library is installed, you can download and execute following Python scripts (Don’t forget to change “MQTT_BROKER” IP Address) –

C. Uninstall Mosquitto MQTT Broker:

To uninstall Mosquitto you can use following command –

sudo apt-get purge mosquitto

If you want to completely remove Mosquitto with it’s associated configuration files, use following command –

sudo apt-get --purge remove mosquitto

26 thoughts on “Mosquitto MQTT Broker on Raspberry Pi

  1. hi Pradeep
    Thank you for this tutorial, I am used to using pc but raspberry pi a complete newb. I have been trying different tutorials to install Mosquitto, this is the first one that worked ‘out of the box’ as they say. I like your no frills, keep it simple approach
    thank you again
    Dave

    Like

  2. Hi i have problem : WebSocket connection to ‘ws://192.168.1.17:1883/mqtt’ failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

    can you help me ?

    Like

  3. Hello sir, thank you for this good tutorial.
    Can you give further information what to do after installing paho-mqtt?

    I cant Publish and Subscribe for some reason, I am wondering what am i doing wrong.

    Thank you,
    Eyal

    Like

    1. Hi Eyal, you may use following python scripts to test your MQTT Broker –

      https://github.com/pradeesi/MQTT_Broker_On_Raspberry_Pi/blob/master/subscriber.py

      https://github.com/pradeesi/MQTT_Broker_On_Raspberry_Pi/blob/master/publisher.py

      Make sure you have paho mqtt Client installed on the machine where you are executing your python scripts.

      If you aren’t comfortable with python scripts you may also use following tools for testing –

      https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm

      https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client

      Like

  4. hi i am getting output from this instruction currently i am doing a project regarding MQTT .
    I need to include sensor data that is temperature sensor DS18b20 1 wireprotocol i need to include the sensor data as MQTT message how would i include the data value

    Like

    1. You need to write MQTT Client code for the Microcontroller you may be using with your sensor (For Ex: Arduino, ESP8266 or Raspberry Pi – Single board computer). You can send the data to MQTT broker in XML or Json format, so that you can parse it on MQTT Subscribers.

      Like

  5. Hi sir I’m doing project on implementing AWS cloud for real time data storage by using raspberry pi I don’t know how MQTT protocol support this cloud can u explan me sir ? Thanks Santosh D

    Like

  6. One of the best and clear tutorial I ever seen.
    I will be very much appreciated if you upgrade this tutorial to setup Mosquito server running on Raspberry Pi with TLS security connection and CA key(port 8883).
    Everything else is compromise. Specially when the raspberry is open to the Wide WEB.
    There is a lot post about that, but I did`t find good enough to work out of the box.
    Thank you and keep going
    Rosen

    PS. I think is better to use in step 3:
    sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list instead wheezy. Mosquitto version in wheezy is outdated.

    Like

  7. 7.3.2017
    I had some problems using the new Jessie Kernel Version 4.4 from 2017-03-02

    I would like to suggest the following changes :
    1) after
    cd /etc/apt/sources.list.d/
    sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
    perform an update
    sudo apt-get install update

    2) apt-get install mosquitto should be ,of course, sudo apt-get install mosquitto

    after that it worked but I used the wheezy list, I will soon try the jessie.list

    NT

    Like

  8. Hi pradeep,
    I had run publish and subscribe code by following this site: https://github.com/aws/aws-iot-device-sdk-python#examples. And i had run this script in two raspberry pi and put one gpio switch, When i apressing that switch than and than code will be execute,that is running ok but i want to do like this: I want when i press first RPi’s switch the message will be print on second RPi’s terminal and when i press second RPi’s switch the message will be print on first RPi’s terminal . So how to do like this. Plz tell me

    Like

    1. You can use two MQTT Topics for this. Use the first Topic to publish messages from Pi-1 and Subscribe to this Topic on Pi-2 to receive and print msgs from Pi-1. Use the second Topic for reverse process.

      Like

    1. You can use Port Forwarding or DDNS for your MQTT Server located in Private Network.

      You can also use one of the free Public MQTT Servers. Following are some of the Public MQTT Servers –

      iot.eclipse.org
      http://test.mosquitto.org/
      http://test6.mosquitto.org/
      http://www.cloudmqtt.com
      mqtt.kgbvax.net

      If you want a better solution, write a Proxy code that can forward MQTT messages from your Private MQTT Broker to Public MQTT Broker and vice-versa.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: