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

34 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 pradeep..thanks for information..
    but from where i DNS name..i want to used it globally not locally . how to used it globally.

    Like

  3. 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

    1. You need to configure MQTT Broker for WebSocket communication, by default it’s disabled. Please check the documentation to make sure WebSockets are supported on your MQTT Broker.

      Like

  4. 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

  5. Hi, thank you for this tutorial, I just want to know can we use this to connect with websocket ? And how can we do this ? Thank you.

    Like

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Hi Pradeep,

    Thanks for the nicely written article. However I am facing an awkward problem while running MQTT broker on RasPi and trying to connect to it from NodeMCU.

    The issue is when I try to connect to the broker ( running on RasPi) from a mobile based client, it works like charm. But when I try to connect it from NodeMCU, it never connects and gives return code as 2.

    But the same NodeMCU works fine when I try to connect to cloud based brokers.

    I have tried to search about this issue for quite some time…but couldn’t find anything that worked for me.

    Will be grateful if you could help me out.

    – AG

    Like

    1. Error code 2 is related to MQTT authentication / permissions –

      0010 – Permission denied (given credentials are insufficient to subscribe to this topic)

      Check your MQTT broker configuration and MQTT Client library used on NodeMCU.

      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

  12. Did you now if there is access control on the publishers and subcribers and how this is being done on the mosquitto broker? and if so what is the info should be provided from SUbs and Pubs

    Like

  13. Really nice guide. I got my mqtt broker running. I would like mqtt to send me emails through gmail when a particular topic change status. Any ideas please?

    Like

  14. Hi ,
    I am new to AWS IoT platform AWS python SDK . Exploring it now .
    I would perform these in setps

    1) I need to record audio from MIC in PC
    2) I need to subscribe this send audio file or live audio from my PC to AWS cloud
    3) I need to save this audio file or live audio in . wave format and publish this audio file to user

    How can I do that in python ? Can I use MQTT or any other protocol for this IoT implementation ?

    Regards
    Arun

    Like

Leave a reply to Pradeep Singh Cancel reply