{"id":97731,"date":"2023-03-03T09:24:00","date_gmt":"2023-03-03T09:24:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=97731"},"modified":"2024-03-10T17:36:41","modified_gmt":"2024-03-10T17:36:41","slug":"cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean\/","title":{"rendered":"Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)"},"content":{"rendered":"\n<p>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&#8217;ll also cover how to connect your ESP boards to the cloud MQTT broker using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)\" class=\"wp-image-128392\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\">You might like: <strong><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/a><\/strong>\u2014learn Node-RED and InfluxDB on a Raspberry Pi to build a Home Automation System with the ESP32 and ESP8266.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing MQTT Protocol<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">To learn more about MQTT, read our complete guide: <a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and how it works<\/a>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"303\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mqtt_broker.png?resize=750%2C303&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mosquitto MQTT Broker publish subscribe example messages\" class=\"wp-image-73565\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mqtt_broker.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mqtt_broker.png?resize=300%2C121&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/mqtt_broker.png?resize=250%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>There are several brokers you can use. In our Home Automation projects and tutorials we use the popular <a rel=\"noreferrer noopener\" href=\"https:\/\/mosquitto.org\/\" target=\"_blank\">Mosquitto MQTT Broker<\/a>. It is easy to install, configure and use. <\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll show you how to install Mosquitto MQTT broker on the cloud\u2014a Linux Ubuntu VM (virtual machine) running on Digital Ocean hosting service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cloud MQTT Broker Overview<\/h2>\n\n\n\n<p>What&#8217;s the advantage of using a Cloud MQTT broker and how it works? <\/p>\n\n\n\n<p>Using a Cloud MQTT broker allows several IoT devices (like <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">ESP32<\/a> and <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">ESP8266<\/a> boards) to communicate with each other using MQTT, even if they are on different networks (different locations connected to different routers). Here&#8217;s an overview.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"950\" height=\"950\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview.png?resize=950%2C950&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Digital Ocean MQTT Mosquitto Broker Overview\" class=\"wp-image-97777\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview.png?w=950&amp;quality=100&amp;strip=all&amp;ssl=1 950w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview.png?resize=300%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview.png?resize=768%2C768&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>You can have several ESP boards on different networks that connect to the same Cloud MQTT broker.<\/li>\n\n\n\n<li>Each ESP board needs to be connected to a router that allows access to the internet in order to connect with the broker.<\/li>\n\n\n\n<li>Because the boards use the same MQTT broker, they can communicate with each other by publishing and subscribing to the same topics.<\/li>\n<\/ul>\n\n\n\n<p>The following diagram shows an example of a possible application:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"914\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview-Publish-Subscribe.png?resize=950%2C914&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Digital Ocean MQTT Mosquitto Broker Overview Publish Subscribe\" class=\"wp-image-97778\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview-Publish-Subscribe.png?w=950&amp;quality=100&amp;strip=all&amp;ssl=1 950w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview-Publish-Subscribe.png?resize=300%2C289&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/ESP32-ESP8266-Digital-Ocean-MQTT-Mosquitto-Broker-Overview-Publish-Subscribe.png?resize=768%2C739&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The previous image shows two ESP32 boards on different networks. Each board is connected to a different router with access to the internet.<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>ESP32 #1 publishes on a topic that ESP32 #2 is subscribed to (<span class=\"rnthl rntliteral\">board2\/output1<\/span>). The message can indicate whether ESP32 #2 should turn an output on or off. So, ESP32 #1 can control the ESP32 #2 outputs.<\/li>\n\n\n\n<li>Similarly, ESP32 #2 publishes temperature readings on the <span class=\"rnthl rntliteral\">board2\/temperature<\/span> topic. ESP32 #1 is subscribed to that topic, so it receives board2 sensor readings.<\/li>\n<\/ul>\n\n\n\n<p>You can also <a href=\"https:\/\/randomnerdtutorials.com\/access-node-red-dashboard-anywhere-digital-ocean\/\">install Node-RED<\/a> 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: <a href=\"https:\/\/randomnerdtutorials.com\/access-node-red-dashboard-anywhere-digital-ocean\/\">Access Node-RED Dashboard from Anywhere using Digital Ocean<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hosting Service &#8211; Digital Ocean<\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Digital Ocean<\/a><span style=\"font-size: inherit; background-color: initial;\"> that offers an Ubuntu server that you can manage through a command line.<\/span><\/p>\n\n\n\n<p>I&#8217;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. <\/p>\n\n\n\n<p>If you don&#8217;t have a hosting account, I recommend <a href=\"https:\/\/randomnerdtutorials.com\/bluehost\">signing up for Digital Ocean<\/a>. 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: <a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/randomnerdtutorials.com\/digitalocean<\/a>.<\/p>\n\n\n\n<p style=\"text-align:center\"><a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" class=\"button\" rel=\"noopener noreferrer nofollow\">Grab Linux Ubuntu VM on Digital Ocean \u00bb<\/a><\/p>\n\n\n\n<p>If you like our projects, you might consider signing up to the recommended hosting service, because you&#8217;ll be supporting our work.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> you can also <a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">run Mosquitto MQTT Broker in your local network using a Raspberry Pi board.<\/a> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Digital Ocean Account<\/h2>\n\n\n\n<p>To create a Digital Ocean Account, go to <a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Digital Ocean<\/a> and sign up using one of the available options.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" rel=\"noopener noreferrer nofollow\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"477\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Digital-ocean-MQTT-mosquitto-get-started.png?resize=750%2C477&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Mosquitto MQTT Broker Installation\" class=\"wp-image-128375\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Digital-ocean-MQTT-mosquitto-get-started.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Digital-ocean-MQTT-mosquitto-get-started.png?resize=300%2C191&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Create your account<\/a>, and you&#8217;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&#8217;re no longer interested in using the service after the free 60 days trial.<\/p>\n\n\n\n<p>Confirm your account and login. On the <strong>Project<\/strong> tab, click on your name. You should see a similar Dashboard.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"944\" height=\"602\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/3-Digital-Ocean-Login-Dashboard-Mosquitto-MQTT-Broker.png?resize=944%2C602&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Login Dashboard Mosquitto MQTT Broker\" class=\"wp-image-97716\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/3-Digital-Ocean-Login-Dashboard-Mosquitto-MQTT-Broker.png?w=944&amp;quality=100&amp;strip=all&amp;ssl=1 944w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/3-Digital-Ocean-Login-Dashboard-Mosquitto-MQTT-Broker.png?resize=300%2C191&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/3-Digital-Ocean-Login-Dashboard-Mosquitto-MQTT-Broker.png?resize=768%2C490&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 944px) 100vw, 944px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Create a Droplet (Linux Ubuntu VM)<\/h2>\n\n\n\n<p>To create a new VM, press the &#8220;<strong>Create<\/strong>&#8221; button on the top right corner and select the &#8220;<strong>Droplets<\/strong>&#8221; option. Digital Ocean calls <strong>Droplets<\/strong> to its VMs.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Important: <\/strong> if you&#8217;re already running a <a href=\"https:\/\/randomnerdtutorials.com\/access-node-red-dashboard-anywhere-digital-ocean\/\">Droplet with Node-RED installed<\/a>, you can skip these next steps (creating a Droplet). You can run both Node-RED and Mosquitto MQTT broker on the same server.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"370\" height=\"524\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet.png?resize=370%2C524&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Create Droplet Mosquitto MQTT Broker\" class=\"wp-image-128376\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet.png?w=370&amp;quality=100&amp;strip=all&amp;ssl=1 370w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet.png?resize=212%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 212w\" sizes=\"(max-width: 370px) 100vw, 370px\" \/><\/figure><\/div>\n\n\n<p>Then, select the following options:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Distributions: Ubuntu<\/li>\n\n\n\n<li>Choose a plan: Shared CPU Basic\u2014we recommend choosing the $6\/month option (the $4 plan will also work, but might be a bit slow).<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"652\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-linux-plan.png?resize=750%2C652&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Install MQTT Broker Choose a Linux Plan\" class=\"wp-image-128377\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-linux-plan.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-linux-plan.png?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Choose a datacenter region\u2014choose the closest to your location.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"553\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/7-Digital-Ocean-VPN-Linux-Ubuntu-Server-Location-Mosquitto-MQTT-Broker-Installation.png?resize=984%2C553&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Location Mosquitto MQTT Broker Installation\" class=\"wp-image-97720\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/7-Digital-Ocean-VPN-Linux-Ubuntu-Server-Location-Mosquitto-MQTT-Broker-Installation.png?w=984&amp;quality=100&amp;strip=all&amp;ssl=1 984w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/7-Digital-Ocean-VPN-Linux-Ubuntu-Server-Location-Mosquitto-MQTT-Broker-Installation.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/7-Digital-Ocean-VPN-Linux-Ubuntu-Server-Location-Mosquitto-MQTT-Broker-Installation.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 984px) 100vw, 984px\" \/><\/figure><\/div>\n\n\n<p>Create the root password that allows you to access your Droplet (save this password, because you&#8217;ll need it to access your server).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"987\" height=\"467\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/8-Digital-Ocean-VPN-Linux-Ubuntu-Server-Set-Define-Password.png?resize=987%2C467&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"c\" class=\"wp-image-97721\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/8-Digital-Ocean-VPN-Linux-Ubuntu-Server-Set-Define-Password.png?w=987&amp;quality=100&amp;strip=all&amp;ssl=1 987w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/8-Digital-Ocean-VPN-Linux-Ubuntu-Server-Set-Define-Password.png?resize=300%2C142&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/8-Digital-Ocean-VPN-Linux-Ubuntu-Server-Set-Define-Password.png?resize=768%2C363&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/figure><\/div>\n\n\n<p>Then, you can select any additional options you think might be useful for your project.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"324\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/digitalocean-additional-options.png?resize=750%2C324&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Select Additional Options\" class=\"wp-image-128378\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/digitalocean-additional-options.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/digitalocean-additional-options.png?resize=300%2C130&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>Finally, choose a <strong>hostname <\/strong>to easily identify which Virtual Machine you are working with. I&#8217;ve named my Droplet <span class=\"rnthl rntliteral\">home-automation-system<\/span>.<\/p>\n\n\n\n<p>That&#8217;s it, you just need to press the big green button <strong>Create Droplet<\/strong> to finish the process.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"446\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet-name.png?resize=750%2C446&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Choose Hostname\" class=\"wp-image-128379\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet-name.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-create-droplet-name.png?resize=300%2C178&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Wait a few minutes and when the progress bar ends, your Droplet is ready.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Your Linux Ubuntu VM Console<\/h2>\n\n\n\n<p>Now, if you click on the <strong>Droplets <\/strong>tab, your newly created droplet should be there.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"367\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-created-droplets.png?resize=709%2C367&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Access Droplets\" class=\"wp-image-128380\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-created-droplets.png?w=709&amp;quality=100&amp;strip=all&amp;ssl=1 709w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-created-droplets.png?resize=300%2C155&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p>Click on the droplet name. A new page will open. At the top right corner, there\u2019s 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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"221\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-launch-console.png?resize=735%2C221&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Open Launch Console\" class=\"wp-image-128381\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-launch-console.png?w=735&amp;quality=100&amp;strip=all&amp;ssl=1 735w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-launch-console.png?resize=300%2C90&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<p>Type your login username (<span class=\"rnthl rntliteral\">root<\/span>) and the password defined earlier, press the Enter key to access your server.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"538\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-console.png?resize=750%2C538&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean VPN Linux Ubuntu Server Login Console Username Password\" class=\"wp-image-128382\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-console.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-console.png?resize=300%2C215&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>There&#8217;s an <strong><em>optional <\/em><\/strong>step, but it goes beyond the scope of this tutorial. It&#8217;s <strong><em>not<\/em> <\/strong>required to make this project work: prepare your server with non-root, sudo-enabled user and basic firewall with this <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/initial-server-setup-with-ubuntu-20-04\" target=\"_blank\">Initial Server Setup with Ubuntu 20.04<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Mosquitto MQTT Broker on Linux Ubuntu VM Digital Ocean<\/h2>\n\n\n\n<p>Let\u2019s install the&nbsp;<strong><a href=\"http:\/\/mosquitto.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mosquitto Broker<\/a><\/strong> on Digital Ocean.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"197\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Eclipse-Mosquitto-logo.png?resize=200%2C197&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Mosquitto MQTT Broker Logo\" class=\"wp-image-67892\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Eclipse-Mosquitto-logo.png?w=200&amp;quality=100&amp;strip=all&amp;ssl=1 200w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/01\/Eclipse-Mosquitto-logo.png?resize=80%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 80w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n<p><strong>1)<\/strong> Run the following command to upgrade and update your system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n\n\n\n<p><strong>2)<\/strong> When asked, press <strong>Y<\/strong> and <strong>Enter<\/strong>. It will take some time to update and upgrade.<\/p>\n\n\n\n<p><strong>3)<\/strong> To install the Mosquitto Broker enter the next command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y mosquitto mosquitto-clients<\/code><\/pre>\n\n\n\n<p>That&#8217;s it! Mosquitto MQTT broker is installed.<\/p>\n\n\n\n<p><strong>4)<\/strong> 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):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable mosquitto.service<\/code><\/pre>\n\n\n\n<p><strong>5)<\/strong> Now, test the installation by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto -v<\/code><\/pre>\n\n\n\n<p>This returns the Mosquitto version that is currently running on your server. It will be 2.0.11 or above.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"472\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-mqtt-broker-version.png?resize=750%2C472&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Droplet Mosquitto Version \" class=\"wp-image-128383\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-mqtt-broker-version.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-mqtt-broker-version.png?resize=300%2C189&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You can ignore the error message \u201cError: Address already in use\u201d.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enable Remote Access\/ Authentication<\/h3>\n\n\n\n<p>To enable remote access so that we can communicate with IoT devices, we need to edit\/create a configuration file.<\/p>\n\n\n\n<p>We\u2019ll add authentication with user and password.<\/p>\n\n\n\n<p><strong>1)<\/strong> Run the following command, but replace <span class=\"rnthl rntliteral\">YOUR_USERNAME<\/span> with the username you want to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mosquitto_passwd -c \/etc\/mosquitto\/passwd <strong>YOUR_USERNAME<\/strong><\/code><\/pre>\n\n\n\n<p>I\u2019ll be using the MQTT user <span class=\"rnthl rntliteral\">sara<\/span>, so I run the command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mosquitto_passwd -c \/etc\/mosquitto\/passwd <strong>sara<\/strong><\/code><\/pre>\n\n\n\n<p>When you run the preceding command with the desired username, you\u2019ll be asked to enter a password. No characters will be displayed while you enter the password. Enter the password and memorize the user\/pass combination, <strong>you\u2019ll need it late<\/strong>r in your projects to make a connection with the broker.<\/p>\n\n\n\n<p>This previous command creates a password file called <span class=\"rnthl rntliteral\">passwd<\/span> on the <span class=\"rnthl rntliteral\">\/etc\/mosquitto<\/span> directory. Now, we need to edit the mosquitto configuration file so that it only allows authentication with the username and password we\u2019ve defined.<\/p>\n\n\n\n<p><strong>2)<\/strong> Set the correct permissions in the <span class=\"rnthl rntliteral\">passwd<\/span> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chown mosquitto \/etc\/mosquitto\/passwd<\/code><\/pre>\n\n\n\n<p><strong>3) <\/strong>Run the following command to edit the configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/mosquitto\/mosquitto.conf<\/code><\/pre>\n\n\n\n<p><strong>4)<\/strong> Add the following line at the top of the file (make sure it is at the top of the file, otherwise it won\u2019t work): <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>per_listener_settings true<\/code><\/pre>\n\n\n\n<p><strong>5)<\/strong> Also add the following three lines to allow connection for authenticated users and tell Mosquitto where the username\/password file is located.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>allow_anonymous false\nlistener 1883\npassword_file \/etc\/mosquitto\/passwd<\/code><\/pre>\n\n\n\n<p>Your configuration file will look as follows (the new lines are in bold):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Place your local configuration in \/etc\/mosquitto\/conf.d\/\n#\n# A full description of the configuration file is at \n# \/usr\/share\/doc\/mosquitto\/examples\/mosquitto.conf.example\n\n<strong>per_listener_settings true\n<\/strong>\npid_file \/run\/mosquitto\/mosquitto.pid\n\npersistence true \npersistence_location \/var\/lib\/mosquitto\/ \n\nlog_dest file \/var\/log\/mosquitto\/mosquitto.log\n\ninclude_dir \/etc\/mosquitto\/conf.d \n<strong>allow_anonymous false \nlistener 1883 \npassword_file \/etc\/mosquitto\/passwd<\/strong><\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"484\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-configuration-file.png?resize=750%2C484&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Digital Ocean Mosquitto Configuraiton File\" class=\"wp-image-128384\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-configuration-file.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-mosquitto-configuration-file.png?resize=300%2C194&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p><strong>6)<\/strong> Press <strong>CTRL-X<\/strong>, then <strong>Y<\/strong>, and finally press <strong>Enter <\/strong>to exit and save the changes.<\/p>\n\n\n\n<p><strong>7)<\/strong> Restart Mosquitto for the changes to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart mosquitto<\/code><\/pre>\n\n\n\n<p><strong>8)<\/strong> Wait a few seconds. To check if Mosquitto is running, you can type the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status mosquitto<\/code><\/pre>\n\n\n\n<p>Now, you have Mosquitto MQTT broker installed on the cloud with authentication with username and password enabled.<\/p>\n\n\n\n<p>On your ESP32\/ESP8266 Arduino code, on the MQTT Host, you should use your droplet IP address.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing MQTT Mosquitto Broker Installation<\/h3>\n\n\n\n<p>To test your MQTT broker installation, you can use another terminal window (Terminal window #2) and establish an SSH communication with your server (you can use <a href=\"https:\/\/www.putty.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">PuTTY<\/a> or a similar SSH client). Enter the droplet IP address and try to establish an SSH connection.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"442\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/SSH-Client-Connecting-To-Digital-Ocean-Server-Putty.png?resize=452%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"SSH Client Connecting To Digital Ocean Server Putty\" class=\"wp-image-97786\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/SSH-Client-Connecting-To-Digital-Ocean-Server-Putty.png?w=452&amp;quality=100&amp;strip=all&amp;ssl=1 452w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/SSH-Client-Connecting-To-Digital-Ocean-Server-Putty.png?resize=300%2C293&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/figure><\/div>\n\n\n<p>Login as <span class=\"rnthl rntliteral\">root<\/span> and enter your password.<\/p>\n\n\n\n<p>Then, enter the following command to subscribe to the <span class=\"rnthl rntliteral\">testTopic<\/span> topic. Replace <span class=\"rnthl rntliteral\">user<\/span> with your username and <span class=\"rnthl rntliteral\">pass<\/span> with your password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -h localhost -t testTopic -u <strong>user<\/strong> -P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>In your Terminal window #1, use the next command to publish the message &#8220;<strong>Hello, world!<\/strong>&#8221; in the <span class=\"rnthl rntliteral\">test<\/span> topic. Replace <span class=\"rnthl rntliteral\">user<\/span> with your username and <span class=\"rnthl rntliteral\">pass<\/span> with your password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -h localhost -t testTopic -m \"Hello, world!\" -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>Terminal window #2 should receive the message.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-testing-mosquitto-broker.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"900\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-testing-mosquitto-broker.png?resize=750%2C900&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-128385\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-testing-mosquitto-broker.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/digitalocean-testing-mosquitto-broker.png?resize=250%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p>You can use the next table as a reference for the parameters you can pass in <span class=\"rnthl rntliteral\">mosquitto_sub<\/span> and <span class=\"rnthl rntliteral\">mosquitto_pub<\/span> commands:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>-h<\/strong><\/td><td>Hostname<\/td><\/tr><tr><td><strong>-t<\/strong><\/td><td>MQTT topic<\/td><\/tr><tr><td><strong>-m<\/strong><\/td><td>MQTT message<\/td><\/tr><tr><td><strong>-u<\/strong><\/td><td>MQTT username<\/td><\/tr><tr><td><strong>-P<\/strong><\/td><td>MQTT Password<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting Your ESP32 to MQTT Mosquitto Broker<\/h2>\n\n\n\n<p>We often use our <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">ESP32 <\/a>and <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">ESP8266<\/a> boards in our MQTT projects. So, we&#8217;ll show you how you can connect the ESP32 board to your Cloud MQTT Broker\u2014it&#8217;s the same for an ESP8266 board, just make sure you use the ESP8266 specific functions. <\/p>\n\n\n\n<p>Before proceeding with this tutorial, make sure you complete the following prerequisites.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arduino IDE<\/h3>\n\n\n\n<p>We\u2019ll program the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 board<\/a> using Arduino IDE, so make sure you have the ESP32 add-on installed.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">MQTT Libraries<\/h3>\n\n\n\n<p>To use MQTT with the ESP32 we\u2019ll use the&nbsp;<a href=\"https:\/\/github.com\/marvinroger\/async-mqtt-client\" target=\"_blank\" rel=\"noreferrer noopener\">Async MQTT Client Library<\/a>.<\/p>\n\n\n\n<p><strong>Installing the Async MQTT Client Library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/marvinroger\/async-mqtt-client\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the Async MQTT client library<\/a>. You should have a .zip folder in your <em>Downloads <\/em>folder<\/li>\n\n\n\n<li>Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Add . ZIP<\/strong>&nbsp;library and select the library you\u2019ve just downloaded.<\/li>\n<\/ol>\n\n\n\n<p><strong>Installing the Async TCP Library<\/strong><\/p>\n\n\n\n<p>To use MQTT with the ESP, you also need the&nbsp;<a href=\"https:\/\/github.com\/me-no-dev\/AsyncTCP\" target=\"_blank\" rel=\"noreferrer noopener\">Async TCP library<\/a>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/me-no-dev\/AsyncTCP\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the Async TCP client library<\/a>. You should have a .zip folder in your Downloads folder<\/li>\n\n\n\n<li>Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Add . ZIP<\/strong>&nbsp;library and select the library you\u2019ve just downloaded.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32 MQTT Publish Messages to Cloud MQTT Broker<\/h3>\n\n\n\n<p>Copy the following code to your Arduino IDE. To make it work for you, you need to insert your network credentials as well as the MQTT broker details (your Digital Ocean Droplet&#8217;s IP Address, broker username and password).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n#include &lt;WiFi.h&gt;\nextern &quot;C&quot; {\n  #include &quot;freertos\/FreeRTOS.h&quot;\n  #include &quot;freertos\/timers.h&quot;\n}\n#include &lt;AsyncMqttClient.h&gt;\n\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n\/\/ Digital Ocean MQTT Mosquitto Broker\n#define MQTT_HOST IPAddress(XXX, XXX, XXX, XXX)\n\/\/ For a cloud MQTT broker, type the domain name\n\/\/#define MQTT_HOST &quot;example.com&quot;\n#define MQTT_PORT 1883\n\n#define MQTT_USERNAME &quot;REPLACE_WITH_YOUR_MQTT_USER&quot;\n#define MQTT_PASSWORD &quot;REPLACE_WITH_YOUR_MQTT_PASSWORD&quot;\n\n\/\/ Test MQTT Topic\n#define MQTT_PUB_TEST &quot;test&quot;\n\nAsyncMqttClient mqttClient;\nTimerHandle_t mqttReconnectTimer;\nTimerHandle_t wifiReconnectTimer;\n\nunsigned long previousMillis = 0;   \/\/ Stores last time temperature was published\nconst long interval = 5000;         \/\/ Interval at which to publish sensor readings\n\nint i = 0;\n\nvoid connectToWifi() {\n  Serial.println(&quot;Connecting to Wi-Fi...&quot;);\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n}\n\nvoid connectToMqtt() {\n  Serial.println(&quot;Connecting to MQTT...&quot;);\n  mqttClient.connect();\n}\n\nvoid WiFiEvent(WiFiEvent_t event) {\n  Serial.printf(&quot;[WiFi-event] event: %d\\n&quot;, event);\n  switch(event) {\n    case ARDUINO_EVENT_WIFI_STA_GOT_IP:\n      Serial.println(&quot;WiFi connected&quot;);\n      Serial.println(&quot;IP address: &quot;);\n      Serial.println(WiFi.localIP());\n      connectToMqtt();\n      break;\n    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:\n      Serial.println(&quot;WiFi lost connection&quot;);\n      xTimerStop(mqttReconnectTimer, 0); \/\/ ensure we don't reconnect to MQTT while reconnecting to Wi-Fi\n      xTimerStart(wifiReconnectTimer, 0);\n      break;\n  }\n}\n\nvoid onMqttConnect(bool sessionPresent) {\n  Serial.println(&quot;Connected to MQTT.&quot;);\n  Serial.print(&quot;Session present: &quot;);\n  Serial.println(sessionPresent);\n}\n\nvoid onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {\n  Serial.println(&quot;Disconnected from MQTT.&quot;);\n  if (WiFi.isConnected()) {\n    xTimerStart(mqttReconnectTimer, 0);\n  }\n}\n\n\/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {\n  Serial.println(&quot;Subscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n  Serial.print(&quot;  qos: &quot;);\n  Serial.println(qos);\n}\nvoid onMqttUnsubscribe(uint16_t packetId) {\n  Serial.println(&quot;Unsubscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}*\/\n\nvoid onMqttPublish(uint16_t packetId) {\n  Serial.print(&quot;Publish acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println();\n\n  mqttReconnectTimer = xTimerCreate(&quot;mqttTimer&quot;, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast&lt;TimerCallbackFunction_t&gt;(connectToMqtt));\n  wifiReconnectTimer = xTimerCreate(&quot;wifiTimer&quot;, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast&lt;TimerCallbackFunction_t&gt;(connectToWifi));\n\n  WiFi.onEvent(WiFiEvent);\n\n  mqttClient.onConnect(onMqttConnect);\n  mqttClient.onDisconnect(onMqttDisconnect);\n  \/*mqttClient.onSubscribe(onMqttSubscribe);\n  mqttClient.onUnsubscribe(onMqttUnsubscribe);*\/\n  mqttClient.onPublish(onMqttPublish);\n  mqttClient.setServer(MQTT_HOST, MQTT_PORT);\n  \/\/ If your broker requires authentication (username and password), set them below\n  mqttClient.setCredentials(MQTT_USERNAME, MQTT_PASSWORD);\n  connectToWifi();\n}\n\nvoid loop() {\n  unsigned long currentMillis = millis();\n  \/\/ Every X number of seconds (interval = 5 seconds) \n  \/\/ it publishes a new MQTT message\n  if (currentMillis - previousMillis &gt;= interval) {\n    \/\/ Save the last time a new reading was published\n    previousMillis = currentMillis;\n    \n    String testString = &quot;Hello, world! #&quot; + String(i);\n    \/\/ Publish an MQTT message on topic test\n    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEST, 1, true, String(testString).c_str());                            \n    Serial.printf(&quot;Publishing on topic %s at QoS 1, packetId: %i&quot;, MQTT_PUB_TEST, packetIdPub1);\n    Serial.printf(&quot; Message: %.2f \\n&quot;, testString);\n    i++;\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_MQTT\/ESP32_Test_Pub.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Type your network credentials on the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert the Digital Ocean Droplet IP address, so that the ESP32 connects to your broker (in my case, it is 178.62.83.231).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_HOST IPAddress(178, 62, 83, 231)<\/code><\/pre>\n\n\n\n<p>If your broker requires authentication, type your MQTT username and MQTT password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_USERNAME \"YOUR_USER\"\n#define MQTT_PASSWORD \"YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing ESP32 MQTT Publishing Messages<\/h3>\n\n\n\n<p>If you have your ESP32 running the uploaded code and you open your Arduino IDE Serial monitor, you&#8217;ll see that your ESP32 is publishing new messages every 5 seconds.<\/p>\n\n\n\n<p>Establish an SSH connection with your cloud server (using PuTTY, for example) and type (replace <span class=\"rnthl rntliteral\">user<\/span> with your username and <span class=\"rnthl rntliteral\">pass<\/span> with your password.):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -h localhost -t test -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>You should start receiving new MQTT messages published by your ESP32.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"291\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/21-ESP32-ESP8266-Subscribe-to-MQTT-topic-receive-Message-Cloud-Mosquitto-MQTT-Broker.png?resize=837%2C291&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Subscribe to MQTT topic receive Message Cloud Mosquitto MQTT Broker\" class=\"wp-image-97710\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/21-ESP32-ESP8266-Subscribe-to-MQTT-topic-receive-Message-Cloud-Mosquitto-MQTT-Broker.png?w=837&amp;quality=100&amp;strip=all&amp;ssl=1 837w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/21-ESP32-ESP8266-Subscribe-to-MQTT-topic-receive-Message-Cloud-Mosquitto-MQTT-Broker.png?resize=300%2C104&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/21-ESP32-ESP8266-Subscribe-to-MQTT-topic-receive-Message-Cloud-Mosquitto-MQTT-Broker.png?resize=768%2C267&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 837px) 100vw, 837px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Cloud MQTT Broker Publish Messages to ESP32<\/h3>\n\n\n\n<p>The next sketch makes the ESP32 subscribe to a cloud MQTT topic to receive messages. Copy it to your Arduino IDE, then insert your network credentials as well as the MQTT broker details (your Digital Ocean Droplet&#8217;s IP Address and the broker username and password).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n#include &lt;WiFi.h&gt;\nextern &quot;C&quot; {\n  #include &quot;freertos\/FreeRTOS.h&quot;\n  #include &quot;freertos\/timers.h&quot;\n}\n#include &lt;AsyncMqttClient.h&gt;\n\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n\/\/ Digital Ocean MQTT Mosquitto Broker\n#define MQTT_HOST IPAddress(XXX, XXX, XXX, XXX)\n\/\/ For a cloud MQTT broker, type the domain name\n\/\/#define MQTT_HOST &quot;example.com&quot;\n#define MQTT_PORT 1883\n\n#define MQTT_USERNAME &quot;REPLACE_WITH_YOUR_MQTT_USER&quot;\n#define MQTT_PASSWORD &quot;REPLACE_WITH_YOUR_MQTT_PASSWORD&quot;\n\n\/\/ Test MQTT Topic\n#define MQTT_SUB_TEST &quot;test&quot;\n\nAsyncMqttClient mqttClient;\nTimerHandle_t mqttReconnectTimer;\nTimerHandle_t wifiReconnectTimer;\n\nunsigned long previousMillis = 0;   \/\/ Stores last time temperature was published\nconst long interval = 5000;         \/\/ Interval at which to publish sensor readings\n\nint i = 0;\n\nvoid connectToWifi() {\n  Serial.println(&quot;Connecting to Wi-Fi...&quot;);\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n}\n\nvoid connectToMqtt() {\n  Serial.println(&quot;Connecting to MQTT...&quot;);\n  mqttClient.connect();\n}\n\nvoid WiFiEvent(WiFiEvent_t event) {\n  Serial.printf(&quot;[WiFi-event] event: %d\\n&quot;, event);\n  switch(event) {\n    case ARDUINO_EVENT_WIFI_STA_GOT_IP:\n      Serial.println(&quot;WiFi connected&quot;);\n      Serial.println(&quot;IP address: &quot;);\n      Serial.println(WiFi.localIP());\n      connectToMqtt();\n      break;\n    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:\n      Serial.println(&quot;WiFi lost connection&quot;);\n      xTimerStop(mqttReconnectTimer, 0); \/\/ ensure we don't reconnect to MQTT while reconnecting to Wi-Fi\n      xTimerStart(wifiReconnectTimer, 0);\n      break;\n  }\n}\n\n\/\/ Add more topics that want your ESP to be subscribed to\nvoid onMqttConnect(bool sessionPresent) {\n  Serial.println(&quot;Connected to MQTT.&quot;);\n  Serial.print(&quot;Session present: &quot;);\n  Serial.println(sessionPresent);\n  \n  \/\/ ESP subscribed to test topic\n  uint16_t packetIdSub = mqttClient.subscribe(MQTT_SUB_TEST, 0);\n  Serial.println(&quot;Subscribing at QoS 0&quot;);\n}\n\nvoid onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {\n  Serial.println(&quot;Disconnected from MQTT.&quot;);\n  if (WiFi.isConnected()) {\n    xTimerStart(mqttReconnectTimer, 0);\n  }\n}\n\nvoid onMqttSubscribe(uint16_t packetId, uint8_t qos) {\n  Serial.println(&quot;Subscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n  Serial.print(&quot;  qos: &quot;);\n  Serial.println(qos);\n}\n\nvoid onMqttUnsubscribe(uint16_t packetId) {\n  Serial.println(&quot;Unsubscribe acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}\n\n\/\/ You can modify this function to handle what happens when you receive a certain message in a specific topic\nvoid onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {\n  String messageTemp;\n  for (int i = 0; i &lt; len; i++) {\n    \/\/Serial.print((char)payload[i]);\n    messageTemp += (char)payload[i];\n  }\n  \/\/ Check if the MQTT message was received on topic test\n  if (strcmp(topic, MQTT_SUB_TEST) == 0) {\n    Serial.println(&quot;TRUE&quot;);\n  }\n \n  Serial.println(&quot;Publish received.&quot;);\n  Serial.print(&quot;  message: &quot;);\n  Serial.println(messageTemp);\n  Serial.print(&quot;  topic: &quot;);\n  Serial.println(topic);\n  Serial.print(&quot;  qos: &quot;);\n  Serial.println(properties.qos);\n  Serial.print(&quot;  dup: &quot;);\n  Serial.println(properties.dup);\n  Serial.print(&quot;  retain: &quot;);\n  Serial.println(properties.retain);\n  Serial.print(&quot;  len: &quot;);\n  Serial.println(len);\n  Serial.print(&quot;  index: &quot;);\n  Serial.println(index);\n  Serial.print(&quot;  total: &quot;);\n  Serial.println(total);\n}\n\n\/*void onMqttPublish(uint16_t packetId) {\n  Serial.print(&quot;Publish acknowledged.&quot;);\n  Serial.print(&quot;  packetId: &quot;);\n  Serial.println(packetId);\n}*\/\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println();\n\n  mqttReconnectTimer = xTimerCreate(&quot;mqttTimer&quot;, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast&lt;TimerCallbackFunction_t&gt;(connectToMqtt));\n  wifiReconnectTimer = xTimerCreate(&quot;wifiTimer&quot;, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast&lt;TimerCallbackFunction_t&gt;(connectToWifi));\n\n  WiFi.onEvent(WiFiEvent);\n\n  mqttClient.onConnect(onMqttConnect);\n  mqttClient.onDisconnect(onMqttDisconnect);\n  mqttClient.onSubscribe(onMqttSubscribe);\n  mqttClient.onUnsubscribe(onMqttUnsubscribe);\n  \/\/mqttClient.onPublish(onMqttPublish);\n  mqttClient.onMessage(onMqttMessage);\n  mqttClient.setServer(MQTT_HOST, MQTT_PORT);\n  \/\/ If your broker requires authentication (username and password), set them below\n  mqttClient.setCredentials(MQTT_USERNAME, MQTT_PASSWORD);\n  connectToWifi();\n}\n\nvoid loop() {\n  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_MQTT\/ESP32_Test_Sub.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Type your network credentials on the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert the Digital Ocean Droplet IP address, so that the ESP32 connects to your broker (in my case, it is 178.62.83.231).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_HOST IPAddress(178, 62, 83, 231)<\/code><\/pre>\n\n\n\n<p>If your broker requires authentication, type your MQTT username and MQTT password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define MQTT_USERNAME \"YOUR_USER\"\n#define MQTT_PASSWORD \"YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing ESP32 Subscribe to MQTT Topic<\/h3>\n\n\n\n<p>To test if your ESP32 is receiving MQTT messages, in your Digital Ocean console start publishing different messages (for example &#8220;<strong>Hi #1!<\/strong>&#8220;, &#8220;<strong>Hi #2!<\/strong>&#8220;, etc). Replace <span class=\"rnthl rntliteral\">user<\/span> with your username and <span class=\"rnthl rntliteral\">pass<\/span> with your password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -h localhost -t test -m \"Hi #1!\" -u <strong>user <\/strong>-P <strong>pass<\/strong>\nmosquitto_pub -h localhost -t test -m \"Hi #2!\" -u <strong>user <\/strong>-P <strong>pass<\/strong>\nmosquitto_pub -h localhost -t test -m \"Hi #3!\" -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"957\" height=\"237\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/22-Cloud-Mosquitto-MQTT-Broker-Publish-MQTT-Message-Topic-to-ESP32-ESP8266-NodeMCU.png?resize=957%2C237&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Cloud Mosquitto MQTT Broker Publish MQTT Message Topic to ESP32 ESP8266 NodeMCU\" class=\"wp-image-97711\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/22-Cloud-Mosquitto-MQTT-Broker-Publish-MQTT-Message-Topic-to-ESP32-ESP8266-NodeMCU.png?w=957&amp;quality=100&amp;strip=all&amp;ssl=1 957w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/22-Cloud-Mosquitto-MQTT-Broker-Publish-MQTT-Message-Topic-to-ESP32-ESP8266-NodeMCU.png?resize=300%2C74&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/22-Cloud-Mosquitto-MQTT-Broker-Publish-MQTT-Message-Topic-to-ESP32-ESP8266-NodeMCU.png?resize=768%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 957px) 100vw, 957px\" \/><\/figure><\/div>\n\n\n<p>Your ESP32 should receive each message and print it in the Serial Monitor, as shown in the image below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"800\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/23-ESP32-ESP8266-NodeMCU-Subscribed-to-MQTT-topic-Receive-MQTT-message-Cloud-MQTT-broker.png?resize=988%2C800&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Subscribed to MQTT topic Receive MQTT message Cloud MQTT broker\" class=\"wp-image-97712\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/23-ESP32-ESP8266-NodeMCU-Subscribed-to-MQTT-topic-Receive-MQTT-message-Cloud-MQTT-broker.png?w=988&amp;quality=100&amp;strip=all&amp;ssl=1 988w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/23-ESP32-ESP8266-NodeMCU-Subscribed-to-MQTT-topic-Receive-MQTT-message-Cloud-MQTT-broker.png?resize=300%2C243&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/23-ESP32-ESP8266-NodeMCU-Subscribed-to-MQTT-topic-Receive-MQTT-message-Cloud-MQTT-broker.png?resize=768%2C622&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/figure><\/div>\n\n\n<p>In these quick examples, we&#8217;ve shown you how to publish and subscribe MQTT messages using the Cloud MQTT broker. The idea is to use several ESP32 or ESP8266 boards that publish and subscribe to the same topics to communicate with each other and\/or use <a href=\"https:\/\/randomnerdtutorials.com\/access-node-red-dashboard-anywhere-digital-ocean\/\">Node-RED on the cloud<\/a> to interact with those boards.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">(Optional) Taking It Further &#8211; MQTT Mosquitto Broker Encrypted Requests<\/h2>\n\n\n\n<p>The best method to add an SSL certificate to your server is by having a domain name pointed at your server and using Let\u2019s Encrypt certificates. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You&#8217;ll have to buy a domain name and point it to Digital Ocean Name Servers.<\/li>\n\n\n\n<li>You&#8217;ll also need to follow these next instructions to <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-use-certbot-standalone-mode-to-retrieve-let-s-encrypt-ssl-certificates-on-ubuntu-1804\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">prepare Certbot Standalone Mode to Retrieve Let&#8217;s Encrypt SSL Certificates on Ubuntu<\/a> (this guide also works with Ubuntu 20.04).<\/li>\n<\/ul>\n\n\n\n<p>Having a domain name and Let&#8217;s Encrypt SSL Certificates ready, follow the next instructions to secure your Mosquitto broker. <\/p>\n\n\n\n<p>To enable SSL encryption, we need to tell Mosquitto where our Let\u2019s Encrypt certificates are stored. Open up the configuration file we previously started:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/mosquitto\/mosquitto.conf<\/code><\/pre>\n\n\n\n<p>Add the next lines to make your default.conf add the Let\u2019s Encrypt certificates.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>allow_anonymous false\npassword_file \/etc\/mosquitto\/passwd\n\nlistener 1883 localhost\n\nlistener 8883\ncertfile \/etc\/letsencrypt\/live\/example.com\/cert.pem\ncafile \/etc\/letsencrypt\/live\/example.com\/chain.pem\nkeyfile \/etc\/letsencrypt\/live\/example.com\/privkey.pem<\/code><\/pre>\n\n\n\n<p>Listener 1883 is the standard unencrypted MQTT port. The localhost instructs Mosquitto to only bind this port to the localhost interface, so it\u2019s not longer accessible externally.<\/p>\n\n\n\n<p>On the other hand, listener 8883 sets up an encrypted listener on port 8883. The next three lines point Mosquitto to the appropriate Let\u2019s Encrypt files to set up the encrypted connections.<\/p>\n\n\n\n<p>Save and exit the file (Ctrl+X, Y, Enter key), then restart Mosquitto to update the settings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart mosquitto<\/code><\/pre>\n\n\n\n<p>Update the firewall to allow connections to port 8883.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 8883<\/code><\/pre>\n\n\n\n<p>Now, you subscribe to the <span class=\"rnthl rntliteral\">test<\/span> MQTT topic in the encrypted port (8883). Don&#8217;t forget to replace example.com with your domain name in the subscribe and publish commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_sub -h example.com -t test -p 8883 --capath \/etc\/ssl\/certs\/ -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>You can publish encrypted messages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mosquitto_pub -h example.com -t test -m \"Secure message\" -p 8883 --capath \/etc\/ssl\/certs\/ -u <strong>user <\/strong>-P <strong>pass<\/strong><\/code><\/pre>\n\n\n\n<p>With this setup, you&#8217;ll need to prepare your ESP32\/ESP8266 to make encrypted MQTT requests on port 8883.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This complete guide was tested and it should work. There are many steps and they must be followed exactly as we describe in the right order. Otherwise, something might not work properly.<\/p>\n\n\n\n<p>In all our guides and projects we always try to help if anyone gets stuck. However, in this particular case, there are so many steps that it can be tough to help you without having access to the server and testing it (of course, we don&#8217;t have the resources to help everyone personally). <\/p>\n\n\n\n<p>If you have any problem installing Mosquitto MQTT broker, preparing your Linux Ubuntu server, running Node-RED, or installing an SSL certificate, contact Digital Ocean support and describe exactly what&#8217;s happening. I&#8217;ve been using their service since 2015 and they always have an extremely helpful support team (or just use their Forum).<\/p>\n\n\n\n<p>Now, if you want to install Node-RED on Digital Ocean, follow the next tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/access-node-red-dashboard-anywhere-digital-ocean\/\">Access Node-RED Dashboard from Anywhere using Digital Ocean<\/a>.<\/p>\n\n\n\n<p>If you like this type of project, make sure you take a look at our SMART HOME course, where you&#8217;ll learn how to setup a home automation system using MQTT, Node-RED, InfluxDB, and much more:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\"><strong>SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p>Read the next guides to learn more about MQTT:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-subscribe-arduino-ide\/\">ESP32 MQTT \u2013 Publish and Subscribe with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-bme280-arduino\/\">ESP32 MQTT \u2013 Publish BME280 Sensor Readings (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean\/#more-97731\" aria-label=\"Read more about Run Your Cloud MQTT Mosquitto Broker (access from anywhere using Digital Ocean)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":128392,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[304,225],"tags":[],"class_list":["post-97731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-home-automation","category-home-automation"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/06\/Install-Cloud-Mosquitto-MQTT-Broker-Digital-Ocean.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=97731"}],"version-history":[{"count":8,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97731\/revisions"}],"predecessor-version":[{"id":150081,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97731\/revisions\/150081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/128392"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=97731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=97731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=97731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}