MQTT Advanced Pub/Sub Example
Overview
This NetBurner application demonstrates advanced MQTT publish/subscribe functionality with additional features including remote configuration management and GPIO pin control over MQTT. The application serves as a comprehensive example of integrating MQTT communication with embedded system control capabilities.
Features
Core MQTT Functionality
- Automatic Publishing: Publishes "Hello World" messages to
testTopic/HelloWorld every 10 seconds
- Message Subscription: Subscribes to the same topic and echoes received messages to the serial console
- Connection Management: Handles MQTT connection establishment and recovery automatically
- Rate Limiting: Implements publish rate limiting (20 messages per 5 seconds, burst of 4)
Advanced Features
- Remote Configuration: Exposes NetBurner Config system via MQTT topic
device/nburnxxxxxx/config
- GPIO Control: Allows remote control of device pins through MQTT topics
device/nburnxxxxxx/pins/+
- MAC-Based Topics: Uses device MAC address for unique topic identification
- Multi-Platform Support: Supports various NetBurner platforms with different pin configurations
MQTT Topics
Published Topics
testTopic/HelloWorld - Publishes "Hello World" message every 10 seconds
Subscribed Topics
testTopic/HelloWorld - Echoes received messages to serial console
device/nburn_{MAC}/config - Configuration management interface
device/nburn_{MAC}/pins/+ - GPIO pin control interface
- Note
{MAC} is replaced with the last 3 octets of the device's MAC address (e.g., device/nburn123456/config)
GPIO Control
Pin Control Format
The application accepts pin control messages in the following formats:
For platforms with direct pin access (NANO54415, SB800EX, SOMRT1061, SBE70LC):**
- Topic:
device/nburn_{MAC}/pins/{pin_number}
Examples: device/nburn123456/pins/1, device/nburn123456/pins/15
For platforms with header/pin structure (MOD5441X):**
- Topic:
device/nburn_{MAC}/pins/{header}/{pin}
- Examples:
device/nburn123456/pins/1/5, device/nburn123456/pins/2/3
Control Values
Pins can be controlled using various message formats:
1 or true - Set pin HIGH
0 or false - Set pin LOW
- Any numeric value - Set pin to that boolean value (0 = LOW, non-zero = HIGH)
Application Flow
- Initialization
- Initialize file system (if not present)
- Initialize network stack
- Wait for active network connection
- Configure MQTT client with rate limiting
- Connection Phase
- Establish MQTT connection using configured broker settings
- Wait for connection establishment before proceeding
- Operation Phase
- Subscribe to echo topic (
testTopic/HelloWorld)
- Enter main loop:
- Publish "Hello World" message every 10 seconds
- Process incoming MQTT messages
- Handle pin control requests
- Monitor for user input to change states
- Message Handling
- Echo messages: Forward received messages to serial console
- Pin control: Parse topic and message to control GPIO pins
- Configuration: Handle remote configuration changes
Configuration
The application uses the NetBurner Config system for MQTT broker configuration:
- Broker URI
- Username/Password authentication
- SSL/TLS certificates (stored in file system)
- Connection parameters
Configuration can be modified remotely via the MQTT configuration topic.
Build Requirements
- NetBurner NNDK (NetBurner Network Development Kit)
- Supported NetBurner hardware platform
- MQTT broker for testing
Usage Instructions
- Setup
- Configure MQTT broker settings using NetBurner Config system
- Flash the application to your NetBurner device
- Ensure network connectivity
- Testing
- Monitor serial output for connection status and published messages
- Use MQTT client to subscribe to
testTopic/HelloWorld to see published messages
- Publish messages to
testTopic/HelloWorld to test echo functionality
- Control GPIO pins by publishing to appropriate pin control topics
- Interactive Control
- Press any key during operation to change application states
- Use serial console to monitor application status and debug information
Platform-Specific Notes
Supported Platforms
- NANO54415: Direct pin access via
Pins[] array
- SB800EX: Direct pin access via
Pins[] array
- SOMRT1061: Direct pin access via
Pins[] array
- SBE70LC: Direct pin access via
Pins[] array
- MOD5441X: Header-based pin access (J1/J2 headers)
Pin Control Differences
- Direct access platforms: Use single pin number (e.g.,
/pins/5)
- Header-based platforms: Use header/pin format (e.g.,
/pins/1/5 for J1 pin 5)
Error Handling
- Connection Loss: Automatically attempts reconnection
- Subscription Failures: Reports error codes and reason strings
- Invalid Pin Messages: Silently ignores malformed pin control messages
- Rate Limiting: Prevents message flooding with configurable limits
Debug Information
The application provides extensive debug output including:
- Connection status updates
- Subscription confirmation
- Message reception notifications
- Pin control operations
- Error conditions and recovery attempts
Security Considerations
- Uses NetBurner's built-in MQTT security features
- Supports SSL/TLS encryption when configured
- MAC-based topic naming provides device-specific addressing
- Configuration changes logged for audit trail