Setup Lab Guide

Setup Project 3: MQTT Broker & Cloud IoT Platform Setup

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.

HOME ROUTER 192.168.1.1 ESP32 Client IP: 192.168.1.105 Publishes to Broker Writes to Cloud API Ubuntu VM Broker IP: 192.168.1.120 Mosquitto (Port 1883) Bridged Interface (enp0s3) ThingSpeak Cloud Platform api.thingspeak.com (HTTPS)
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.

ubuntu@iot-vm:~$ sudo apt update && sudo apt install -y mosquitto mosquitto-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.

ubuntu@iot-vm:~$ sudo nano /etc/mosquitto/mosquitto.conf
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.

1. ESP32 Acquisition Reads physical DHT22 Reads physical LDR 2. Local MQTT Client Publishes to VM IP Topic: /office/telemetry JSON payload, QoS 0 3. Mosquitto VM Broker Listens on 1883 Routes to Subscribers 4. ThingSpeak REST API HTTP POST (port 80) Renders line graphs
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 # ----------------------------------------------------------------- persistence true persistence_location /var/lib/mosquitto/ # ----------------------------------------------------------------- # Logging configuration # ----------------------------------------------------------------- log_dest file /var/log/mosquitto/mosquitto.log log_dest stdout # ----------------------------------------------------------------- # Listener configuration (Allows remote connections) # ----------------------------------------------------------------- # Bind to port 1883 on all network interface cards (NICs) listener 1883 0.0.0.0 # Allow connections from clients without username/password (for dev testing) allow_anonymous true
5. Deliverables Summary

Created Artifacts

  • Modified configuration file: /etc/mosquitto/mosquitto.conf.
  • Saved connection profile in MQTT Explorer pointing to your VM's IP address.
  • Configured ThingSpeak channel showing Field 1 ("Temperature") and Field 2 ("Light").

Verification Proof

  • A screenshot of the terminal window running mosquitto_sub displaying a message published from another window.
  • A screenshot of the ThingSpeak dashboard showing the empty public channel graphs, ready to receive incoming sensor telemetry data.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes