Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)
Install Mosquitto Broker for MQTT communication on a Linux Ubuntu VM using Digital Ocean. Connect ESP32/ESP8266 boards from anywhere using different networks.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Learn how to install Mosquitto Broker for MQTT communication on a Linux Ubuntu VM (Virtual Machine) using Digital Ocean. Running an MQTT Mosquitto Broker in the cloud allows you to connect several ESP32/ESP8266 boards and other IoT devices from anywhere using different networks as long as they have an Internet connection. We’ll also cover how to connect your ESP boards to the cloud MQTT broker using Arduino IDE.
You might like:
SMART HOME with Raspberry Pi, ESP32, and ESP8266
—learn Node-RED and InfluxDB on a Raspberry Pi to build a Home Automation System with the ESP32 and ESP8266.
Introducing MQTT Protocol
MQTT stands for Message Queuing Telemetry Transport. It is a lightweight publish and subscribe system where you can publish and receive messages as a client. It is widely used in the home automation and IoT fields.
To learn more about MQTT, read our complete guide:
What is MQTT and how it works
An MQTT broker is primarily responsible for receiving all MQTT messages, filtering the messages, decide who is interested in each message and then, publishing the messages to all subscribed clients.
There are several brokers you can use. In our Home Automation projects and tutorials we use the popular
Mosquitto MQTT Broker
. It is easy to install, configure and use.
In this tutorial, we’ll show you how to install Mosquitto MQTT broker on the cloud—a Linux Ubuntu VM (virtual machine) running on Digital Ocean hosting service.
Cloud MQTT Broker Overview
What’s the advantage of using a Cloud MQTT broker and how it works?
Using a Cloud MQTT broker allows several IoT devices (like
ESP32
and
ESP8266
boards) to communicate with each other using MQTT, even if they are on different networks (different locations connected to different routers). Here’s an overview.
Mosquitto MQTT broker is running on the cloud (host service provided by Digital Ocean). So, it can receive messages from IoT devices all around the world.
You can have several ESP boards on different networks that connect to the same Cloud MQTT broker.
Each ESP board needs to be connected to a router that allows access to the internet in order to connect with the broker.
Because the boards use the same MQTT broker, they can communicate with each other by publishing and subscribing to the same topics.
The following diagram shows an example of a possible application:
The previous image shows two ESP32 boards on different networks. Each board is connected to a different router with access to the internet.
Even though they are on different networks, they can communicate with each other via the Cloud MQTT broker by subscribing and publishing on the same topics.
ESP32 #1 publishes on a topic that ESP32 #2 is subscribed to (
board2/output1
). The message can indicate whether ESP32 #2 should turn an output on or off. So, ESP32 #1 can control the ESP32 #2 outputs.
Similarly, ESP32 #2 publishes temperature readings on the
board2/temperature
topic. ESP32 #1 is subscribed to that topic, so it receives board2 sensor readings.
You can also
install Node-RED
on the same cloud (Digital Ocean hosting account) to control and monitor your boards from anywhere in the world using your computer or your smartphone. You can follow this tutorial:
Access Node-RED Dashboard from Anywhere using Digital Ocean
Hosting Service – Digital Ocean
To run your Cloud MQTT Mosquitto Broker, you need to use a hosting service that allows you to have access to the command line and install any software that you need. I recommend using
Digital Ocean
that offers an Ubuntu server that you can manage through a command line.
I’ve been using it since 2015 and I personally recommend it, but you can use any other hosting service. Any hosting service that offers a Linux Ubuntu VM with full console access should work.
If you don’t have a hosting account, I recommend
signing up for Digital Ocean
. When you sign up for Digital Ocean, you can try it for 60 days (they give you free credits to test the platform). You need to go to this link in order to claim the free credits:
Note:
you can also
run Mosquitto MQTT Broker in your local network using a Raspberry Pi board.
However, the purpose of this tutorial is to run an MQTT broker in the cloud to communicate with boards (or other IoT devices) across different networks.
Creating Digital Ocean Account
To create a Digital Ocean Account, go to
Digital Ocean
and sign up using one of the available options.
Create your account
, and you’ll receive a $200 credit that you can use for 60 days to test the platform. You might need to enter a valid credit card, but you can cancel your account anytime if you’re no longer interested in using the service after the free 60 days trial.
Confirm your account and login. On the
Project
tab, click on your name. You should see a similar Dashboard.
Create a Droplet (Linux Ubuntu VM)
To create a new VM, press the “
Create
” button on the top right corner and select the “
Droplets
” option. Digital Ocean calls
Droplets
to its VMs.
Important:
if you’re already running a
Droplet with Node-RED installed
, you can skip these next steps (creating a Droplet). You can run both Node-RED and Mosquitto MQTT broker on the same server.
Then, select the following options:
Distributions: Ubuntu
Choose a plan: Shared CPU Basic—we recommend choosing the $6/month option (the $4 plan will also work, but might be a bit slow).
Choose a datacenter region—choose the closest to your location.
Create the root password that allows you to access your Droplet (save this password, because you’ll need it to access your server).
Then, you can select any additional options you think might be useful for your project.
Finally, choose a
hostname
to easily identify which Virtual Machine you are working with. I’ve named my Droplet
home-automation-system
That’s it, you just need to press the big green button
Create Droplet
to finish the process.
Wait a few minutes and when the progress bar ends, your Droplet is ready.
Accessing Your Linux Ubuntu VM Console
Now, if you click on the
Droplets
tab, your newly created droplet should be there.
Click on the droplet name. A new page will open. At the top right corner, there’s a Console link. If you click there, it will open a new console/terminal window where you can type Linux commands to install software or run commands the same way you do on your Raspberry Pi via SSH.
Type your login username (
root
) and the password defined earlier, press the Enter key to access your server.
There’s an
optional
step, but it goes beyond the scope of this tutorial. It’s
not
required to make this project work: prepare your server with non-root, sudo-enabled user and basic firewall with this
Initial Server Setup with Ubuntu 20.04
Installing Mosquitto MQTT Broker on Linux Ubuntu VM Digital Ocean
Let’s install the
Mosquitto Broker
on Digital Ocean.
1)
Run the following command to upgrade and update your system:
sudo apt update && sudo apt upgrade -y
2)
When asked, press
Y
and
Enter
. It will take some time to update and upgrade.
3)
To install the Mosquitto Broker enter the next command:
sudo apt install -y mosquitto mosquitto-clients
That’s it! Mosquitto MQTT broker is installed.
4)
To make Mosquitto auto start when the server boots, you need to run the following command (this step is optional, but it ensures that as long as the server is running, Mosquitto will be running even after a server restart):
sudo systemctl enable mosquitto.service
5)
Now, test the installation by running the following command:
mosquitto -v
This returns the Mosquitto version that is currently running on your server. It will be 2.0.11 or above.
You can ignore the error message “Error: Address already in use”.
Enable Remote Acce
You might like:
SMART HOME with Raspberry Pi, ESP32, and ESP8266
—learn Node-RED and InfluxDB on a Raspberry Pi to build a Home Automation System with the ESP32 and ESP8266.
Introducing MQTT Protocol
MQTT stands for Message Queuing Telemetry Transport. It is a lightweight publish and subscribe system where you can publish and receive messages as a client. It is widely used in the home automation and IoT fields.
To learn more about MQTT, read our complete guide:
What is MQTT and how it works
An MQTT broker is primarily responsible for receiving all MQTT messages, filtering the messages, decide who is interested in each message and then, publishing the messages to all subscribed clients.
There are several brokers you can use. In our Home Automation projects and tutorials we use the popular
Mosquitto MQTT Broker
. It is easy to install, configure and use.
In this tutorial, we’ll show you how to install Mosquitto MQTT broker on the cloud—a Linux Ubuntu VM (virtual machine) running on Digital Ocean hosting service.
Cloud MQTT Broker Overview
What’s the advantage of using a Cloud MQTT broker and how it works?
Using a Cloud MQTT broker allows several IoT devices (like
ESP32
and
ESP8266
boards) to communicate with each other using MQTT, even if they are on different networks (different locations connected to different routers). Here’s an overview.
Mosquitto MQTT broker is running on the cloud (host service provided by Digital Ocean). So, it can receive messages from IoT devices all around the world.
You can have several ESP boards on different networks that connect to the same Cloud MQTT broker.
Each ESP board needs to be connected to a router that allows access to the internet in order to connect with the broker.
Because the boards use the same MQTT broker, they can communicate with each other by publishing and subscribing to the same topics.
The following diagram shows an example of a possible application:
The previous image shows two ESP32 boards on different networks. Each board is connected to a different router with access to the internet.
Even though they are on different networks, they can communicate with each other via the Cloud MQTT broker by subscribing and publishing on the same topics.
ESP32 #1 publishes on a topic that ESP32 #2 is subscribed to (
board2/output1
). The message can indicate whether ESP32 #2 should turn an output on or off. So, ESP32 #1 can control the ESP32 #2 outputs.
Similarly, ESP32 #2 publishes temperature readings on the
board2/temperature
topic. ESP32 #1 is subscribed to that topic, so it receives board2 sensor readings.
You can also
install Node-RED
on the same cloud (Digital Ocean hosting account) to control and monitor your boards from anywhere in the world using your computer or your smartphone. You can follow this tutorial:
Access Node-RED Dashboard from Anywhere using Digital Ocean
Hosting Service – Digital Ocean
To run your Cloud MQTT Mosquitto Broker, you need to use a hosting service that allows you to have access to the command line and install any software that you need. I recommend using
Digital Ocean
that offers an Ubuntu server that you can manage through a command line.
I’ve been using it since 2015 and I personally recommend it, but you can use any other hosting service. Any hosting service that offers a Linux Ubuntu VM with full console access should work.
If you don’t have a hosting account, I recommend
signing up for Digital Ocean
. When you sign up for Digital Ocean, you can try it for 60 days (they give you free credits to test the platform). You need to go to this link in order to claim the free credits:
Note:
you can also
run Mosquitto MQTT Broker in your local network using a Raspberry Pi board.
However, the purpose of this tutorial is to run an MQTT broker in the cloud to communicate with boards (or other IoT devices) across different networks.
Creating Digital Ocean Account
To create a Digital Ocean Account, go to
Digital Ocean
and sign up using one of the available options.
Create your account
, and you’ll receive a $200 credit that you can use for 60 days to test the platform. You might need to enter a valid credit card, but you can cancel your account anytime if you’re no longer interested in using the service after the free 60 days trial.
Confirm your account and login. On the
Project
tab, click on your name. You should see a similar Dashboard.
Create a Droplet (Linux Ubuntu VM)
To create a new VM, press the “
Create
” button on the top right corner and select the “
Droplets
” option. Digital Ocean calls
Droplets
to its VMs.
Important:
if you’re already running a
Droplet with Node-RED installed
, you can skip these next steps (creating a Droplet). You can run both Node-RED and Mosquitto MQTT broker on the same server.
Then, select the following options:
Distributions: Ubuntu
Choose a plan: Shared CPU Basic—we recommend choosing the $6/month option (the $4 plan will also work, but might be a bit slow).
Choose a datacenter region—choose the closest to your location.
Create the root password that allows you to access your Droplet (save this password, because you’ll need it to access your server).
Then, you can select any additional options you think might be useful for your project.
Finally, choose a
hostname
to easily identify which Virtual Machine you are working with. I’ve named my Droplet
home-automation-system
That’s it, you just need to press the big green button
Create Droplet
to finish the process.
Wait a few minutes and when the progress bar ends, your Droplet is ready.
Accessing Your Linux Ubuntu VM Console
Now, if you click on the
Droplets
tab, your newly created droplet should be there.
Click on the droplet name. A new page will open. At the top right corner, there’s a Console link. If you click there, it will open a new console/terminal window where you can type Linux commands to install software or run commands the same way you do on your Raspberry Pi via SSH.
Type your login username (
root
) and the password defined earlier, press the Enter key to access your server.
There’s an
optional
step, but it goes beyond the scope of this tutorial. It’s
not
required to make this project work: prepare your server with non-root, sudo-enabled user and basic firewall with this
Initial Server Setup with Ubuntu 20.04
Installing Mosquitto MQTT Broker on Linux Ubuntu VM Digital Ocean
Let’s install the
Mosquitto Broker
on Digital Ocean.
1)
Run the following command to upgrade and update your system:
sudo apt update && sudo apt upgrade -y
2)
When asked, press
Y
and
Enter
. It will take some time to update and upgrade.
3)
To install the Mosquitto Broker enter the next command:
sudo apt install -y mosquitto mosquitto-clients
That’s it! Mosquitto MQTT broker is installed.
4)
To make Mosquitto auto start when the server boots, you need to run the following command (this step is optional, but it ensures that as long as the server is running, Mosquitto will be running even after a server restart):
sudo systemctl enable mosquitto.service
5)
Now, test the installation by running the following command:
mosquitto -v
This returns the Mosquitto version that is currently running on your server. It will be 2.0.11 or above.
You can ignore the error message “Error: Address already in use”.
Enable Remote Acce
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
- Raspberry Pi
Software & tools
- Arduino IDE
- Node-RED
- MQTT
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.