MQTT Periodic Publish Example
Overview
The MQTT Periodic Publish Example is a comprehensive demonstration of implementing the NetBurner MQTT client at a deep level, designed for developers who want to customize every aspect of the MQTT interaction. This example showcases advanced MQTT publishing techniques using periodic timers, semaphores, and quality of service (QoS) configurations.
Application Features
Core Functionality
- Dual Publishing Modes: The application operates in two distinct modes:
- Echo Mode (PublishCounter): Sends sequential binary data chunks every 10 seconds
- Sine Wave Mode: Publishes calculated sine wave values at different frequencies
MQTT Topics
The application publishes to three primary MQTT topics:
testTopic/example/echo - Binary counter data in echo mode
testTopic/example/sine_x - Sine wave values (sin(x))
testTopic/example/sine_3x - Scaled sine wave values (3*sin(x))
Advanced MQTT Features
- Quality of Service (QoS): Implements "At Least Once" delivery guarantee
- Publish Acknowledgment Callbacks: Custom handlers for publication confirmations
- Connection State Management: Automatic handling of connection establishment
- Priority-based Message Queuing: Configurable message priorities for publishing
- Semaphore-based Flow Control: Prevents message queue overflow
Technical Implementation
Key Components
MQTT Client Configuration
- Uses
MQTT::ConfiguredClient with broker settings from the configuration system
- Implements connection notifications and automatic reconnection handling
- Supports secure connections with certificate/key storage via filesystem
Interval Timing System
- IntervalTimer: Manages periodic publishing operations
- IntervalAction: Defines callback actions for different publishing tasks
- OS Semaphores: Provides thread-safe publishing coordination
Publishing Mechanisms
- Sine Wave Publishers:
- Calculate mathematical sine values with configurable step size
- Publish floating-point values as formatted strings
- Implement separate callbacks for acknowledgment handling
- Counter Publisher:
- Generates sequential binary data (64 bytes per message)
- Uses union structure for efficient byte/word access
- Publishes at lower priority with fire-and-forget semantics
State Management
The application operates in three distinct run states:
- State 0: Connection establishment and timer initialization
- State 1: Active publishing with user interaction support
- State 2: Graceful shutdown and cleanup
Application Behavior
Startup Sequence
- Initialization: Network stack and filesystem setup
- Connection Waiting: Displays "NotConnected" until MQTT broker connection established
- Timer Start: Shows "Starting IntervalSem: {timer}" when connected and ready
- Active Publishing: Begins sending data according to configured intervals
Runtime Operation
- Echo Mode: Sends 64-byte binary counter data every 5 seconds to echo topic
- Sine Mode: Publishes calculated sine values with different frequencies
sine_x published every 4th cycle
sine_3x published every cycle
- Interactive Control: Press any key to toggle between modes or stop operation
Message Flow Control
- Uses semaphores to prevent overwhelming the MQTT broker
- Implements timeout mechanisms for reliable message delivery
- Provides graceful fallback when broker becomes unavailable
Configuration Requirements
Network Setup
- Requires active network connection before MQTT operations begin
- Waits up to 5 seconds for network establishment during startup
MQTT Broker Settings
- Broker URI, username, password, and other client configurations are managed through the NetBurner configuration system
- Supports secure connections with TLS/SSL certificates stored in filesystem
Development Environment
- Designed for NetBurner embedded systems
- Requires NNDK (NetBurner Network Development Kit)
- File system support recommended for certificate management
Known Issues
- Subscription and message reception functionality for the echo topic is not currently implemented (marked as TODO)
- The application currently focuses on publishing; subscriber callbacks need to be added for complete bidirectional communication
Usage Notes
This example is ideal for developers who need:
- Fine-grained control over MQTT message publishing
- Understanding of advanced QoS implementation
- Integration of periodic timers with MQTT operations
- Custom acknowledgment handling and flow control
- Educational reference for NetBurner MQTT client implementation