Custom MQTT broker

NOTE: The following how-to guide applies only to IoT (MQTT) connection type.

By default, GUI-O application uses a MQTT test broker that is running on our own server. The broker listens on ports 1883 (unencrypted), 8883 (encrypted with client certificates) and 8884 (encrypted). This is perfect for testing and some smaller use cases, but some IoT scenarios require using a broker that gives you finer control over additional settings (e.g., managing credentials for different clients).

This how-to guide focuses on migrating from GUI-O broker to a self-managed broker running on Linux or Windows. This enables full control over all configuration settings, but comes with the cost of increased management complexity.

Linux Mosquitto MQTT broker

The following tutorial shows how to setup Mosquitto broker on Linux based devices.

Prerequisites

IMPORTANT NOTE #1: It is necessary to ask your ISP for static IP configuration. The certificate in the following steps will be issued for a specific IP. If you do not use static IP configuration, your ISP can change the IP address without notice and the connection to the MQTT broker will not work.

IMPORTANT NOTE #2: It is generally recommended that a host name is used instead of IP address, but this requires registering the host name with a domain name registrar and setting up some additional settings.

Step 1: Install Mosquitto broker

sudo apt install mosquitto

Step 2: Determine your external IP address

curl ifconfig.me; echo

This will output your external IP, which is needed when issuing the server certificate. You can alternatively navigate to: https://whatismyipaddress.com/.

Step 3: Create self-signed certificate for certificate authority (CA)

openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt

IMPORTANT NOTE: The "ca.key" file should be kept secure and not shared with anyone.

The "days" value is in this case set to 365 (1 year) and denotes the certificate validity. This value can be changed based on your requirements. After the command is executed, you will be prompted to enter a pass phrase and additional certificate information such as country code, state name, city, etc.

openssl x509 -noout -text -in ca.crt

Step 4: Create a server certificate and sign it with CA

nano server.cnf

Add the following content and replace the "[dn]" and "[alt_names]" sections with your information (use the external IP address obtained in Step 2):


[req]

default_bits = 2048

prompt = no

default_md = sha256

distinguished_name = dn

req_extensions = req_ext


[dn]

C = COUNTRY_CODE_HERE

ST = STATE_HERE

L = CITY_HERE

O = ORGANIZATION_HERE

OU = ORGANIZATION_UNIT_HERE

CN = IP_ADDRESS_HERE


[req_ext]

subjectAltName = @alt_names


[alt_names]

IP.1 = IP_ADDRESS_HERE

NOTE: To save and exit "nano" text editor, press "Ctrl+x", then "y" and "Enter" key.

openssl genrsa -out server.key 2048

openssl req -new -key server.key -out server.csr -config server.cnf

openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256

The "days" value is in this case set to 365 (1 year) and denotes the certificate validity. This value can be changed based on your requirements. After the command is executed, you will be prompted to enter the pass phrase for "ca.key" (this is the pass phrase that was set in Step 3).

sudo chmod a+r server.key

Step 5: Copy the certificate to your Android device

Step 6: Move the files to proper locations

sudo mv ca.crt ca.key /etc/mosquitto/ca_certificates/

sudo mv server.crt server.key /etc/mosquitto/certs/

Step 7: Setup broker configuration

sudo nano /etc/mosquitto/conf.d/default.conf

Add the following content:


# Listener

listener 8883

cafile /etc/mosquitto/ca_certificates/ca.crt

certfile /etc/mosquitto/certs/server.crt

keyfile /etc/mosquitto/certs/server.key

require_certificate false

use_identity_as_username false


# TLS

tls_version tlsv1.2


# Security

allow_anonymous false

password_file /etc/mosquitto/passwd


# Logging

#log_type error

#log_type warning

log_type all

NOTE: To save and exit "nano" text editor, press "Ctrl+x", then "y" and "Enter" key.

sudo mosquitto_passwd -b /etc/mosquitto/passwd USER PASS

NOTE: You can add more users with different credentials using this command.

sudo systemctl restart mosquitto.service

sudo systemctl status mosquitto.service

Step 8: Configure port forwarding rules for your router

ifconfig

This command will output the MAC address after the "ether" keyword (formatted as xx:xx:xx:xx:xx:xx).

Step 9: Setup GUI-O application and connect to Mosquitto broker

(Optional) Step 10: Setup ESP32 and connect to Mosquitto broker

cat /etc/mosquitto/ca_certificates/ca.crt

Copy the certificate displayed in the terminal and replace the one in BasicMQTT_Mosquitto.ino source code (keep same certificate formatting).

NOTE: Do not forget to generate and set the publish and subscribe topics!

Windows Mosquitto MQTT broker

The following tutorial shows how to setup Mosquitto broker on Windows based devices.

Prerequisites

Download light version here and install. Install OpenSSL directly to the main partition (e.g., "C:\OpenSSL-Win64"). When prompted copy DLLs to OpenSSL binaries directory.

IMPORTANT NOTE #1: It is necessary to ask your ISP for static IP configuration. The certificate in the following steps will be issued for a specific IP. If you do not use static IP configuration, your ISP can change the IP address without notice and the connection to the MQTT broker will not work.

IMPORTANT NOTE #2: It is generally recommended that a host name is used instead of IP address, but this requires registering the host name with a domain name registrar and setting up some additional settings.

Step 1: Download and install Mosquitto broker

Step 2: Determine your external IP address

curl ifconfig.me

This will output your external IP, which is needed when issuing the server certificate. You can alternatively navigate to: https://whatismyipaddress.com/.

Step 3: Create self-signed certificate for certificate authority (CA)

cd C:\mosquitto

mkdir certs

cd certs

C:\OpenSSL-Win64\bin\openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt

IMPORTANT NOTE: The "ca.key" file should be kept secure and not shared with anyone.

The "days" value is in this case set to 365 (1 year) and denotes the certificate validity. This value can be changed based on your requirements. After the command is executed, you will be prompted to enter a pass phrase and additional certificate information such as country code, state name, city, etc.

C:\OpenSSL-Win64\bin\openssl x509 -noout -text -in ca.crt

Step 4: Create a server certificate and sign it with CA

cd. > server.cnf

Add the following content and replace the "[dn]" and "[alt_names]" sections with your information (use the external IP address obtained in Step 2):


[req]

default_bits = 2048

prompt = no

default_md = sha256

distinguished_name = dn

req_extensions = req_ext


[dn]

C = COUNTRY_CODE_HERE

ST = STATE_HERE

L = CITY_HERE

O = ORGANIZATION_HERE

OU = ORGANIZATION_UNIT_HERE

CN = IP_ADDRESS_HERE


[req_ext]

subjectAltName = @alt_names


[alt_names]

IP.1 = IP_ADDRESS_HERE

C:\OpenSSL-Win64\bin\openssl genrsa -out server.key 2048

C:\OpenSSL-Win64\bin\openssl req -new -key server.key -out server.csr -config server.cnf

C:\OpenSSL-Win64\bin\openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256

The "days" value is in this case set to 365 (1 year) and denotes the certificate validity. This value can be changed based on your requirements. After the command is executed, you will be prompted to enter the pass phrase for "ca.key" (this is the pass phrase that was set in Step 3).

Step 5: Copy the certificate to your Android device

Step 6: Setup broker configuration

Find the "Listeners" section and set:


listener 8883

Find the "Certificate based SSL/TLS support" section and set:


cafile C:\mosquitto\certs\ca.crt

certfile C:\mosquitto\certs\server.crt

keyfile C:\mosquitto\certs\server.key

require_certificate false

use_identity_as_username false

tls_version tlsv1.2

Find the "Security" section and set:


allow_anonymous false

password_file C:\mosquitto\pwfile.example

Save the file.

C:\mosquitto\mosquitto_passwd -b C:\mosquitto\pwfile.example USER PASS

NOTE: You can add more users with different credentials using this command.

C:\mosquitto\mosquitto install

Step 7: Configure port forwarding rules for your router

ipconfig /all

This command will output the MAC address / "Physical Address" (formatted as xx-xx-xx-xx-xx-xx).

Step 8: Setup GUI-O application and connect to Mosquitto broker

(Optional) Step 9: Setup ESP32 and connect to Mosquitto broker

NOTE: Do not forget to generate and set the publish and subscribe topics!

Connect . Create . Control

Connect . Create . Control

YouTube channel
Facebook