NetBurner 3.5.6
PDF Version
Advanced MQTT Echo Example

MQTT Echo Application

Overview

This NetBurner MQTT Echo application demonstrates advanced MQTT client functionality by subscribing to a specific topic and echoing received messages to the serial console. The application uses low-level MQTT protocol handling to provide detailed message processing capabilities.

Features

  • MQTT Topic Subscription: Automatically subscribes to testTopic/HelloWorld
  • Message Echo: Outputs all received messages to the serial console
  • Low-Level Protocol Handling: Uses file descriptors and readall/writeall functions for direct message processing
  • Connection Management: Handles MQTT connection establishment and monitoring
  • Property Handling: Processes MQTT message properties (metadata) appropriately
  • Error Handling: Manages subscription termination and connection failures

Architecture

Key Components

  • ConfiguredClient: MQTT client configured through the NetBurner configuration system
  • TopicHandler: Low-level message handler that processes incoming MQTT Publish messages
  • EchoSubscriber: Callback function that handles message reception and console output

Message Processing Flow

  1. Application establishes connection to MQTT broker
  2. Subscribes to the configured topic (testTopic/HelloWorld)
  3. Receives MQTT Publish messages through file descriptor interface
  4. Processes message properties (skips them in this implementation)
  5. Reads message payload and outputs to serial console
  6. Handles connection status changes and subscription termination

Configuration

The MQTT client configuration (broker URI, username, password, certificates) is managed through the NetBurner configuration system rather than hardcoded values.

Default Settings

#define MQTT_TOPIC_NAME "testTopic/HelloWorld"
#define MQTT_PUBLISH_MSG "Hello World"

Technical Details

File Descriptor Interface

This application demonstrates the low-level MQTT interface using:

  • readall() function for reading message data
  • writeall() function for console output
  • Direct file descriptor manipulation for message processing

Message Structure Handling

The application properly handles MQTT Publish message structure:

  • Properties Section: MQTT v5.0 properties for metadata (skipped in this example)
  • Payload Section: Actual message content (echoed to console)

Connection Management

  • Waits for active network connection (5-second timeout)
  • Monitors MQTT connection status continuously
  • Automatically attempts subscription when connection is established
  • Handles connection failures gracefully

Usage

  1. Network Setup: Ensure the device is connected to a network
  2. MQTT Configuration: Configure MQTT broker settings through NetBurner config
  3. Deploy Application: Flash the application to the NetBurner device
  4. Monitor Output: Watch the serial console for received messages
  5. Test Messaging: Publish messages to testTopic/HelloWorld from any MQTT client

Console Output

The application provides detailed console output including:

  • Connection status updates
  • Subscription confirmation with handler ID
  • Received message topic information
  • Message payload content
  • Subscription termination notifications

Error Handling

  • Subscription Termination: Detects when subscriptions end and reports reason codes
  • Invalid Message Types: Filters out non-Publish messages
  • Connection Failures: Continuously monitors and reports connection status
  • File System Support: Conditionally initializes filesystem for certificate storage

Dependencies

  • NetBurner NNDK (NetBurner Network Development Kit)
  • MQTT library support
  • Network stack initialization
  • Optional: Filesystem support for certificates

Development Notes

  • System diagnostics are enabled for development (should be disabled for production)
  • Uses 10-second polling interval for connection monitoring
  • 4-second timeout for subscription operations
  • Supports both platforms with and without filesystem capabilities

Example Use Cases

  • IoT device message monitoring
  • MQTT broker testing and validation
  • Network communication debugging
  • Real-time message processing demonstrations
  • Integration testing for MQTT-based systems