In this third part of our IoT project series, we'll establish the communication backbone of our system by setting up an MQTT broker using Eclipse Mosquitto. The broker acts as the central message hub that enables real-time communication between ESP32 devices and our PyQt5 dashboard.
Creating a Virtual Network Adapter (Windows)Before setting up Mosquitto, we need to create a stable network environment. A virtual network adapter provides a consistent IP address that won't change when switching between different network connections.
Network Configuration
For this project, we'll use the following network configuration:
- IPv4 address: 192.168.1.52
- Subnet mask: 255.255.255.0
- Default IPv4 gateway: 192.168.1.1
Virtual Adapter Setup
Creating a virtual network adapter in Windows involves accessing the Device Manager and adding a Microsoft Loopback Adapter. This process ensures your MQTT broker maintains a consistent IP address regardless of your primary network connection. For a detailed visual guide on creating the virtual network adapter, refer to this tutorial: Virtual Network Adapter Setup
Mosquitto Broker Installation & ConfigurationInstallation Process
- Download Eclipse Mosquitto from the official website
- Install Mosquitto with default settings
- follow this detailed visual tutorial:Install Mosquitto MQTT Broker
Configuration Setup
- Open Command Prompt CMD as Administrator
- Navigate to the installation directory (
C:\Program Files\mosquitto
)
cd "C:\Program Files\mosquitto"
- Create a user account for MQTT authentication:
mosquitto_passwd -c passwd demo
- When prompted, enter the password:
azerty
This creates a user named "demo
" with the password "azerty
" in the passwd file.
- Create a backup of the original configuration file:
copy mosquitto.conf mosquitto.conf.bak
- Edit the configuration file:
notepad mosquitto.conf
- Add the following configuration to enable secure MQTT communication:
listener 1883 0.0.0.0
allow_anonymous false
password_file C:\Program Files\mosquitto\passwd
This configuration makes the broker listen on port 1883 from all network interfaces, disablesanonymous connections for security, and defines the location of the password file.
- Start the Mosquitto broker with the new configuration:
mosquitto -c mosquitto.conf
MQTT Explorer Installation & ConfigurationMQTT Explorer provides a visual interface for monitoring and testing MQTT communication, making it invaluable for debugging and development.
Installation
- Download MQTT Explorer from the official GitHub repository
- Install with default settings
- Launch the application
Connection Configuration
- Connection Name: mqtt.test
- Host: 192.168.1.52 same as my IPv4 address
- Port: 1883
- Username: demo
- Password: azerty
Advanced Topic Configuration
System Topics:
mqtt/request
- General system requestsmqtt/response
- System responses
Device-Specific Topics:
arduino/Led
- LED control commandsarduino/Weather
- Weather sensor dataarduino/weather_alerts
- Weather alert notificationsarduino/Weather_threshold
- Weather threshold settingsarduino/sensor
- Water level sensor dataarduino/sensor_Control
- Water level control commandsarduino/LoadCell
- Load cell measurementsarduino/MPU6050
- Accelerometer/Gyroscope dataarduino/gas
- Gas sensor readings
These topics create a structured communication framework that organizes data flow between different IoT modules and the dashboard.
MQTT Client ImplementationThe Python MQTT client implementation provides the communication layer between our PyQt5 dashboard and the Mosquitto broker. Here are the key functions from our MqttClient
class:
Connection Management
def connect_to_broker(self, broker, port, username, password):
self._connection_result = None
if username and password:
self.client.username_pw_set(username, password)
try:
self.client.connect(broker, port, 60)
self.client.loop_start()
return self._connection_result
except Exception as e:
print(f"Failed to connect to MQTT broker: {e}")
return -1
Topic Subscription with Handler Registration
def subscribe_to_topic(self, topic, handler=None):
if self.client.is_connected():
self.client.subscribe(topic)
print(f"Subscribed to topic: {topic}")
else:
self._topics_to_subscribe.append(topic)
if handler:
self._topic_handlers[topic] = handler
Message Publishing
def publish(self, topic, message):
if self.client:
self.client.publish(topic, message)
print(f"Published '{message}' to '{topic}'")
Intelligent Message Routing
The client implements smart message routing through callback handlers:
def on_message(self, client, userdata, msg):
topic = msg.topic
payload = msg.payload.decode()
if topic in self._topic_handlers:
self._topic_handlers[topic](topic, payload)
This architecture allows different IoT projects to register their own message handlers, creating a clean separation of concerns and enabling modular development.
Testing the SetupAfter completing the installation and configuration:
- Verify Mosquitto: Check that the broker is running and listening on port 1883
- Test MQTT Explorer: Connect using the configured credentials
- Publish Test Messages: Send test messages to verify communication
In this article, we established the MQTT communication infrastructure essential for our IoT ecosystem. We configured a virtual network adapter for network stability, set up the Mosquitto broker with security authentication, and configured MQTT Explorer for monitoring and debugging. The Python MQTT client implementation provides robust communication capabilities with intelligent message routing and connection management.
In the next part of this series, IoT Projects Part 4: LED & Button Control with MQTT, we'll dive into our first hardware project, implementing basic digital I/O control through MQTT communication to demonstrate the complete data flow from ESP32 to dashboard.
That's all!If you have any questions or suggestions, don’t hesitate to leave a comment below.
Comments