How-To Get Started with Mosquitto MQTT Broker on a Raspberry Pi

How-To Get Started with Mosquitto MQTT Broker on a Raspberry Pi


Yo, In this video, I'll be covering how-to install, setup, and use Mosquitto MQTT broker on a Raspberry Pi. Mosquitto is an awesome open source MQT

ADB | Android Debug Bridge | Introduction & Setup
[ HD ] Nếu không phải là em – The Men ( Nếu một ngày bạn cảm thấy muốn khóc)
43 mẫu áo sơ mi nam kẻ sọc, áo lụa cao cấp thịnh hành nhất hiện nay


In this video, I’ll be covering how-to install, setup, and use Mosquitto MQTT broker on a Raspberry Pi. Mosquitto is an awesome open source MQTT broker that you can run on a device like a Raspberry Pi. It’s great because you don’t have to share you data to the cloud and can keep everything locally on your system. Plus, it’s free! MQTT is a great protocol for various DIY tech – like an Arduino, NodeMCU, or ESP8266 chip. If you have any questions, let me know! Happy to help with what I can!


— The Tech I Use —

—Favorite Parts—

Raspberry Pi 3 –
Case –
SD Card –
Power Supply –
Aeotec Z-Stick Gen 5 –
NodeMCU ESP8266 Chip –

—Shell Commands—

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

sudo nano /etc/mosquitto/mosquitto.conf

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

sudo mosquitto_passwd -c /etc/mosquitto/pwfile username

mosquitto_sub -d -u username -P password -t “dev/test”
mosquitto_pub -d -u username -P password -t “dev/test” -m “Hello world”


Mosquitto Configuration –
MQTT Home Assistant –
My Website –
Home Assistant –
HA Forum –
HA Chatroom –
HA GitHub –



  • comment-avatar

    18 seconds in… Where's the link?

  • comment-avatar

    Love your Vids, this one helped me with my MQTT Server. But that part with the router and the port forwarding is extremly dangerous. Never ever expose Ports to the Internet if you don't have to. Actually i can't even think of a scenario to use MQTT on the internet. But MQTT is open source which means finding vulnerabilitys or zero days is a lot easier.
    Also you don't remotely stress that you should use a complex password for your MQTT Server. You need it rarely so put it in a password manager and make it 30 chars long.
    Someone that sets up his MQTT like in your example is gonna have a bad time.

    I'd love a video about Smart Home Network Security and Password Security because it gets rarely mentioned in any Smart Home tutorial.
    I'm a network security engineer. Maybe i could help?

  • comment-avatar

    Nice Tutorial. If anybody wants to test it's MQTT Broker then use web based MQTT Client

  • comment-avatar

    trying to get this done, but starting with the call (sudo apt-get install mosquitto) doesnt work on Raspbian, but i followed instructions and got it isntalled by downloading the file through github (webrowser), later on "make" and "sudo make isntall" worked, then trying to modify the config comes empty.

  • comment-avatar

    A great tutorial. Thank you! Theo

  • comment-avatar

    don't run nano (or any editor) as root unnecessarily! use sudoedit, which makes a temporary copy of the file, calls the editor (from the unprivileged account) on that copy, then copies it over the original when you exit the editor.

  • comment-avatar

    i have a blutooth enabled multisensor want to upload the data in raspberry pi using mqtt can you suggest the way?

  • comment-avatar

    when I type this in mosquitto_sub -d -u username -P password -t "dev/test"

    I get this
    pi@raspberrypi:~ $ mosquitto_sub -d -u username -P password -t "dev/test"

    Client mosqsub|1246-raspberryp sending CONNECT

    Client mosqsub|1246-raspberryp received CONNACK (0)

    Client mosqsub|1246-raspberryp sending SUBSCRIBE (Mid: 1, Topic: dev/test, QoS: 0)

    Client mosqsub|1246-raspberryp received SUBACK

    Subscribed (mid: 1): 0

    when I type this
    mosquitto_pub -d -u username -P password -t "dev/test" -m "Hello world"

    nothing happens

  • comment-avatar

    How to build mosquitto with websockets

  • comment-avatar

    Nice video! Please bear in mind, if you open the port of any MQTT broker to the internet from your home network, please secure it with authentication and authorization. You can read more about this here ( Otherwise it is open to anybody in the web.

  • comment-avatar

    Ha, I love things like this. I can't log in with putty the connection is refused. I suspect there is a long list of things I have to do before 'just logging in". Also by the time I've figured that out, I'll probably find all this is out of date as well as the RPi.

  • comment-avatar

    try this Web SCADA platform to create free MQTT connection to public MQTT broker:

  • comment-avatar

    it works!!!! thank you

  • comment-avatar

    Hey buddy! what happened at 2:40?

  • comment-avatar

    Hi Ben,
    Great instruction as always, but I think I am missing something. I have followed your other video that you published a year later called “Setting Up the SSH Add On – HassIO” works fine and enables access to HassIO as described. However this means that I cannot follow your instruction for installing updating using ‘sudo apt-get update’ because (as far as I can see) I do not have access to pi@raspberrypi: prompt.
    I have tried a new connection in Putty without SSH connection authorisation but no luck there.
    Does the SSH add-on as set up for HassIO mean I no longer have access to a ‘normal’ SSH connection?
    I can’t help but think that I must be missing something basic, but any help would be greatfully received.

  • comment-avatar

    mqtt required in order to add sonof devices i created?

  • comment-avatar

    where i can find teh file configuration.yaml?

  • comment-avatar

    sorry if this is not allowed, but another video on here really messed this up, can you help please

    pi@hassbian:~ $ sudo apt-get install mosquitto

    Reading package lists… Done

    Building dependency tree

    Reading state information… Done

    mosquitto is already the newest version (1.5.5-0mosquitto1).

    0 upgraded, 0 newly installed, 0 to remove and 53 not upgraded.

    1 not fully installed or removed.

    After this operation, 0 B of additional disk space will be used.

    Do you want to continue? [Y/n] y

    Setting up mosquitto (1.5.5-0mosquitto1) …

    Failed to start mosquitto.service: Unit mosquitto.service is not loaded properly : Invalid argument.

    See system logs and 'systemctl status mosquitto.service' for details.

    invoke-rc.d: initscript mosquitto, action "start" failed.

    ● mosquitto.service – Mosquitto MQTT Broker daemon

    Loaded: error (Reason: Invalid argument)

    Active: inactive (dead)

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Hint: Some lines were ellipsized, use -l to show in full.

    dpkg: error processing package mosquitto (–configure):

    subprocess installed post-installation script returned error exit status 1

    Errors were encountered while processing:


    E: Sub-process /usr/bin/dpkg returned an error code (1)

  • comment-avatar

    HI: a little problem my port :1883 does not appear on the order comand:.((C: Users > netstat )) …………End CMD mosquitto the message comes ((Error: Unable to connect because the target computer refused the connection)).

  • comment-avatar

    Maybe you can talk a little bit faster?

  • comment-avatar

    How do I set up a toggle switch on ha to send one message when toggled on and a different one when toggled off

  • comment-avatar

    Newbie here….. I am struggling with understanding the basic 'architecture' of running HA on a Raspberry Pi. I have one Pi and a micro SD card with Raspbian (ip address: and a micro SD card with HA (ip address: In my mind this means that I either run the OS Raspbian or I run HA, but when i watch the various HA video's you all use Linux (e.g. Sudo) commands while you have HA up and running. When I run HA, I can not SSH into Raspbian on my PI, I can only SSH into HA. Any hints or tips on how these two environments are integrated while the PI has the HA SD card, is very much appreciated!

  • comment-avatar

    Finally got past the connection refused and was successful in publishing the Hello World message to topic dev/test when sending from the pi console. When trying to publish from MQTT service in a browser it fails.

  • comment-avatar

    Seriously man, this is an excellent tutorial. Good job. You made me subscribe to you on this one.

  • comment-avatar

    You need to add the following to create file called /lib/systemd/system/mosquitto.service
    Add this content to it:
    Description=Mosquitto MQTT Broker daemon

    ExecStart=/bin/sh -c "/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d"
    ExecReload=/bin/kill -HUP $MAINPID


    This will allow you create a service which can be enabled to start at boot with : sudo systemctl start mosquitto,
    I also found that you need to modify the lines in
    sudo nano /etc/mosquitto/mosquitto.conf to

    allow_anonymous false
    password_file /etc/mosquitto/pwfile
    port 1883

    after you do this reboot

  • comment-avatar

    May people and myself have been having issue with connection refused after rebooting. It seems running the command: mosquitto -d will fix the issue but that command needs to be run after each reboot. Therefore, my fix was to place a start up command in my LXDE file to run the mosquitto -d command upon restart / boot up. An easy single echo line command: echo "mosquitto -d" >> ~/.config/lxsession/LXDE-pi/autostart Now I have access after reboot without issue. Ben might want to add that to you commands in the description above … Good luck all.

  • comment-avatar

    can you tell us how to connect owntracks app to an mqtt broker and then publish the information in the form of latitudes and longitudes in google maps in another device? Please do reply 🙂 Cheers

  • comment-avatar

    Fantastic tutorial, works perfect first time
    For those who are having issues, persist with it, because it is not this tutorial, but your own environment.

  • comment-avatar
  • comment-avatar

    Hi Ben, nice VID, like all yours, but for me "port: 1883" in configuration.yaml makes homeassistant refuse to start … also comes up with an error – any hints ? Without that line my home assistant rans well so far …

  • comment-avatar

    Thanks for that, helped me get going with it via openHab.

  • comment-avatar

    core-ssh:/# mosquitto_sub -d -u LedStrip -P LedStrip -t "dev/test"
    Error: Address not available
    :C Can every people help me?

  • comment-avatar

    If you're not able to connect, try erasing the username and password fields in the sonoff configuration menu. The default values won't work, they need to be cleared out and saved as blank (they'll fill in with '0' afterwards I think, not sure).

  • comment-avatar

    Hi Ben,
    Need some help configuring a tasmotized WEMOS d1 mini with a ds18b20 temp sensor to talk with ha.
    Everything is working, but not able to push temp values to dashboard ….. HELP!!!!!

  • comment-avatar

    Thanks for the video. much clearer and easier to understand than the others I found

  • comment-avatar

    How do I send messages from outside of the home network?

  • comment-avatar

    pi@raspberrypi:~ $ mosquitto_sub -d -u username -P password -t "dev/test"
    Error: Connection refused

  • comment-avatar

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