MQTT Theory Explained With Diagrams
MQTT Theory Explained With Diagrams
Here's a structured progression of MQTT C++ code, starting from basic connectivity to advanced
message handling and persistence. I'll break it into steps, so you can build up your knowledge.
Installation on Linux/macOS
sh
sh
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER_ADDRESS, CLIENT_ID);
try {
std::cout << "Connecting to the MQTT broker..." << std::endl;
client.connect();
std::cout << "Connected successfully!" << std::endl;
client.disconnect();
std::cout << "Disconnected from the broker." << std::endl;
} catch (const mqtt::exception& exc) {
std::cerr << "MQTT Error: " << exc.what() << std::endl;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/123
return 0;
}
✅ Key Concepts:
Connects to a broker (`hivemq.com` as an example).
Publishes a message to a topic.
Disconnects from the broker.
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER_ADDRESS, CLIENT_ID);
Callback cb;
client.set_callback(cb);
try {
std::cout << "Connecting to broker..." << std::endl;
client.connect();
std::cout << "Connected and subscribing to topic: " << TOPIC << std::endl;
// Keep running
while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }
return 0;
}
✅ Key Concepts:
Uses callbacks to handle incoming messages.
Subscribes to a topic and listens for messages continuously.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/123
A robust client that handles disconnections, persistent sessions, and QoS 2 messaging.
cpp
#include <iostream>
#include <mqtt/async_client.h>
public:
Callback(mqtt::async_client& cli) : client(cli) {}
void on_success(const mqtt::token&) override { std::cout << "Action success" << std::endl; }
void on_failure(const mqtt::token&) override { std::cout << "Action failed" << std::endl; }
int main() {
mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID, "./persistence");
Callback cb(client);
client.set_callback(cb);
mqtt::connect_options connOpts;
connOpts.set_clean_session(false); // Persistent session
try {
std::cout << "Connecting..." << std::endl;
client.connect(connOpts)->wait();
std::cout << "Connected!" << std::endl;
// Keep running
while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }
return 0;
}
✅ Key Concepts:
Uses async_client for non-blocking operations.
Implements reconnection logic in case of failures.
Persistent session ensures messages aren’t lost.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/123
Uses QoS 2 (exactly-once messaging) for reliability.
On Linux/macOS:
sh
On Windows:
sh
Final Thoughts
🔹 Beginner: Connect, publish, and subscribe.
🔹 Intermediate: Use callbacks for real-time messages.
🔹 Advanced: Implement persistence, QoS 2, and reconnection handling.
🔹 Pro: Run your own broker with Mosquitto.
Would you like me to guide you on securing MQTT with TLS authentication? 🚀
yes
To secure your MQTT communication, TLS (Transport Layer Security) ensures encryption and prevents
unauthorized access. Below is a step-by-step guide on configuring TLS in C++ with Eclipse Paho MQTT.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/123
A Server certificate
A Client certificate and private key (optional for client authentication)
sh
sh
sh
ini
listener 8883
cafile /etc/mosquitto/ca.crt
certfile /etc/mosquitto/server.crt
keyfile /etc/mosquitto/server.key
require_certificate true
use_identity_as_username true
🔹 This enables TLS on port 8883 and requires clients to present valid certificates.
Restart Mosquitto:
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/123
Modify your MQTT client to use SSL/TLS encryption.
cpp
#include <iostream>
#include <mqtt/async_client.h>
int main() {
mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID);
Callback cb;
client.set_callback(cb);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(true);
try {
std::cout << "Connecting securely to MQTT broker..." << std::endl;
client.connect(connOpts)->wait();
std::cout << "Secure connection established!" << std::endl;
client.subscribe(TOPIC, 1);
return 0;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/123
1. Start the Mosquitto subscriber:
sh
sh
5. Enhancements
🔹 Using Mutual TLS Authentication
If `require_certificate true` is set in Mosquitto, the client must provide `client.crt` and
`client.key`.
ini
allow_anonymous false
password_file /etc/mosquitto/passwords.txt
Create credentials:
sh
cpp
connOpts.set_user_name("username");
connOpts.set_password("password");
Final Thoughts
✅ Basic Security: Use TLS encryption to prevent data leaks.
✅ Advanced Security: Implement mutual TLS authentication with client certificates.
✅ Authentication: Add username & password authentication for extra security.
Would you like help with deploying MQTT on Docker with TLS? 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/123
yes
This guide will help you set up an MQTT broker (Mosquitto) with TLS in Docker, ensuring encrypted
communication for secure messaging.
sh
Generate CA Certificate
sh
sh
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/123
ini
listener 8883
protocol mqtt
cafile /mosquitto/config/certs/ca.crt
certfile /mosquitto/config/certs/server.crt
keyfile /mosquitto/config/certs/server.key
require_certificate true
use_identity_as_username true
allow_anonymous false
password_file /mosquitto/config/passwords.txt
sh
touch passwords.txt
mosquitto_passwd -b passwords.txt myuser mypassword
yaml
version: '3.8'
services:
mosquitto:
image: eclipse-mosquitto
container_name: mqtt_broker
restart: unless-stopped
networks:
- mqtt_network
volumes:
- ./config/mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
- ./config/certs:/mosquitto/config/certs:ro
- ./config/passwords.txt:/mosquitto/config/passwords.txt:ro
ports:
- "8883:8883"
docker-compose up -d
✅
This will: Start Mosquitto in a Docker container.
✅ Secure MQTT with TLS authentication.
✅ Enforce username/password authentication.
Check logs:
sh
sh
sh
cpp
#include <iostream>
#include <mqtt/async_client.h>
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/123
const std::string CLIENT_CERT = "/certs/client.crt";
const std::string CLIENT_KEY = "/certs/client.key";
int main() {
mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID);
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(true);
connOpts.set_user_name("myuser");
connOpts.set_password("mypassword");
try {
std::cout << "Connecting securely to MQTT broker..." << std::endl;
client.connect(connOpts)->wait();
std::cout << "Secure connection established!" << std::endl;
client.subscribe(TOPIC, 1);
mqtt::message_ptr msg = mqtt::make_message(TOPIC, "Hello Secure MQTT from C++");
msg->set_qos(1);
client.publish(msg)->wait();
return 0;
}
dockerfile
FROM ubuntu:20.04
RUN apt update && apt install -y libpaho-mqttpp3-dev libpaho-mqtt3c-dev g++ cmake
WORKDIR /app
COPY . /app
CMD ["./mqtt_client"]
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/123
2. Run the client inside Docker
sh
Final Thoughts
✅ Dockerized Mosquitto Broker with TLS encryption.
✅ C++ Client in Docker securely publishing & subscribing.
✅ Mutual Authentication using client certificates.
✅ Username & Password Authentication for extra security.
Would you like guidance on deploying this setup on Kubernetes? 🚀
When developing MQTT-based systems, using well-structured design patterns ensures scalability,
reliability, and efficiency. Below are different MQTT coding designs, progressing from basic to
enterprise-level architectures.
Publisher:
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, "publisher1");
client.connect();
client.disconnect();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/123
Subscriber:
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, "subscriber1");
Callback cb;
client.set_callback(cb);
client.connect();
client.subscribe(TOPIC, 1);
✅ Best Practices:
Use QoS 1 or 2 for reliability.
Use unique client IDs to prevent disconnections.
cpp
class MQTTClientFactory {
public:
static mqtt::async_client create_client(const std::string& broker, const std::string& client_id
return mqtt::async_client(broker, client_id);
}
};
// Usage
mqtt::async_client publisher = MQTTClientFactory::create_client("tcp://broker.hivemq.com:1883", "pub
mqtt::async_client subscriber = MQTTClientFactory::create_client("tcp://broker.hivemq.com:1883", "su
✅ Best Practices:
Makes code more scalable and maintainable.
Ensures consistent initialization across clients.
cpp
class MQTTCommand {
public:
virtual void execute(mqtt::async_client& client) = 0;
};
// Usage
mqtt::async_client client("tcp://broker.hivemq.com:1883", "command_client");
PublishCommand publishCmd("test/topic", "Hello Command Pattern");
SubscribeCommand subscribeCmd("test/topic");
publishCmd.execute(client);
subscribeCmd.execute(client);
✅ Best Practices:
Allows dynamic command execution.
Decouples MQTT operations from application logic.
cpp
class MQTTMiddleware {
mqtt::async_client client;
public:
MQTTMiddleware(const std::string& broker, const std::string& clientID)
: client(broker, clientID) {}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/123
mqtt::message_ptr msg = mqtt::make_message(topic, message);
msg->set_qos(1);
client.publish(msg);
}
void subscribe(const std::string& topic) { client.subscribe(topic, 1); }
};
✅ Best Practices:
Encapsulates MQTT logic into a service.
Makes future enhancements easier (e.g., adding security).
cpp
class MQTTEvent {
public:
virtual void handle_event(mqtt::const_message_ptr msg) = 0;
};
✅ Best Practices:
Allows multiple event types (temperature, humidity, etc.).
Decouples event handling from MQTT logic.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/123
cpp
class MQTTLoadBalancer {
std::vector<std::string> brokers = {
"tcp://broker1.com:1883", "tcp://broker2.com:1883"
};
int index = 0;
public:
std::string get_next_broker() {
index = (index + 1) % brokers.size();
return brokers[index];
}
};
// Usage
MQTTLoadBalancer lb;
mqtt::async_client client(lb.get_next_broker(), "client_id");
✅ Best Practices:
Distributes load across multiple brokers.
Improves fault tolerance in case of broker failure.
sh
mosquitto -c /etc/mosquitto/mosquitto.conf
cpp
✅ Best Practices:
Reduces cloud communication costs.
Improves latency by processing data locally.
Final Thoughts
✔️ Basic MQTT Design → Publisher-Subscriber model.
✔️ Advanced MQTT Patterns → Factory, Command, Middleware.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/123
✔️ Scalable Solutions → Load Balancing, Edge Computing.
✔️ Enterprise-Grade → Event-Driven MQTT, Fault-Tolerant Brokers.
Would you like MQTT microservices architecture for a real-world system? 🚀
both
To create a scalable and fault-tolerant MQTT system, microservices architecture ensures modular,
loosely coupled services for better performance and maintainability.
Architecture Diagram
pgsql
+--------------------+ +------------------+
| Edge Devices | -----> | MQTT Broker |
| (Sensors, Cameras)| | (Mosquitto) |
+--------------------+ +------------------+
|
+-----------------------------------+--------------------------------+
| MQTT Microservices |
| |
| +------------------+ +-------------------+ +------------+ |
| | Message Handler | -> | Data Storage (DB) | -> | Analytics | |
| +------------------+ +-------------------+ +------------+ |
| |
| +-----------------+ +----------------+ +---------------+ |
| | Notification Svc| -> | API Gateway | -> | Frontend (UI)| |
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/123
| +-----------------+ +----------------+ +---------------+ |
+--------------------------------------------------------------------+
C++ Implementation
cpp
#include <iostream>
#include <mqtt/async_client.h>
int main() {
mqtt::async_client client(SERVER, "message_handler");
Callback cb;
client.set_callback(cb);
client.connect();
client.subscribe(TOPIC, 1);
✅ Key Features:
Subscribes to `iot/sensor/data`.
Sends messages to a Data Storage Service (Flask API).
Python Implementation
python
app = Flask(__name__)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/123
client = MongoClient("mongodb://localhost:27017/")
db = client.mqtt_data
@app.route('/store', methods=['POST'])
def store():
data = request.form.get('data')
db.sensors.insert_one({"message": data})
return "Data stored", 200
if __name__ == '__main__':
app.run(port=5000)
✅ Key Features:
Accepts MQTT messages from `Message Processor`.
Saves data into MongoDB.
Python Implementation
python
BROKER = "localhost"
TOPIC = "iot/sensor/data"
EMAIL_THRESHOLD = 50 # Example threshold
def send_email(alert_msg):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("[email protected]", "your_password")
server.sendmail("[email protected]", "[email protected]", alert_msg)
server.quit()
client = mqtt.Client()
client.connect(BROKER)
client.subscribe(TOPIC)
client.on_message = on_message
client.loop_forever()
✅ Key Features:
Sends email alerts if sensor data exceeds threshold.
Can be expanded to SMS, push notifications.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/123
Python FastAPI
python
app = FastAPI()
client = MongoClient("mongodb://localhost:27017/")
db = client.mqtt_data
@app.get("/data")
def get_data():
return list(db.sensors.find({}, {"_id": 0}))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
✅ Key Features:
Serves MQTT historical data via HTTP API.
Allows frontend/dashboard integration.
javascript
useEffect(() => {
const client = mqtt.connect(MQTT_BROKER);
client.subscribe("iot/sensor/data");
✅ Key Features:
Real-time updates via WebSockets.
Can be expanded into charts & analytics.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/123
4. Deploying with Docker
Use Docker Compose for deployment.
yaml
version: '3.8'
services:
mqtt_broker:
image: eclipse-mosquitto
container_name: mqtt_broker
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
database:
image: mongo
container_name: mongo_db
ports:
- "27017:27017"
api_gateway:
build: ./api_gateway
ports:
- "8000:8000"
notification_service:
build: ./notification_service
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-broker
spec:
replicas: 3
template:
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 1883
For a production-ready MQTT system, we need: ✅ High availability with failover brokers.
✅ TLS encryption & authentication for security.
✅ Efficient resource management (thread pools, async calls).
✅ Robust error handling & reconnect logic.
✅ Logging & monitoring integration.
1. Architecture Overview
In production, the system must handle:
pgsql
+----------------------------+
| MQTT Load Balancer |
| (HAProxy/Nginx + WebSockets)|
+------------+---------------+
|
+------------+------------+
| MQTT Brokers (HA) |
| (Eclipse Mosquitto/EMQX)|
+------------+------------+
| |
+------------+ +------------+
| C++ Client | | Python Client |
| (TLS, QoS2)| | (Alerts) |
+------------+ +------------+
For logging:
sh
cpp
#include <iostream>
#include <thread>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Settings
const std::string SERVER_URI = "ssl://mqtt-broker:8883";
const std::string CLIENT_ID = "prod_client";
const std::string TOPIC = "production/data";
public:
explicit Callback(mqtt::async_client& cli) : client(cli) {}
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
Callback cb(client);
client.set_callback(cb);
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/123
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false); // Persistent session
connOpts.set_keep_alive_interval(20);
connOpts.set_automatic_reconnect(true);
connOpts.set_user_name("user");
connOpts.set_password("password");
try {
spdlog::info("Connecting securely to MQTT broker...");
client.connect(connOpts)->wait();
spdlog::info("Connected successfully!");
client.subscribe(TOPIC, 2);
mqtt::message_ptr pubmsg = mqtt::make_message(TOPIC, "Production data payload");
pubmsg->set_qos(2);
client.publish(pubmsg)->wait();
return 0;
}
Modify `mosquitto.conf`:
ini
listener 8883
protocol mqtt
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
require_certificate true
use_identity_as_username true
allow_anonymous false
password_file /mosquitto/config/passwords.txt
✅ Security Features:
TLS encryption
Client authentication
Password protection
yaml
version: '3.8'
services:
mosquitto:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/123
image: eclipse-mosquitto
container_name: mqtt_broker
restart: always
networks:
- mqtt_network
volumes:
- ./config:/mosquitto/config:ro
- ./certs:/mosquitto/certs:ro
ports:
- "8883:8883"
networks:
mqtt_network:
driver: bridge
Run:
sh
docker-compose up -d
Add to `mosquitto.conf`:
ini
listener 9001
protocol websockets
allow_anonymous true
Run Prometheus:
yaml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'mosquitto'
static_configs:
- targets: ['mqtt_broker:9001']
Run:
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/123
5.1. Mosquitto Deployment
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-broker
spec:
replicas: 3
selector:
matchLabels:
app: mqtt-broker
template:
metadata:
labels:
app: mqtt-broker
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 8883
volumeMounts:
- name: config
mountPath: /mosquitto/config
- name: certs
mountPath: /mosquitto/certs
volumes:
- name: config
configMap:
name: mosquitto-config
- name: certs
secret:
secretName: mqtt-certificates
yes
To ensure high availability and fault tolerance, we need load balancing and failover mechanisms for
MQTT brokers in Kubernetes.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 26/123
✅ Handles high traffic – Distributes load across multiple brokers.
✅ Ensures uptime – If a broker fails, another takes over.
✅ Prevents single points of failure – Automatic failover.
✅ Supports scalability – Easily add brokers when needed.
2. Architecture Overview
We deploy multiple MQTT brokers and use a load balancer (HAProxy/Nginx/Kubernetes Service) for
distributing traffic.
diff
+----------------------------+
| Kubernetes Ingress (LB) |
| (HAProxy / Nginx) |
+----------------------------+
|
+------------+------------+------------+
| MQTT Broker 1 | MQTT Broker 2 | MQTT Broker 3 |
| (Eclipse Mosquitto) | (Eclipse Mosquitto) | (Eclipse Mosquitto) |
+------------+------------+------------+
|
+------------+------------+
| Clients (C++/Python) |
| Connecting via LB |
+------------+------------+
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mosquitto-config
data:
mosquitto.conf: |
listener 1883
allow_anonymous true
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 27/123
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-broker
spec:
replicas: 3
selector:
matchLabels:
app: mqtt-broker
template:
metadata:
labels:
app: mqtt-broker
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 1883
volumeMounts:
- name: config
mountPath: /mosquitto/config
volumes:
- name: config
configMap:
name: mosquitto-config
✅ Key Features:
3 replicas ensure high availability.
Uses ConfigMap for centralized configuration.
yaml
apiVersion: v1
kind: Service
metadata:
name: mqtt-loadbalancer
spec:
type: LoadBalancer
selector:
app: mqtt-broker
ports:
- protocol: TCP
port: 1883
targetPort: 1883
ini
global
log stdout format raw local0
maxconn 4096
defaults
log global
timeout connect 10s
timeout client 1m
timeout server 1m
frontend mqtt
bind *:1883
default_backend mqtt_brokers
backend mqtt_brokers
balance leastconn
server broker1 mqtt-broker-0.mqtt-broker.default.svc.cluster.local:1883 check
server broker2 mqtt-broker-1.mqtt-broker.default.svc.cluster.local:1883 check
server broker3 mqtt-broker-2.mqtt-broker.default.svc.cluster.local:1883 check
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
spec:
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
containers:
- name: haproxy
image: haproxy:latest
ports:
- containerPort: 1883
volumeMounts:
- name: config
mountPath: /usr/local/etc/haproxy
volumes:
- name: config
configMap:
name: haproxy-config
yaml
apiVersion: v1
kind: Service
metadata:
name: mqtt-haproxy
spec:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 29/123
type: LoadBalancer
selector:
app: haproxy
ports:
- protocol: TCP
port: 1883
targetPort: 1883
cpp
#include <iostream>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
int main() {
mqtt::async_client client(SERVERS[0], CLIENT_ID);
Callback cb;
client.set_callback(cb);
try {
spdlog::info("Connecting...");
client.connect()->wait();
spdlog::info("Connected!");
client.subscribe(TOPIC, 1);
mqtt::message_ptr msg = mqtt::make_message(TOPIC, "Load Balancing Test");
msg->set_qos(1);
client.publish(msg)->wait();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 30/123
} catch (const mqtt::exception& exc) {
spdlog::error("Error: {}", exc.what());
}
return 0;
}
sh
sh
sh
./mqtt_client
sh
7. Final Thoughts
✔ Load Balancing via HAProxy for efficient traffic distribution.
✔ Failover Mechanism in C++ client to reconnect automatically.
✔ High Availability using multiple MQTT brokers in Kubernetes.
✔ Scalable & Fault-Tolerant MQTT architecture.
Would you like help with TLS security & authentication for this HA setup? 🚀
yes
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 31/123
Securing High-Availability MQTT with TLS & Authentication in Kubernetes
sh
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 32/123
sh
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mosquitto-config
data:
mosquitto.conf: |
listener 8883
protocol mqtt
allow_anonymous false
password_file /mosquitto/config/passwords.txt
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
require_certificate true
use_identity_as_username true
sh
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-secure-broker
spec:
replicas: 3
selector:
matchLabels:
app: mqtt-broker
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 33/123
template:
metadata:
labels:
app: mqtt-broker
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 8883
volumeMounts:
- name: config
mountPath: /mosquitto/config
- name: certs
mountPath: /mosquitto/certs
volumes:
- name: config
configMap:
name: mosquitto-config
- name: certs
secret:
secretName: mqtt-tls-secrets
Create `haproxy.cfg`:
ini
global
log stdout format raw local0
maxconn 4096
defaults
log global
timeout connect 10s
timeout client 1m
timeout server 1m
frontend mqtt
bind *:8883 ssl crt /etc/haproxy/certs/server.pem
default_backend mqtt_brokers
backend mqtt_brokers
balance leastconn
server broker1 mqtt-broker-0.mqtt-broker.default.svc.cluster.local:8883 check ssl verify require
server broker2 mqtt-broker-1.mqtt-broker.default.svc.cluster.local:8883 check ssl verify require
server broker3 mqtt-broker-2.mqtt-broker.default.svc.cluster.local:8883 check ssl verify require
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
spec:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 34/123
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
containers:
- name: haproxy
image: haproxy:latest
ports:
- containerPort: 8883
volumeMounts:
- name: certs
mountPath: /etc/haproxy/certs
volumes:
- name: certs
secret:
secretName: mqtt-tls-secrets
cpp
#include <iostream>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// TLS Certificates
const std::string CA_CERT = "/certs/ca.crt";
const std::string CLIENT_CERT = "/certs/client.crt";
const std::string CLIENT_KEY = "/certs/client.key";
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
Callback cb;
client.set_callback(cb);
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false);
connOpts.set_user_name("mqtt_user");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 35/123
connOpts.set_password("mqtt_password");
try {
spdlog::info("Connecting securely...");
client.connect(connOpts)->wait();
spdlog::info("Connected to secure MQTT broker!");
client.subscribe(TOPIC, 2);
mqtt::message_ptr msg = mqtt::make_message(TOPIC, "Secure Message");
msg->set_qos(2);
client.publish(msg)->wait();
return 0;
}
1. What is MQTT?
MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe messaging
protocol designed for low-bandwidth, high-latency, or unreliable networks. It is commonly used in
IoT (Internet of Things), real-time data streaming, and low-power devices.
✅ Key Features:
Efficient: Uses minimal bandwidth.
Lightweight: Low overhead, ideal for embedded systems.
Reliable: Supports Quality of Service (QoS) levels.
Scalable: Supports millions of devices.
Bi-directional: Devices can both publish and subscribe.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 36/123
2. MQTT Architecture Overview
MQTT follows a Publish-Subscribe (Pub/Sub) model, unlike traditional Client-Server models.
arduino
🔴 Problems:
Scalability issues: Each client requires a direct connection.
Higher latency: Requests must be processed one by one.
lua
3. MQTT Components
3.1. MQTT Broker
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 37/123
4. MQTT Workflow
Step 1: Connection
Client ID
Username & Password (Optional)
TLS Encryption (Optional)
Step 2: Subscription
arduino
Step 3: Publishing
bash
📌 Use Cases:
QoS 0 → Sensor data (if some messages are lost, it's fine).
QoS 1 → Notifications (ensure delivery but allow duplicates).
QoS 2 → Payment transactions (no duplicate messages).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 38/123
Topics are hierarchical and use `/` as a separator.
bash
iot/temperature
iot/humidity
iot/room1/temp
6.2. Wildcards
✅ Use Case:
A device publishes its status ("Online"), and new subscribers see it instantly.
✅ Use Case:
If a smart home device loses connection, the broker informs others:
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 39/123
8.1. TLS Encryption
pgsql
client.connect(username="user", password="pass")
Client Certificates:
Uses X.509 certificates for strong authentication.
lua
Final Thoughts
✔ MQTT is ideal for low-power, real-time communication.
✔ Supports pub/sub model, QoS levels, retained messages, and LWT.
✔ TLS encryption, authentication, and ACLs improve security.
✔ High availability with load balancing and clustering.
Would you like real-world MQTT project examples? 🚀
yes
lua
📌 Implementation Details:
Sensors publish temperature data to `home/temperature`.
Smart switches & lights subscribe to `home/lights` and `home/switch`.
Mobile App subscribes to all topics for remote monitoring.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 41/123
Topic Publisher Subscriber
`home/lights` Mobile App Smart Lights
`home/switch` Mobile App Smart Switch
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
lua
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 42/123
cpp
#include <iostream>
#include <mqtt/async_client.h>
int main() {
mqtt::async_client client(SERVER, CLIENT_ID);
Callback cb;
client.set_callback(cb);
client.connect();
client.subscribe(TOPIC, 1);
lua
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 43/123
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
lua
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 44/123
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
MQTT in automotive
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 45/123
Detect unauthorized vehicle use.
🛠 3. Predictive Maintenance
Detect component wear & tear.
Send alerts for servicing before breakdown occurs.
Optimize spare parts inventory.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 46/123
Topic Publisher Subscriber
This C++ program simulates a vehicle sending GPS data to an MQTT broker.
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 47/123
client.disconnect();
}
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
Callback cb;
client.set_callback(cb);
client.connect();
client.subscribe(TOPIC, 1);
cpp
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store("/certs/ca.crt");
sslOpts.set_key_store("/certs/client.crt");
sslOpts.set_private_key("/certs/client.key");
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
🔑 6.2. Authentication
Use username/password for access control.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 48/123
cpp
connOpts.set_user_name("vehicle_123");
connOpts.set_password("securepass");
pgsql
user fleet_manager
topic read vehicle/+/gps
topic write vehicle/OTA/update
lua
+--------------------+ +----------------+
| Autonomous Vehicle | -----> | MQTT Broker |
+--------------------+ +----------------+
| |
+----------------+ +-------------------+
| Traffic Lights | <-------> | Roadside Sensors |
+----------------+ +-------------------+
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 50/123
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
LIDAR
Radar
Cameras
GPS & IMU sensors
+-------------------+
| Vehicle ECU |
+-------------------+
|
+---------------+
| Sensor Fusion |
+---------------+
|
+----------------+
| MQTT Broker |
+----------------+
|
+---------------+
| AI Decision |
+---------------+
#include <iostream>
#include <mqtt/client.h>
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 51/123
const std::string TOPIC = "autonomous/vehicle/sensors";
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
+-----------------+ +----------------+
| Real Vehicle | -----> | MQTT Broker |
+-----------------+ +----------------+
| |
+-----------------+ +-----------------+
| Digital Twin AI | | Cloud Storage |
+-----------------+ +-----------------+
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 52/123
5. Remote Diagnostics & Over-the-Air (OTA) Updates
MQTT enables remote vehicle software updates and real-time diagnostics.
+---------------------+ +----------------+
| Vehicle ECU | -----> | MQTT Broker |
+---------------------+ +----------------+
| |
+------------------+ +-----------------+
| Service Center | | Cloud Storage |
+------------------+ +-----------------+
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
std::string error_msg = "{ \"code\": \"P0420\", \"desc\": \"Catalyst System Efficiency Below Th
mqtt::message_ptr msg = mqtt::make_message(TOPIC, error_msg);
client.publish(msg);
client.disconnect();
}
+----------------+ +----------------+
| EV Battery | -----> | MQTT Broker |
+----------------+ +----------------+
| |
+----------------+ +----------------+
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 53/123
| Charging Station | | Payment System |
+----------------+ +----------------+
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 54/123
1.1. High-Level MQTT Architecture
pgsql
+----------------------------+
| Load Balancer (HA) |
| (HAProxy / Nginx) |
+----------------------------+
|
+-----------+-----------+-----------+
| MQTT Broker 1 | MQTT Broker 2 | MQTT Broker 3 |
| (Eclipse Mosquitto/EMQX) | (HiveMQ) | (Mosquitto) |
+-----------+-----------+-----------+
| |
+----------------+ +----------------+
| Edge Gateway 1 | | Edge Gateway 2 |
+----------------+ +----------------+
| |
+-----------+-----------+-----------+
| Vehicle 1 | Vehicle 2 | Vehicle N |
| (ECU & Sensors) | (ADAS & LIDAR) | (EV Battery) |
+-----------+-----------+-----------+
bash
vehicle/{id}/gps
vehicle/{id}/engine/temp
vehicle/{id}/tire/pressure
vehicle/{id}/battery/status
vehicle/{id}/ota/update
v2v/collision/alert
v2i/traffic/signal
markdown
ini
frontend mqtt
bind *:8883 ssl crt /etc/haproxy/certs/server.pem
default_backend mqtt_brokers
backend mqtt_brokers
balance leastconn
server broker1 mqtt-broker-1:8883 check ssl verify required ca-file /etc/haproxy/certs/ca.crt
server broker2 mqtt-broker-2:8883 check ssl verify required ca-file /etc/haproxy/certs/ca.crt
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-broker
spec:
replicas: 3
selector:
matchLabels:
app: mqtt-broker
template:
metadata:
labels:
app: mqtt-broker
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 8883
volumeMounts:
- name: certs
mountPath: /mosquitto/certs
volumes:
- name: certs
secret:
secretName: mqtt-tls-secrets
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 56/123
MQTT TLS Configuration (`mosquitto.conf`)
ini
listener 8883
protocol mqtt
allow_anonymous false
password_file /mosquitto/config/passwords.txt
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
require_certificate true
use_identity_as_username true
📌 Use Case: Monitor fleet location, fuel consumption & engine health.
C++ MQTT Code for GPS Tracking
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 57/123
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
cpp
#include <iostream>
#include <mqtt/client.h>
int main() {
mqtt::client client(SERVER, CLIENT_ID);
client.connect();
client.disconnect();
}
+----------------------------+
| HAProxy Load Balancer |
| (TLS Secured) |
+------------+---------------+
|
+------------+------------+------------+
| MQTT Broker 1 | MQTT Broker 2 | MQTT Broker 3 |
| (Mosquitto/EMQX) | (HiveMQ) | (EMQX) |
+------------+------------+------------+
|
+------------+------------+------------+
| Edge Gateway 1 | Edge Gateway 2 |
+------------+------------+------------+
|
+------------+------------+------------+
| Vehicle 1 | Vehicle 2 | Vehicle N |
| (ECU, Sensors) | (ADAS, LIDAR) | (EV Battery) |
+------------+------------+------------+
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 59/123
This C++ MQTT Client handles: ✔TLS encryption 🔒
✔ Auto-reconnection 🔄
✔ QoS 2 (Exactly Once Delivery) ✅
✔ Multi-threaded processing ⚡
2.1. Install Dependencies
sh
For logging:
sh
cpp
#include <iostream>
#include <thread>
#include <vector>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Config
const std::string SERVER_URI = "ssl://mqtt-haproxy:8883";
const std::string CLIENT_ID = "vehicle_telemetry";
const std::string TOPIC = "vehicle/123/telemetry";
public:
explicit Callback(mqtt::async_client& cli) : client(cli) {}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 60/123
void on_success(const mqtt::token&) override { spdlog::info("MQTT action succeeded"); }
void on_failure(const mqtt::token&) override { spdlog::warn("MQTT action failed"); }
};
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
Callback cb(client);
client.set_callback(cb);
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false); // Persistent session
connOpts.set_keep_alive_interval(20);
connOpts.set_automatic_reconnect(true);
connOpts.set_user_name("vehicle_123");
connOpts.set_password("securepass");
try {
spdlog::info("Connecting securely to MQTT broker...");
client.connect(connOpts)->wait();
spdlog::info("Connected successfully!");
client.subscribe(TOPIC, 2);
mqtt::message_ptr pubmsg = mqtt::make_message(TOPIC, "{ \"speed\": 60, \"battery\": 80 }");
pubmsg->set_qos(2);
client.publish(pubmsg)->wait();
return 0;
}
✅ Features
✔ TLS encryption for secure vehicle telemetry.
✔ Auto-reconnect logic for failover.
✔ Persistent session ( ) to avoid message loss.
✔ Multi-threaded message processing for scalability.
`clean_session=false`
yaml
version: '3.8'
services:
mosquitto:
image: eclipse-mosquitto
container_name: mqtt_broker
restart: always
networks:
- mqtt_network
volumes:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 61/123
- ./config:/mosquitto/config:ro
- ./certs:/mosquitto/certs:ro
ports:
- "8883:8883"
networks:
mqtt_network:
driver: bridge
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-broker
spec:
replicas: 3
selector:
matchLabels:
app: mqtt-broker
template:
metadata:
labels:
app: mqtt-broker
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 8883
volumeMounts:
- name: certs
mountPath: /mosquitto/certs
volumes:
- name: certs
secret:
secretName: mqtt-tls-secrets
Modify `mosquitto.conf`:
ini
listener 9001
protocol websockets
allow_anonymous true
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 62/123
4.2. Prometheus Configuration
yaml
scrape_configs:
- job_name: 'mosquitto'
static_configs:
- targets: ['mqtt-broker:9001']
Transport Layer Security (TLS) is a cryptographic protocol that ensures secure communication
between MQTT clients and brokers by providing:
1️⃣ Client Hello → The MQTT client sends a request to the broker, listing supported encryption
algorithms.
2️⃣ Server Hello → The broker responds with a selected encryption algorithm and TLS certificate.
3️⃣ Client Certificate (Optional) → If the broker requires mutual TLS authentication, the client sends
its certificate.
4️⃣ Key Exchange → Both sides generate a shared encryption key using the selected algorithm.
5️⃣ Session Established → Secure connection is now ready for encrypted MQTT communication.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 63/123
Step 2: MQTT over TLS
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 64/123
Client Certificate & Key (For mTLS)
sh
sh
sh
ini
listener 8883
protocol mqtt
allow_anonymous false
password_file /mosquitto/config/passwords.txt
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
require_certificate true
use_identity_as_username true
Restart Mosquitto:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 65/123
sh
cpp
#include <iostream>
#include <mqtt/async_client.h>
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
Callback cb;
client.set_callback(cb);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false);
connOpts.set_user_name("mqtt_user");
connOpts.set_password("securepass");
try {
std::cout << "Connecting securely to MQTT broker..." << std::endl;
client.connect(connOpts)->wait();
std::cout << "Secure connection established!" << std::endl;
client.subscribe(TOPIC, 2);
mqtt::message_ptr msg = mqtt::make_message(TOPIC, "{ \"speed\": 60, \"battery\": 80 }");
msg->set_qos(2);
client.publish(msg)->wait();
return 0;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 66/123
✅ TLS Encryption 🔒
✅ mTLS Authentication 🔑
✅ Failover Reconnect 🔄
7. Testing Secure MQTT Connection
Mosquitto Client Test
sh
ini
tls_version tlsv1.3
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 67/123
MQTT simulater in C++
+-----------------+ +----------------+
| MQTT Clients | ---> | MQTT Broker |
| (Simulated) | | (Mosquitto) |
+-----------------+ +----------------+
| |
+----------------+ +----------------+
| Vehicle Data 1 | | Vehicle Data 2 |
| {GPS, Speed} | | {Battery, Temp} |
+----------------+ +----------------+
For logging:
sh
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Configuration
const std::string SERVER_URI = "ssl://mqtt-broker:8883";
const std::string TOPIC_PREFIX = "vehicle/";
const int NUM_VEHICLES = 5; // Adjust for larger simulations
// TLS Certificates
const std::string CA_CERT = "/certs/ca.crt";
const std::string CLIENT_CERT = "/certs/client.crt";
const std::string CLIENT_KEY = "/certs/client.key";
public:
VehicleClient(int id) : client(SERVER_URI, "vehicle_" + std::to_string(id)), vehicle_id(id) {
topic = TOPIC_PREFIX + std::to_string(vehicle_id) + "/telemetry";
}
void connect_and_publish() {
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false);
connOpts.set_automatic_reconnect(true);
try {
spdlog::info("Vehicle {} connecting to MQTT broker...", vehicle_id);
client.connect(connOpts)->wait();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 69/123
spdlog::info("Vehicle {} connected!", vehicle_id);
while (true) {
std::string telemetry_data = generate_telemetry_data(vehicle_id);
mqtt::message_ptr pubmsg = mqtt::make_message(topic, telemetry_data);
pubmsg->set_qos(1);
client.publish(pubmsg)->wait();
spdlog::info("Vehicle {} published data: {}", vehicle_id, telemetry_data);
std::this_thread::sleep_for(std::chrono::seconds(2));
}
} catch (const mqtt::exception& exc) {
spdlog::error("Vehicle {} MQTT Error: {}", vehicle_id, exc.what());
}
}
};
int main() {
spdlog::info("Starting MQTT Vehicle Simulator...");
start_simulation();
return 0;
}
yaml
version: '3.8'
services:
mqtt_simulator:
build: .
container_name: mqtt_simulator
restart: always
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 70/123
networks:
- mqtt_network
networks:
mqtt_network:
driver: bridge
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-simulator
spec:
replicas: 10 # Simulate 10 vehicles per pod
selector:
matchLabels:
app: mqtt-simulator
template:
metadata:
labels:
app: mqtt-simulator
spec:
containers:
- name: mqtt-simulator
image: mqtt-simulator:latest
env:
- name: SERVER_URI
value: "ssl://mqtt-haproxy:8883"
Modify `mosquitto.conf`:
ini
listener 9001
protocol websockets
allow_anonymous true
yaml
scrape_configs:
- job_name: 'mosquitto'
static_configs:
- targets: ['mqtt-broker:9001']
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 71/123
✅ Creates real-time dashboards of vehicle telemetry.
8. MQTT Simulator Testing
8.1. Subscribe to Vehicle Data
sh
sh
sh
For logging:
sh
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Configuration
const std::string SERVER_URI = "ssl://mqtt-haproxy:8883"; // TLS Secured MQTT Broker
const std::string TOPIC_PREFIX = "vehicle/";
const int NUM_VEHICLES = 10; // Change this value to simulate more vehicles
// TLS Certificates
const std::string CA_CERT = "/certs/ca.crt";
const std::string CLIENT_CERT = "/certs/client.crt";
const std::string CLIENT_KEY = "/certs/client.key";
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 73/123
// Generate randomized telemetry data
std::string generate_telemetry_data(int vehicle_id) {
int speed = speed_dist(gen);
double lat = lat_dist(gen);
double lon = lon_dist(gen);
public:
VehicleClient(int id) : client(SERVER_URI, "vehicle_" + std::to_string(id)), vehicle_id(id) {
topic = TOPIC_PREFIX + std::to_string(vehicle_id) + "/telemetry";
}
void connect_and_publish() {
mqtt::ssl_options sslOpts;
sslOpts.set_trust_store(CA_CERT);
sslOpts.set_key_store(CLIENT_CERT);
sslOpts.set_private_key(CLIENT_KEY);
mqtt::connect_options connOpts;
connOpts.set_ssl(sslOpts);
connOpts.set_clean_session(false);
connOpts.set_automatic_reconnect(true);
connOpts.set_keep_alive_interval(30); // Keep connection alive
try {
spdlog::info("Vehicle {} connecting to MQTT broker...", vehicle_id);
client.connect(connOpts)->wait();
spdlog::info("Vehicle {} connected!", vehicle_id);
while (true) {
std::string telemetry_data = generate_telemetry_data(vehicle_id);
mqtt::message_ptr pubmsg = mqtt::make_message(topic, telemetry_data);
pubmsg->set_qos(2); // QoS 2: Exactly Once Delivery
client.publish(pubmsg)->wait();
spdlog::info("Vehicle {} published data: {}", vehicle_id, telemetry_data);
std::this_thread::sleep_for(std::chrono::seconds(2));
}
} catch (const mqtt::exception& exc) {
spdlog::error("Vehicle {} MQTT Error: {}", vehicle_id, exc.what());
}
}
};
int main() {
spdlog::info(" 🚗
Starting MQTT Vehicle Simulator...");
start_simulation();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 74/123
return 0;
}
dockerfile
FROM ubuntu:20.04
RUN apt update && apt install -y libpaho-mqttpp3-dev libpaho-mqtt3c-dev cmake g++
COPY . /app
WORKDIR /app
RUN g++ -std=c++17 mqtt_simulator.cpp -o mqtt_simulator -lpaho-mqttpp3 -lpaho-mqtt3c -pthread
CMD ["./mqtt_simulator"]
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-simulator
spec:
replicas: 5 # Adjust number of vehicles
selector:
matchLabels:
app: mqtt-simulator
template:
metadata:
labels:
app: mqtt-simulator
spec:
containers:
- name: mqtt-simulator
image: mqtt-simulator:latest
env:
- name: SERVER_URI
value: "ssl://mqtt-haproxy:8883"
Modify `mosquitto.conf`:
ini
listener 9001
protocol websockets
allow_anonymous true
yaml
scrape_configs:
- job_name: 'mosquitto'
static_configs:
- targets: ['mqtt-broker:9001']
sh
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 76/123
MQTT packet inspection
diff
+------------+---------------+-------------+
| FixedHdr | VariableHdr | Payload |
+------------+---------------+-------------+
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 77/123
✅ Captures MQTT packets in real time.
✅ Decodes MQTT messages (topic, QoS, retain flag).
✅ Detects anomalies and security threats.
Steps to Capture MQTT Packets in Wireshark
sh
mqtt
3. Analyze MQTT packets with topic names, payloads, and QoS levels.
sh
sh
sudo tcpdump -i eth0 'tcp port 1883 and tcp[13] & 0x10 != 0' -X
MQTT brokers like Mosquitto support verbose logging to inspect MQTT packets.
Edit `mosquitto.conf`:
ini
log_type all
log_dest file /var/log/mosquitto/mqtt_packets.log
Restart Mosquitto:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 78/123
sh
This program:
cpp
#include <iostream>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Configuration
const std::string SERVER_URI = "tcp://mqtt-broker:1883";
const std::string CLIENT_ID = "packet_inspector";
const std::string TOPIC = "#"; // Subscribe to all topics
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
PacketInspector inspector;
client.set_callback(inspector);
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
try {
client.connect(connOpts)->wait();
client.subscribe(TOPIC, 1);
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
} catch (const mqtt::exception& exc) {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 79/123
spdlog::error("MQTT Error: {}", exc.what());
}
return 0;
}
This version:
cpp
#include <iostream>
#include <mqtt/async_client.h>
#include "spdlog/spdlog.h"
// MQTT Configuration
const std::string SERVER_URI = "tcp://mqtt-broker:1883";
const std::string CLIENT_ID = "packet_publisher";
const std::string TOPIC = "vehicle/telemetry";
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
try {
client.connect(connOpts)->wait();
spdlog::info("Connected to MQTT Broker!");
while (true) {
std::string data = generate_packet_data();
mqtt::message_ptr pubmsg = mqtt::make_message(TOPIC, data);
pubmsg->set_qos(1);
pubmsg->set_retained(true);
client.publish(pubmsg)->wait();
std::this_thread::sleep_for(std::chrono::seconds(2));
}
} catch (const mqtt::exception& exc) {
spdlog::error("MQTT Error: {}", exc.what());
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 80/123
return 0;
}
🛡 Detecting Attacks:
Monitor unexpected MQTT packet types (`tcpdump` + Wireshark).
Block unauthorized clients using `mosquitto.conf`.
Detect replay attacks via unique message IDs.
what is spdlog
sh
sh
sh
#include <iostream>
#include <spdlog/spdlog.h>
int main() {
spdlog::info("This is an info log message!");
spdlog::warn("This is a warning log message!");
spdlog::error("This is an error log message!");
spdlog::debug("This is a debug message!");
return 0;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 82/123
pgsql
#include <spdlog/spdlog.h>
int main() {
spdlog::set_pattern("[%H:%M:%S] [%^%l%$] %v"); // Format: Time | Log Level | Message
spdlog::info("Formatted log message!");
}
🖥 Console Output
css
📌 Log to a File
cpp
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
int main() {
auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/output.log");
file_logger->info("Logging to a file!");
}
📂 logs/output.log
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 83/123
css
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
int main() {
spdlog::init_thread_pool(8192, 1); // Async queue size, 1 worker thread
auto async_logger = spdlog::basic_logger_mt<spdlog::async_factory>("async_logger", "logs/async.l
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 84/123
cpp
#include <iostream>
#include <mqtt/async_client.h>
#include <spdlog/spdlog.h>
// MQTT Configuration
const std::string SERVER_URI = "tcp://mqtt-broker:1883";
const std::string CLIENT_ID = "mqtt_logger";
const std::string TOPIC = "vehicle/telemetry";
int main() {
mqtt::async_client client(SERVER_URI, CLIENT_ID);
LoggerCallback logger;
client.set_callback(logger);
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
try {
client.connect(connOpts)->wait();
client.subscribe(TOPIC, 1);
spdlog::info("Listening for MQTT packets...");
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
} catch (const mqtt::exception& exc) {
spdlog::error("MQTT Error: {}", exc.what());
}
return 0;
}
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/syslog_sink.h>
🔥 Final Thoughts
✔ is a fast and easy-to-use C++ logging library.
✔ Supports file logging, async logging, and structured logs.
`spdlog`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 86/123
Test Steps:
Expected Outcome:
✔ Vehicle reconnects automatically if disconnected.
✔ Broker maintains persistent sessions if `clean_session=false`.
Send messages with QoS 0 (At most once), QoS 1 (At least once), QoS 2 (Exactly once).
Verify message loss, duplication, or correctness.
Test Steps:
1. Publish messages using `QoS 0`, `QoS 1`, and `QoS 2`.
2. Disconnect the client after publishing.
3. Reconnect & verify received messages.
Test Steps:
Expected Outcome:
✔ New subscribers receive the last retained message instantly.
Test Steps:
Expected Outcome:
✔ Connections without TLS fail.
✔ Packet capture should not reveal plaintext messages.
Test Steps:
Expected Outcome:
✔ Unauthorized clients cannot connect.
✔ Invalid credentials result in connection rejection.
Test Steps:
ini
user vehicle_123
topic read vehicle/123/#
topic write vehicle/123/gps
Expected Outcome:
✔ Clients can only access authorized topics.
✔ Unauthorized topic access is blocked.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 88/123
4️⃣ Load & Performance Testing
✅ Test Case 7: Scalability Test (1000 Vehicles)
Scenario:
Test Steps:
Expected Outcome:
✔ Broker handles high message throughput.
✔ Latency remains low (<100ms per message).
Test Steps:
Expected Outcome:
✔ Clients reconnect automatically after broker recovery.
✔ No message loss for `QoS 1/2` messages.
Test Steps:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 89/123
Expected Outcome:
✔ Edge broker successfully forwards messages to cloud.
✔ Minimal delay in telemetry propagation.
Test Steps:
Expected Outcome:
✔ Vehicle automatically reconnects after network recovery.
✔ QoS 1/2 messages are delivered after reconnection.
✅ Test Case 11: High Latency Network Conditions
Scenario:
Test Steps:
sh
Expected Outcome:
✔ MQTT should still deliver messages within QoS guarantees.
🔥 Final Thoughts
✅ Essential MQTT Tests for Automotive Systems
✔ Connection Stability – Auto-reconnect & persistent sessions.
✔ Message Reliability – QoS testing for accurate delivery.
✔ Security Testing – TLS, authentication, ACLs.
✔ Scalability – Load test with 1000+ vehicles.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 90/123
✔ Edge & Cloud Integration – MQTT bridging validation.
✔ Failure Handling – Broker crash recovery, network disruptions.
Would you like automated MQTT test scripts for large-scale simulation? 🚀
what else
Test Steps:
Expected Outcome:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 91/123
✔ Edge broker stores messages when cloud is unavailable.
✔ All stored messages are replayed once cloud is restored.
✔ No duplicate or missing messages (QoS 1 or 2).
✅ MQTT Feature Used: Persistent Sessions & Message Queueing.
8️⃣ Vehicle-to-Everything (V2X) Communication Reliability
📌 Scenario:
Vehicles exchange messages with other vehicles and infrastructure.
Requires low latency (<50ms) for real-time safety alerts.
Test Steps:
Expected Outcome:
Test Steps:
Expected Outcome:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 92/123
🔟 Time Synchronization & Message Ordering
📌 Scenario:
Vehicles send telemetry at high frequency (10ms intervals).
Messages must be processed in order to maintain data accuracy.
Test Steps:
Expected Outcome:
Test Steps:
Expected Outcome:
Test Steps:
Expected Outcome:
Test Steps:
Expected Outcome:
Final Thoughts
✔ Edge-to-Cloud Failover – Ensures seamless message buffering.
✔ V2X Communication – Low-latency vehicle safety alerts.
✔ Multi-Region MQTT Brokers – Cross-region scalability.
✔ Time-Sensitive Message Processing – Ensures correct ordering.
✔ Secure OTA Updates – Fault-tolerant firmware updates.
Would you like automated MQTT test scripts for large-scale vehicle simulation? 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 94/123
yes
📌 1. Installing Dependencies
Before running the tests, install the required libraries:
sh
python
import pytest
import asyncio
import paho.mqtt.client as mqtt
import json
import random
import time
from rich.console import Console
# MQTT Configuration
BROKER = "mqtt-broker.yourdomain.com"
PORT = 8883 # Use 1883 for non-TLS
VEHICLE_COUNT = 10
TOPIC_PREFIX = "vehicle/{}/telemetry"
USERNAME = "vehicle_client"
PASSWORD = "securepass"
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 95/123
TLS_CERT = "/certs/ca.crt"
console = Console()
else:
console.print(f"[red]Failed to connect. Code: {rc}
test_results.append("FAIL: MQTT Connection")
❌
[/red]")
client.on_connect = on_connect
client.on_publish = on_publish
client.on_message = on_message
if subscribe:
topic = TOPIC_PREFIX.format(vehicle_id)
client.subscribe(topic)
console.print(f"[magenta]Subscribed to {topic} 📡[/magenta]")
return client
# Disconnect Clients
for client in clients:
client.disconnect()
@pytest.mark.asyncio
async def test_mqtt_vehicle_simulation():
await simulate_vehicles()
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 96/123
assert "FAIL" not in test_results, "Some MQTT tests failed!"
# Run Tests
if __name__ == "__main__":
console.print("[yellow]Running MQTT Vehicle Simulation Tests...
pytest.main(["-v", "mqtt_test_suite.py"])
🚗[/yellow]")
sh
python mqtt_test_suite.py
@pytest.mark.asyncio
async def test_mqtt_connection_failure():
client = mqtt.Client("test_client")
client.username_pw_set("wrong_user", "wrong_pass")
try:
client.connect(BROKER, PORT, keepalive=10)
test_results.append("FAIL: MQTT Connection Should Have Failed")
except:
console.print("[red]Expected Connection Failure ✅
[/red]")
test_results.append("PASS: MQTT Connection Failure")
@pytest.mark.asyncio
async def test_mqtt_retained_messages():
client = create_mqtt_client(999)
topic = "vehicle/999/status"
await asyncio.sleep(2)
assert "ONLINE" in test_results, "Retained message not received!"
python
VEHICLE_COUNT = 1000
@pytest.mark.asyncio
async def test_large_scale_mqtt_load():
await simulate_vehicles()
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-test-suite
spec:
replicas: 3 # Simulate multiple test instances
selector:
matchLabels:
app: mqtt-test
template:
metadata:
labels:
app: mqtt-test
spec:
containers:
- name: mqtt-tester
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 98/123
image: python:3.9
command: ["python", "/app/mqtt_test_suite.py"]
what else?
sh
Expected Outcome
Test Steps
1. Disable internet access on the edge gateway (`iptables block` MQTT traffic).
2. Publish telemetry to the edge broker.
3. Restore cloud connectivity after 5 minutes.
4. Verify all stored messages are replayed to the cloud.
python
import time
import paho.mqtt.client as mqtt
BROKER_EDGE = "edge-broker.local"
BROKER_CLOUD = "cloud-broker.global"
TOPIC = "vehicle/123/telemetry"
client = mqtt.Client()
client.connect(BROKER_EDGE, 1883)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 100/123
client.on_publish = on_publish
Expected Outcome
Approach
python
import numpy as np
from sklearn.ensemble import IsolationForest
# Predict anomalies
predictions = model.predict(data)
for i, p in enumerate(predictions):
print(f"Message {i}: {'ANOMALY 🚨' if p == -1 else 'Normal ✅'}")
Expected Outcome
Message rejected 🚫
Unauthorized Topic `mosquitto_pub -t "vehicle/999/gps" -m
Access "Hacked!"`
Prometheus Configuration
yaml
scrape_configs:
- job_name: 'mqtt-broker'
static_configs:
- targets: ['mqtt-broker:9001']
Expected Outcome
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 102/123
🔥 Final Thoughts
✔ Network Testing – Simulate latency, jitter, and packet loss.
✔ Edge-to-Cloud Resilience – Ensure zero message loss in network failures.
✔ AI-Powered Anomaly Detection – Detect abnormal vehicle telemetry patterns.
✔ Security & Penetration Testing – Prevent MQTT cyberattacks.
✔ Real-Time Monitoring – Visualize MQTT data in Grafana dashboards.
Would you like a full-scale MQTT test automation framework for deployment? 🚀🚗
yes
📌 1. Installing Dependencies
sh
python
import pytest
import asyncio
import paho.mqtt.client as mqtt
import json
import random
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 103/123
import time
from rich.console import Console
from influxdb_client import InfluxDBClient, Point
from sklearn.ensemble import IsolationForest
import numpy as np
# MQTT Configuration
BROKER = "mqtt-broker.yourdomain.com"
PORT = 8883 # Use 1883 for non-TLS
VEHICLE_COUNT = 100 # Adjust for large-scale tests
TOPIC_PREFIX = "vehicle/{}/telemetry"
USERNAME = "vehicle_client"
PASSWORD = "securepass"
TLS_CERT = "/certs/ca.crt"
console = Console()
test_results = []
# MQTT Callbacks
def on_connect(client, userdata, flags, rc):
if rc == 0:
console.print(f"[green]Connected to MQTT Broker {BROKER}
test_results.append("PASS: MQTT Connection")
✅
[/green]")
else:
console.print(f"[red]Failed to connect. Code: {rc}
test_results.append("FAIL: MQTT Connection")
❌
[/red]")
if subscribe:
topic = TOPIC_PREFIX.format(vehicle_id)
client.subscribe(topic)
console.print(f"[magenta]Subscribed to {topic} 📡[/magenta]")
return client
for _ in range(5):
for i, client in enumerate(clients):
topic = TOPIC_PREFIX.format(i)
telemetry = generate_telemetry(i)
client.publish(topic, telemetry, qos=1)
await asyncio.sleep(1)
@pytest.mark.asyncio
async def test_mqtt_vehicle_simulation():
await simulate_vehicles()
assert "FAIL" not in test_results, "Some MQTT tests failed!"
pytest.main(["-v", "mqtt_test_framework.py"])
detect_anomalies()
python mqtt_test_framework.py
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 105/123
✅ If all tests pass:
css
✅ [/yellow]
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-test-suite
spec:
replicas: 3 # Run multiple test instances
selector:
matchLabels:
app: mqtt-test
template:
metadata:
labels:
app: mqtt-test
spec:
containers:
- name: mqtt-tester
image: python:3.9
command: ["python", "/app/mqtt_test_framework.py"]
python
import pytest
import asyncio
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 107/123
import paho.mqtt.client as mqtt
import json
import random
import time
from rich.console import Console
# MQTT Configuration
BROKER = "mqtt-broker.nftplatform.com"
PORT = 8883 # Use 1883 for non-TLS
NFT_ITEM = "CryptoPunk_9999"
TOPIC_AUCTION = f"nft/{NFT_ITEM}/auction"
TOPIC_PRICE = f"nft/{NFT_ITEM}/price"
TOPIC_OWNERSHIP = f"nft/{NFT_ITEM}/ownership"
USERNAME = "nft_user"
PASSWORD = "securepass"
TLS_CERT = "/certs/ca.crt"
console = Console()
test_results = []
# MQTT Callbacks
def on_connect(client, userdata, flags, rc):
if rc == 0:
console.print(f"[green]Connected to MQTT Broker {BROKER}
test_results.append("PASS: MQTT Connection")
✅
[/green]")
else:
console.print(f"[red]Failed to connect. Code: {rc}
test_results.append("FAIL: MQTT Connection")
❌
[/red]")
if subscribe_topics:
for topic in subscribe_topics:
client.subscribe(topic)
console.print(f"[magenta]Subscribed to {topic} 📡[/magenta]")
return client
client.loop_stop()
client.disconnect()
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 108/123
client.loop_stop()
client.disconnect()
@pytest.mark.asyncio
async def test_mqtt_nft_auction():
await simulate_nft_auction()
assert "FAIL" not in test_results, "Some MQTT tests failed!"
@pytest.mark.asyncio
async def test_mqtt_nft_transfer():
await simulate_nft_transfer()
assert "FAIL" not in test_results, "Some MQTT tests failed!"
if __name__ == "__main__":
console.print("[yellow]Running MQTT NFT Test Suite...
pytest.main(["-v", "mqtt_nft_test_framework.py"])
🚀[/yellow]")
python mqtt_nft_test_framework.py
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 109/123
Test Case Description
Price Manipulation Protection Ensure only authorized sellers can update NFT prices.
High-Load Testing Simulate 100K concurrent users subscribing to auctions.
Latency Testing Measure real-time message delivery speed.
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-nft-test
spec:
replicas: 5 # Run multiple test instances
selector:
matchLabels:
app: mqtt-nft
template:
metadata:
labels:
app: mqtt-nft
spec:
containers:
- name: mqtt-nft-tester
image: python:3.9
command: ["python", "/app/mqtt_nft_test_framework.py"]
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 110/123
🚀 Non-Functional Testing for MQTT in Automotive, IoT, and NFT
Systems
Non-functional testing (NFT) in MQTT ensures the performance, reliability, security, and scalability
of MQTT-based applications under real-world conditions.
import asyncio
import paho.mqtt.client as mqtt
import random
import time
BROKER = "mqtt-broker.yourdomain.com"
PORT = 1883
TOPIC = "performance/loadtest"
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 111/123
payload = f"Message from client {client_id} at {time.time()}"
client.publish(TOPIC, payload, qos=1)
await asyncio.sleep(random.uniform(0.1, 0.5)) # Variable message rate
client.disconnect()
asyncio.run(main())
Expected Outcome
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqtt-scalability-test
spec:
replicas: 10000 # Simulate 10K clients
selector:
matchLabels:
app: mqtt-test
template:
metadata:
labels:
app: mqtt-test
spec:
containers:
- name: mqtt-client
image: python:3.9
command: ["python", "/app/mqtt_test.py"]
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 112/123
Expected Outcome
Expected Outcome
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 113/123
Test Command/Script Expected Result
Brute-Force MQTT Unauthorized access
🚫
`hydra -L users.txt -P passwords.txt mqtt-
Login broker -V` blocked
Broker survives flood attack
DoS Attack Simulation `hping3 -S -p 1883 --flood mqtt-broker`
🛡
Unauthorized Topic
Access
`mosquitto_pub -t "nft/999/gps" -m
"Hacked!"`
Message rejected 🚫
✅ Ensures MQTT is protected against cyber threats.
📌 6. Network Fault & Recovery Testing
📌 Why?
🌍 Real-world networks experience latency, jitter, and packet loss.
🚗 Automotive systems rely on 5G, LTE, and satellite connectivity.
🛠️ Test Approach
1️⃣ Inject 500ms network latency.
2️⃣ Simulate 2% packet loss.
3️⃣ Measure message delay & recovery time.
🚀 Simulating Latency & Packet Loss
sh
sudo tc qdisc add dev eth0 root netem delay 500ms loss 2%
sleep 60
sudo tc qdisc del dev eth0 root netem
Expected Outcome
Expected Outcome
yes
python
# MQTT Configuration
BROKER = "mqtt-broker.yourdomain.com"
PORT = 1883
TOPIC = "mqtt/performance"
USERNAME = "test_user"
PASSWORD = "test_pass"
# InfluxDB Configuration
INFLUX_URL = "http://localhost:8086"
INFLUX_TOKEN = "your-influxdb-token"
INFLUX_ORG = "mqtt-testing"
INFLUX_BUCKET = "mqtt_data"
# MQTT Callbacks
def on_connect(client, userdata, flags, rc):
print(f"[INFO] Connected to MQTT Broker: {BROKER}")
client.subscribe(TOPIC)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 116/123
print(f"[INFO] Received message - Latency: {latency:.3f}s")
client.loop_forever()
yaml
global:
scrape_interval: 10s
evaluation_interval: 10s
scrape_configs:
- job_name: 'mqtt-broker'
static_configs:
- targets: ['mqtt-broker:9001']
- job_name: 'mqtt-client'
static_configs:
- targets: ['localhost:8086']
Reload Prometheus:
sh
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 117/123
Broker CPU > 80% (Overload warning).
sh
# Remove latency
sudo tc qdisc del dev eth0 root netem
python
import pytest
import requests
GRAFANA_API_URL = "http://localhost:3000/api/dashboards/db/mqtt-dashboard"
@pytest.mark.performance
def test_mqtt_latency():
response = requests.get(GRAFANA_API_URL)
latency = response.json()["dashboard"]["panels"][0]["value"]
assert latency < 500, f"High Latency Detected: {latency}ms"
@pytest.mark.scalability
def test_mqtt_throughput():
response = requests.get(GRAFANA_API_URL)
throughput = response.json()["dashboard"]["panels"][1]["value"]
assert throughput > 1000, f"Throughput Too Low: {throughput} msg/sec"
sh
pytest test_mqtt_performance.py
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 118/123
ini
[smtp]
enabled = true
host = smtp.gmail.com:587
user = [email protected]
password = your-email-password
from_address = [email protected]
sh
yes
python
import requests
import pandas as pd
import datetime
from fpdf import FPDF
from influxdb_client import InfluxDBClient
data = []
for table in result:
for record in table.records:
data.append({
"time": record.get_time(),
"latency": record.get_value(),
"message_rate": record.values.get("message_rate", 0)
})
df = pd.DataFrame(data)
return df
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, f"Report Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=T
pdf.ln(10)
# Add Table
pdf.set_font("Arial", style='B', size=12)
pdf.cell(50, 10, "Time", border=1)
pdf.cell(50, 10, "Latency (ms)", border=1)
pdf.cell(50, 10, "Message Rate", border=1)
pdf.ln()
pdf.set_font("Arial", size=10)
for _, row in df.tail(10).iterrows(): # Show last 10 records
pdf.cell(50, 10, str(row["time"]), border=1)
pdf.cell(50, 10, f"{row['latency']:.2f}", border=1)
pdf.cell(50, 10, f"{row['message_rate']:.2f}", border=1)
pdf.ln()
print("📄
pdf.output("mqtt_test_report.pdf")
Test report generated: mqtt_test_report.pdf")
python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# Email Configuration
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
SENDER_EMAIL = "[email protected]"
SENDER_PASSWORD = "your-email-password"
RECIPIENT_EMAIL = "[email protected]"
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 121/123
with open("mqtt_test_report.pdf", "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment", filename="mqtt_test_report.pdf")
msg.attach(part)
# Send Email
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
import requests
SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
def send_slack_notification():
with open("mqtt_test_report.pdf", "rb") as file:
response = requests.post(SLACK_WEBHOOK_URL, files={"file": file})
print(" ✅
if response.status_code == 200:
Test report sent to Slack!")
else:
print(" ❌
Failed to send Slack report!")
send_slack_notification()
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 122/123
yaml
on:
schedule:
- cron: "0 12 * * *" # Run daily at 12 PM UTC
jobs:
test-mqtt:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 123/123