Install and configure a local Mosquitto MQTT broker on VirtualBox and register a cloud IoT platform channel.
Environment
VirtualBox / Linux (Ubuntu) / Cloud
Difficulty
⭐⭐☆☆☆ (Moderate)
Course Module
IoT Networking
Deliverables
Local Mosquitto Run, ThingSpeak Channel
1. Local & Cloud Communication Network Topology
To enable network-level interactions, we transition from direct serial cable connections to wireless TCP/IP sockets. Below is the network bridge schema. The VirtualBox VM's network card is configured in "Bridged Adapter" mode, giving the guest OS its own IP address on the local home router. This allows microcontrollers to target the VM's Mosquitto broker on port 1883. For remote access, data is pushed to the public cloud (ThingSpeak) using REST API calls over port 80/443.
2. Part 1: Step-by-Step Individual Installation Commands & VM Configurations
Follow these detailed steps to switch your VirtualBox adapter, install Mosquitto, configure listeners, and create a cloud API channel.
STEP 1
Configure VM Network Card to Bridged Mode
Shut down your VM. Modify the network driver settings in VirtualBox to attach the guest OS directly to your physical local network.
VirtualBox GUI Configuration:
1. Open the Oracle VM VirtualBox manager window on Windows.
2. Select your VM name ("Ubuntu-IoT") in the left column.
3. Click the orange "Settings" gear icon in the top horizontal toolbar.
4. In the Settings window, click the "Network" menu tab in the left list.
5. Under "Adapter 1", locate the "Attached to:" dropdown field.
6. Change the selection from "NAT" to "Bridged Adapter".
7. Under the "Name:" dropdown, select your physical computer's network interface (e.g. your Intel/Realtek Wi-Fi or Ethernet Controller). Click "OK".
8. Start the VM.
We change the network card configuration to Bridged Adapter to bypass NAT virtualization, allowing the host router to assign a unique local IP address to the virtual machine.
STEP 2
Install Mosquitto Broker on Ubuntu Guest VM
Open a terminal window inside the Ubuntu VM (Ctrl+Alt+T) and run the commands to install the Mosquitto broker and package clients.
We run `apt install` to load the `mosquitto` broker daemon and `mosquitto-clients` testing packages, deploying local command tools like `mosquitto_pub` and `mosquitto_sub`.
STEP 3
Configure Mosquitto to Accept Remote Socket Connections
By default, Mosquitto blocks external traffic. Edit the configuration file to open listener ports on all network interfaces.
We run `nano` as root to open the Mosquitto configuration file, allowing us to edit the system parameters and open network paths.
STEP 4
Append Listener Rules to the Configuration File
Scroll down to the very end of the file in the nano editor. Add the listener port and anonymous connection rules.
Configuration text to append (use arrow keys to scroll, type, then press Ctrl+O, Enter, Ctrl+X):
listener 1883 0.0.0.0
allow_anonymous true
We add `listener 1883 0.0.0.0` to instruct the broker to bind to port 1883 on all local interfaces, and enable anonymous access to bypass initial credential checks.
STEP 5
Restart the Mosquitto Daemon Process
Reload the background Mosquitto system service to apply the modified configuration rules.
ubuntu@iot-vm:~$ sudo systemctl restart mosquitto
We run `systemctl restart` to restart the Mosquitto daemon, forcing the process to read our modified config file and bind to port 1883.
STEP 6
Identify the VM's Local IP Address
Query the network interfaces inside the VM to find the assigned IP address, which you will use in your ESP32 configuration.
ubuntu@iot-vm:~$ ip a show dev enp0s3
We run `ip a` to view the parameters of our virtual ethernet card, locating the local IP address (typically `192.168.1.X`) assigned by the router.
STEP 7
Verify Local Broker Message Transmission
Open a second terminal window. In the first window, subscribe to a test topic. In the second window, publish a payload to verify the broker forwards messages correctly.
Terminal 1 (Subscriber):
ubuntu@iot-vm:~$ mosquitto_sub -h localhost -t "test/topic"
Terminal 2 (Publisher - run in separate window):
ubuntu@iot-vm:~$ mosquitto_pub -h localhost -t "test/topic" -m "Hello IoT!"
We run `mosquitto_sub` and `mosquitto_pub` to perform a local loopback test, verifying that the broker successfully routes published messages to active subscribers.
STEP 8
Create a Channel on ThingSpeak Cloud Platform
Register a free developer account on the MathWorks ThingSpeak platform and configure a channel to receive sensor data.
Cloud Configuration Steps:
1. Navigate to https://thingspeak.com in your web browser.
2. Click "Get Started For Free" and register a new account.
3. Once logged in, click "Channels" -> "My Channels" in the top menu.
4. Click the green "New Channel" button.
5. Name the channel "IoT Telemetry".
6. Check the boxes next to Field 1 (label: "Temperature") and Field 2 (label: "Light").
7. Scroll down to the bottom of the page and click "Save Channel".
8. Click on the "API Keys" tab. Copy the "Write API Key" string and save it.
We create a ThingSpeak channel and generate API keys to allocate cloud storage and configure HTTP endpoints that will receive and index our sensor data.
3. Operational Telemetry Pipeline Flow
The telemetry pipeline handles data transmission across local networks and remote APIs. The workflow diagram below outlines how sensor data is formatted, sent to the local VM broker, and written to the public cloud.
4. Part 2: Complete Broker Configuration File Template
To configure your local environment, copy the template below and save it to the specified location on your virtual machine. This replaces the default configuration with our custom listener rules.
# Template Configuration for Eclipse Mosquitto Broker# Save to: /etc/mosquitto/mosquitto.conf on VirtualBox Ubuntu Guest OS# -----------------------------------------------------------------# Persistence settings# -----------------------------------------------------------------persistencetruepersistence_location/var/lib/mosquitto/# -----------------------------------------------------------------# Logging configuration# -----------------------------------------------------------------log_destfile /var/log/mosquitto/mosquitto.loglog_deststdout# -----------------------------------------------------------------# Listener configuration (Allows remote connections)# -----------------------------------------------------------------# Bind to port 1883 on all network interface cards (NICs)listener1883 0.0.0.0# Allow connections from clients without username/password (for dev testing)allow_anonymoustrue